Skip to content

Commit

Permalink
ci: Fix cppcheck errors (#792)
Browse files Browse the repository at this point in the history
* Fix cppcheck errors

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Fix cppcheck errors

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Fix headers

Signed-off-by: Uilian Ries <uilianries@gmail.com>

---------

Signed-off-by: Uilian Ries <uilianries@gmail.com>
  • Loading branch information
uilianries committed Jul 6, 2024
1 parent e4f7f68 commit e1cbca0
Show file tree
Hide file tree
Showing 23 changed files with 94 additions and 97 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ build-linux-clang
bazel-*
MODULE.bazel.lock
install/

tests/default.profraw
defaults.cfg
14 changes: 4 additions & 10 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,16 @@ T weightedArrayElement(const std::vector<WeightedElement<T>>& data)
const std::integral auto targetWeightValue = number::integer<unsigned>(1, sumOfWeights);

unsigned currentSum = 0;

size_t currentIdx = 0;

while (currentIdx < data.size())
for (const auto& elem : data)
{
currentSum += data[currentIdx].weight;

currentSum += elem.weight;
if (currentSum >= targetWeightValue)
{
break;
return elem.value;
}

currentIdx++;
}

return data.at(currentIdx).value;
return data.back().value;
}

}
4 changes: 2 additions & 2 deletions include/faker-cxx/Person.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ enum class Language;
* faker::person::prefix(Sex::Male) // "Mr."
* @endcode
*/
FAKER_CXX_EXPORT std::string_view prefix(std::optional<Country> countryOpt = std::nullopt,
FAKER_CXX_EXPORT std::string_view prefix(std::optional<Country> country = std::nullopt,
std::optional<Sex> sex = std::nullopt);

/**
Expand All @@ -86,7 +86,7 @@ enum class Language;
* faker::person::suffix() // "Jr."
* @endcode
*/
FAKER_CXX_EXPORT std::string_view suffix(std::optional<Country> countryOpt = std::nullopt,
FAKER_CXX_EXPORT std::string_view suffix(std::optional<Country> country = std::nullopt,
std::optional<Sex> sex = std::nullopt);

/**
Expand Down
7 changes: 5 additions & 2 deletions include/faker-cxx/RandomGenerator.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <random>
#include <type_traits>

namespace faker
{
Expand All @@ -18,9 +19,11 @@ class RandomGenerator
RandomGenerator& operator=(const RandomGenerator&) = default;
RandomGenerator& operator=(RandomGenerator&&) = default;

int operator()(std::uniform_int_distribution<>& dist)
template <typename Dist>
requires std::is_invocable_r_v<int, Dist&, T&>
int operator()(Dist&& dist)
{
return dist(generator_);
return std::forward<Dist>(dist)(generator_);
}

private:
Expand Down
7 changes: 3 additions & 4 deletions src/common/AlgoHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <set>
#include <stdexcept>
#include <string>
#include <iterator>

#include "faker-cxx/Datatype.h"
#include "faker-cxx/Export.h"
Expand Down Expand Up @@ -41,11 +42,9 @@ static T::key_type objectKey(const T& object)
}

std::vector<typename T::key_type> keys;
keys.reserve(object.size());

for (const auto& entry : object)
{
keys.push_back(entry.first);
}
std::transform(object.begin(), object.end(), std::back_inserter(keys), [](const auto& entry) { return entry.first; });

return arrayElement<typename T::key_type>(keys);
}
Expand Down
10 changes: 5 additions & 5 deletions src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string>
#include <string_view>

#include "../../common/FormatHelper.h"
#include "common/FormatHelper.h"
#include "ColorData.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
Expand Down Expand Up @@ -38,13 +38,13 @@ std::string hex(HexCasing casing, HexPrefix prefix, bool includeAlpha)
return string::hexadecimal(includeAlpha ? 8 : 6, casing, prefix);
}

std::string hsl(bool include_alpha)
std::string hsl(bool includeAlpha)
{
std::integral auto hue = number::integer(360);
std::integral auto saturation = number::integer(100);
std::integral auto lightness = number::integer(100);

if (!include_alpha)
if (!includeAlpha)
{
return common::format("hsl({}, {}, {})", hue, saturation, lightness);
}
Expand All @@ -54,13 +54,13 @@ std::string hsl(bool include_alpha)
return common::format("hsla({}, {}, {}, {:.2f})", hue, saturation, lightness, alpha);
}

std::string lch(bool include_alpha)
std::string lch(bool includeAlpha)
{
std::integral auto luminance = number::integer(100);
std::integral auto chroma = number::integer(100);
std::integral auto hue = number::integer(360);

if (!include_alpha)
if (!includeAlpha)
{
return common::format("lch({}, {}, {})", luminance, chroma, hue);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/commerce/Commerce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string>
#include <string_view>

#include "../../common/FormatHelper.h"
#include "common/FormatHelper.h"
#include "CommerceData.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/String.h"
Expand Down
2 changes: 1 addition & 1 deletion src/modules/company/Company.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string>
#include <string_view>

#include "../../common/FormatHelper.h"
#include "common/FormatHelper.h"
#include "CompanyData.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
Expand Down
2 changes: 1 addition & 1 deletion src/modules/date/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <string>
#include <string_view>

#include "../../common/FormatHelper.h"
#include "common/FormatHelper.h"
#include "DateData.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
Expand Down
4 changes: 2 additions & 2 deletions src/modules/finance/Finance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <string_view>
#include <vector>

#include "../../common/AlgoHelper.h"
#include "../../common/FormatHelper.h"
#include "common/AlgoHelper.h"
#include "common/FormatHelper.h"
#include "faker-cxx/Date.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
Expand Down
4 changes: 2 additions & 2 deletions src/modules/git/Git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <optional>
#include <string>

#include "../../common/FormatHelper.h"
#include "../../common/StringHelper.h"
#include "common/FormatHelper.h"
#include "common/StringHelper.h"
#include "../date/DateData.h"
#include "faker-cxx/Date.h"
#include "faker-cxx/Internet.h"
Expand Down
2 changes: 1 addition & 1 deletion src/modules/hacker/Hacker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string>
#include <string_view>

#include "../../common/StringHelper.h"
#include "common/StringHelper.h"
#include "faker-cxx/Helper.h"
#include "HackerData.h"

Expand Down
6 changes: 3 additions & 3 deletions src/modules/helper/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <regex>
#include <string>

#include "../../common/LuhnCheck.h"
#include "../../common/StringHelper.h"
#include "../../common/AlgoHelper.h"
#include "common/LuhnCheck.h"
#include "common/StringHelper.h"
#include "common/AlgoHelper.h"
#include "faker-cxx/Number.h"

namespace faker::helper
Expand Down
2 changes: 1 addition & 1 deletion src/modules/image/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <string_view>
#include <unordered_map>

#include "../../common/FormatHelper.h"
#include "common/FormatHelper.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"

Expand Down
16 changes: 8 additions & 8 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,25 @@ std::vector<std::string_view> getAllEmojis()
return emojis;
}

std::string username(std::optional<std::string> firstNameInit, std::optional<std::string> lastNameInit, Country country)
std::string username(std::optional<std::string> firstName, std::optional<std::string> lastName, Country country)
{
const auto firstName = common::toLower(std::string{firstNameInit ? *firstNameInit : person::firstName(country)});
const auto lastName = common::toLower(std::string{lastNameInit ? *lastNameInit : person::lastName(country)});
const auto firstNameLower = common::toLower(std::string{firstName ? *firstName : person::firstName(country)});
const auto lastNameLower = common::toLower(std::string{lastName ? *lastName : person::lastName(country)});

std::string username;

switch (number::integer<int>(2))
{
case 0:
username = common::format("{}{}{}", firstName, lastName, number::integer<int>(1970, 2005));
username = common::format("{}{}{}", firstNameLower, lastNameLower, number::integer<int>(1970, 2005));
break;
case 1:
username =
common::format("{}{}{}", firstName, helper::arrayElement(std::vector<std::string>{".", "_", ""}), lastName);
common::format("{}{}{}", firstNameLower, helper::arrayElement(std::vector<std::string>{".", "_", ""}), lastNameLower);
break;
case 2:
username =
common::format("{}{}{}", lastName, helper::arrayElement(std::vector<std::string>{".", "_", ""}), firstName);
common::format("{}{}{}", lastNameLower, helper::arrayElement(std::vector<std::string>{".", "_", ""}), firstNameLower);
break;
}

Expand Down Expand Up @@ -313,9 +313,9 @@ unsigned port()

std::string url(const WebProtocol& webProtocol)
{
const auto protocol = webProtocol == WebProtocol::Https ? "https" : "http";
const auto protocolStr = webProtocol == WebProtocol::Https ? "https" : "http";

return common::format("{}://{}", protocol, domainName());
return common::format("{}://{}", protocolStr, domainName());
}

std::string domainName()
Expand Down
4 changes: 2 additions & 2 deletions src/modules/location/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <string_view>
#include <unordered_map>

#include "../../common/FormatHelper.h"
#include "../../common/AlgoHelper.h"
#include "common/FormatHelper.h"
#include "common/AlgoHelper.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
#include "faker-cxx/Person.h"
Expand Down
17 changes: 8 additions & 9 deletions src/modules/lorem/Lorem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include <string>
#include <string_view>
#include <vector>
#include <algorithm>
#include <iterator>

#include "../../common/FormatHelper.h"
#include "../../common/StringHelper.h"
#include "common/FormatHelper.h"
#include "common/StringHelper.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
#include "LoremData.h"
Expand Down Expand Up @@ -57,15 +59,12 @@ std::string sentences(unsigned minNumberOfSentences, unsigned maxNumberOfSentenc

std::string slug(unsigned int numberOfWords)
{
std::vector<std::string> words;
words.reserve(numberOfWords);
std::vector<std::string> wordList;
wordList.reserve(numberOfWords);

for (unsigned i = 0; i < numberOfWords; i++)
{
words.push_back(std::string(word()));
}
std::generate_n(std::back_inserter(wordList), numberOfWords, []{ return std::string(word());});

return common::joinString(words, "-");
return common::joinString(wordList, "-");
}

std::string paragraph(unsigned int minNumberOfSentences, unsigned int maxNumberOfSentences)
Expand Down
Loading

0 comments on commit e1cbca0

Please sign in to comment.