From 5cd0f50e39ef9f2e7feefd06c91c79ec71ce9cc7 Mon Sep 17 00:00:00 2001 From: Alexandre Coderre-Chabot Date: Sun, 14 Apr 2024 00:39:08 -0700 Subject: [PATCH] Update clang-format and add clang-tidy --- source/Configuration.hpp | 8 ++++---- source/Matchings.hpp | 4 ++-- source/Messenger/Argument.hpp | 6 +++--- source/Messenger/Emailer.hpp | 9 +++++---- source/Messenger/Settings.hpp | 6 +++--- source/Participant.hpp | 16 ++++++++-------- source/Randomizer/Argument.hpp | 8 ++++---- source/Randomizer/Settings.hpp | 8 ++++---- source/String.hpp | 3 ++- test/Matchings.cpp | 4 ++-- test/Messenger/Emailer.cpp | 6 +++--- test/Participant.cpp | 6 +++--- 12 files changed, 43 insertions(+), 41 deletions(-) diff --git a/source/Configuration.hpp b/source/Configuration.hpp index 7690db4..4b71961 100644 --- a/source/Configuration.hpp +++ b/source/Configuration.hpp @@ -38,7 +38,7 @@ class Configuration { Configuration() = default; // Constructor. Constructs configuration details by reading them from a YAML configuration file. - Configuration(const std::filesystem::path& path) { + explicit Configuration(const std::filesystem::path& path) { if (!std::filesystem::exists(path)) { std::cout << "Cannot find the YAML configuration file at " << path << "; please check the file path." << std::endl; @@ -112,7 +112,7 @@ class Configuration { // Subject of the email message that will be sent to each participant. A default value is used if // no message subject is defined in the YAML configuration file. - const std::string& MessageSubject() const noexcept { + [[nodiscard]] const std::string& MessageSubject() const noexcept { return message_subject_; } @@ -120,12 +120,12 @@ class Configuration { // message body is defined in the YAML configuration file. A brief greeting of "Hello " is // automatically prepended to this body, and information regarding the participant's giftee is // automatically appended to this body. - const std::string& MessageBody() const noexcept { + [[nodiscard]] const std::string& MessageBody() const noexcept { return message_body_; } // Set of participants. - const std::set& Participants() const noexcept { + [[nodiscard]] const std::set& Participants() const noexcept { return participants_; } diff --git a/source/Matchings.hpp b/source/Matchings.hpp index a5a2116..6a9c879 100644 --- a/source/Matchings.hpp +++ b/source/Matchings.hpp @@ -83,7 +83,7 @@ class Matchings { } // Constructor. Constructs matchings by reading them from a given YAML file. - Matchings(const std::filesystem::path& path) { + explicit Matchings(const std::filesystem::path& path) { if (!std::filesystem::exists(path)) { std::cout << "Cannot find the YAML matchings file at " << path << "; please check the file path." << std::endl; @@ -132,7 +132,7 @@ class Matchings { // Map of gifter participant names to giftee participant names. For example, the map element // {Alice, Bob} means that Alice is the gifter and Bob is the giftee, such that Alice is Bob's // Secret Santa. - const std::map& GiftersToGiftees() const noexcept { + [[nodiscard]] const std::map& GiftersToGiftees() const noexcept { return gifters_to_giftees_; } diff --git a/source/Messenger/Argument.hpp b/source/Messenger/Argument.hpp index 3297e02..815a750 100644 --- a/source/Messenger/Argument.hpp +++ b/source/Messenger/Argument.hpp @@ -48,17 +48,17 @@ static const std::string Path{""}; } // namespace Value // Prints usage instructions and exits. Optional. -std::string_view Help() { +[[nodiscard]] std::string_view Help() { return Key::Help; } // Path to the YAML configuration file to be read. Required. -std::string Configuration() { +[[nodiscard]] std::string Configuration() { return Key::Configuration + " " + Value::Path; } // Path to the YAML matchings file to be read. Required. -std::string Matchings() { +[[nodiscard]] std::string Matchings() { return Key::Matchings + " " + Value::Path; } diff --git a/source/Messenger/Emailer.hpp b/source/Messenger/Emailer.hpp index ba6a704..6baea98 100644 --- a/source/Messenger/Emailer.hpp +++ b/source/Messenger/Emailer.hpp @@ -29,7 +29,7 @@ namespace SecretSanta::Messenger { // Composes the full email message body for a given gifter. Prefixes a brief greeting to the given // main message body and appends the giftee information. -std::string ComposeFullMessageBody( +[[nodiscard]] std::string ComposeFullMessageBody( const Participant& gifter, const Participant& giftee, const std::string& main_message_body) { std::string text; @@ -53,8 +53,9 @@ std::string ComposeFullMessageBody( } // Composes the command used to invoke the S-nail utility for a given gifter. -std::string ComposeCommand(const Participant& gifter, const std::string& message_subject, - const std::string& message_body) { +[[nodiscard]] std::string ComposeCommand( + const Participant& gifter, const std::string& message_subject, + const std::string& message_body) { return "echo \"" + message_body + "\" | s-nail --subject \"" + message_subject + "\" " + gifter.Email(); } @@ -91,7 +92,7 @@ void ComposeAndSendEmailMessages(const Configuration& configuration, const Match // Obtain the giftee information. const std::set::const_iterator giftee = - configuration.Participants().find({gifter_name_and_giftee_name->second}); + configuration.Participants().find(Participant{gifter_name_and_giftee_name->second}); if (giftee == configuration.Participants().cend()) { continue; diff --git a/source/Messenger/Settings.hpp b/source/Messenger/Settings.hpp index 337fc94..ff14541 100644 --- a/source/Messenger/Settings.hpp +++ b/source/Messenger/Settings.hpp @@ -64,12 +64,12 @@ class Settings { Settings& operator=(Settings&& other) noexcept = delete; // Path to the YAML configuration file to be read. - const std::filesystem::path& ConfigurationFile() const noexcept { + [[nodiscard]] const std::filesystem::path& ConfigurationFile() const noexcept { return configuration_file_; } // Path to the YAML matchings file to be read. - const std::filesystem::path& MatchingsFile() const noexcept { + [[nodiscard]] const std::filesystem::path& MatchingsFile() const noexcept { return matchings_file_; } @@ -142,7 +142,7 @@ class Settings { } // Returns whether there is at least one more element after the given element index. - bool AtLeastOneMore(const int index, const int count) const noexcept { + [[nodiscard]] bool AtLeastOneMore(const int index, const int count) const noexcept { return index + 1 < count; } diff --git a/source/Participant.hpp b/source/Participant.hpp index 629459e..6dc1330 100644 --- a/source/Participant.hpp +++ b/source/Participant.hpp @@ -37,14 +37,14 @@ class Participant { // Constructor. Creates a participant from a given name. The email address, street address, and // instructions are empty. Only used for searching through a set of participants. - Participant(const std::string& name) : name_(name) {} + explicit Participant(const std::string& name) : name_(name) {} // Constructor. Creates a participant from a YAML node of the form: // Alice Smith: // email: alice.smith@gmail.com // address: 123 First Ave, Apt 1, Townsville, CA, 91234 USA // instructions: Leave the package with the doorman in the lobby. - Participant(const YAML::Node& node) { + explicit Participant(const YAML::Node& node) { if (!node.IsMap()) { return; } @@ -90,27 +90,27 @@ class Participant { Participant& operator=(Participant&& other) noexcept = default; // Name of this participant. Each participant must have a unique name. - const std::string& Name() const noexcept { + [[nodiscard]] const std::string& Name() const noexcept { return name_; } // Email address of this participant. - const std::string& Email() const noexcept { + [[nodiscard]] const std::string& Email() const noexcept { return email_; } // Street address of this participant. - const std::string& Address() const noexcept { + [[nodiscard]] const std::string& Address() const noexcept { return address_; } // Additional instructions for mailing packages to this participant. - const std::string& Instructions() const noexcept { + [[nodiscard]] const std::string& Instructions() const noexcept { return instructions_; } // Prints this participant as a string. - std::string Print() const noexcept { + [[nodiscard]] std::string Print() const noexcept { std::string details; if (!email_.empty()) { @@ -144,7 +144,7 @@ class Participant { // email: alice.smith@gmail.com // address: 123 First Ave, Apt 1, Townsville, CA, 91234 USA // instructions: Leave the package with the doorman in the lobby. - YAML::Node YAML() const { + [[nodiscard]] YAML::Node YAML() const { YAML::Node node; node[name_]["email"] = email_; node[name_]["address"] = address_; diff --git a/source/Randomizer/Argument.hpp b/source/Randomizer/Argument.hpp index 8f6fb89..f00fd0c 100644 --- a/source/Randomizer/Argument.hpp +++ b/source/Randomizer/Argument.hpp @@ -54,22 +54,22 @@ static const std::string Path{""}; } // namespace Value // Prints usage instructions and exits. Optional. -std::string_view Help() { +[[nodiscard]] std::string_view Help() { return Key::Help; } // Path to the YAML configuration file to be read. Required. -std::string Configuration() { +[[nodiscard]] std::string Configuration() { return Key::Configuration + " " + Value::Path; } // Path to the YAML matchings file to be written. Optional. -std::string Matchings() { +[[nodiscard]] std::string Matchings() { return Key::Matchings + " " + Value::Path; } // Seed value for pseudo-random number generation. Optional. -std::string Seed() { +[[nodiscard]] std::string Seed() { return Key::Seed + " " + Value::Integer; } diff --git a/source/Randomizer/Settings.hpp b/source/Randomizer/Settings.hpp index d36aac9..00a96d7 100644 --- a/source/Randomizer/Settings.hpp +++ b/source/Randomizer/Settings.hpp @@ -64,18 +64,18 @@ class Settings { Settings& operator=(Settings&& other) noexcept = delete; // Path to the YAML configuration file to be read. - const std::filesystem::path& ConfigurationFile() const noexcept { + [[nodiscard]] const std::filesystem::path& ConfigurationFile() const noexcept { return configuration_file_; } // Path to the YAML matchings file to be written. If empty, no matchings file is written. - const std::filesystem::path& MatchingsFile() const noexcept { + [[nodiscard]] const std::filesystem::path& MatchingsFile() const noexcept { return matchings_file_; } // Optional seed value for pseudo-random number generation. If no value is specified, the seed // value is randomized. - constexpr const std::optional& RandomSeed() const noexcept { + [[nodiscard]] constexpr const std::optional& RandomSeed() const noexcept { return random_seed_; } @@ -156,7 +156,7 @@ class Settings { } // Returns whether there is at least one more element after the given element index. - bool AtLeastOneMore(const int index, const int count) const noexcept { + [[nodiscard]] bool AtLeastOneMore(const int index, const int count) const noexcept { return index + 1 < count; } diff --git a/source/String.hpp b/source/String.hpp index bcbd053..7b413f8 100644 --- a/source/String.hpp +++ b/source/String.hpp @@ -31,7 +31,8 @@ namespace SecretSanta { // Returns a copy of a given string where the copy has been padded to a given length with trailing // spaces. If the given string is already longer than the given length, nothing is changed. -std::string PadToLength(const std::string_view text, const std::size_t length) noexcept { +[[nodiscard]] std::string PadToLength( + const std::string_view text, const std::size_t length) noexcept { std::string padded_text{text}; const std::size_t text_length = text.size(); if (length > text_length) { diff --git a/test/Matchings.cpp b/test/Matchings.cpp index c486b9d..98b50b9 100644 --- a/test/Matchings.cpp +++ b/test/Matchings.cpp @@ -38,7 +38,7 @@ std::set CreateSampleParticipants() { } TEST(Matchings, ComparisonOperators) { - const Matchings first{{CreateSampleParticipantA()}}; + const Matchings first{std::set{Participant{CreateSampleParticipantA()}}}; const Matchings second{CreateSampleParticipants()}; EXPECT_EQ(first, first); EXPECT_NE(first, second); @@ -56,7 +56,7 @@ TEST(Matchings, ConstructorFromNoParticipants) { } TEST(Matchings, ConstructorFromOneParticipant) { - const Matchings matchings{{CreateSampleParticipantA()}}; + const Matchings matchings{std::set{Participant{CreateSampleParticipantA()}}}; EXPECT_EQ(matchings.GiftersToGiftees().size(), 1); EXPECT_NE(matchings.GiftersToGiftees().find("Alice Smith"), matchings.GiftersToGiftees().cend()); EXPECT_EQ(matchings.GiftersToGiftees().at("Alice Smith"), "Alice Smith"); diff --git a/test/Messenger/Emailer.cpp b/test/Messenger/Emailer.cpp index 4b1f88a..2184e4c 100644 --- a/test/Messenger/Emailer.cpp +++ b/test/Messenger/Emailer.cpp @@ -30,8 +30,8 @@ namespace SecretSanta::Messenger { namespace { TEST(MessengerEmailer, ComposeFullMessageBody) { - const Participant gifter = CreateSampleParticipantA(); - const Participant giftee = CreateSampleParticipantB(); + const Participant gifter{CreateSampleParticipantA()}; + const Participant giftee{CreateSampleParticipantB()}; const std::string main_message_body{ "You are receiving this message because you opted to participate in a Secret Santa gift " @@ -46,7 +46,7 @@ TEST(MessengerEmailer, ComposeFullMessageBody) { } TEST(MessengerEmailer, ComposeCommand) { - const Participant gifter = CreateSampleParticipantA(); + const Participant gifter{CreateSampleParticipantA()}; const std::string message_subject{"My Message Subject"}; const std::string message_body{"My Message Body"}; diff --git a/test/Participant.cpp b/test/Participant.cpp index 3aa19ab..f27cf68 100644 --- a/test/Participant.cpp +++ b/test/Participant.cpp @@ -60,7 +60,7 @@ TEST(Participant, ConstructorFromYamlNode) { TEST(Participant, CopyAssignmentOperator) { const Participant first{CreateSampleParticipantA()}; - Participant second = CreateSampleParticipantB(); + Participant second = Participant{CreateSampleParticipantB()}; second = first; EXPECT_EQ(second, first); } @@ -88,7 +88,7 @@ TEST(Participant, Hash) { TEST(Participant, MoveAssignmentOperator) { Participant first{CreateSampleParticipantA()}; - Participant second = CreateSampleParticipantB(); + Participant second{CreateSampleParticipantB()}; second = std::move(first); EXPECT_EQ(second, Participant(CreateSampleParticipantA())); } @@ -117,7 +117,7 @@ TEST(Participant, Stream) { TEST(Participant, YAML) { const Participant participant{CreateSampleParticipantA()}; - const YAML::Node node = participant.YAML(); + const YAML::Node node{participant.YAML()}; ASSERT_TRUE(node.IsMap()); ASSERT_EQ(node.size(), 1); for (const YAML::detail::iterator_value& element : node) {