Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3814,7 +3814,7 @@ bool isUnreachableOperand(const Token *tok)
// ternary
if (Token::simpleMatch(parent, ":") && Token::simpleMatch(parent->astParent(), "?")) {
const Token *condTok = parent->astParent()->astOperand1();
if (condTok->hasKnownIntValue() && static_cast<bool>(condTok->getKnownIntValue()) != left)
if (condTok->hasKnownIntValue() && ((condTok->getKnownIntValue() != 0) != left))
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/ctu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ std::string CTU::FileInfo::FunctionCall::toXmlString() const
out << "<function-call"
<< toBaseXmlString()
<< " " << ATTR_CALL_ARGEXPR << "=\"" << ErrorLogger::toxml(callArgumentExpression) << "\""
<< " " << ATTR_CALL_ARGVALUETYPE << "=\"" << static_cast<int>(callValueType) << "\""
<< " " << ATTR_CALL_ARGVALUETYPE << "=\"" << static_cast<unsigned>(callValueType) << "\""
<< " " << ATTR_CALL_ARGVALUE << "=\"" << callArgValue.value << "\""
<< " " << ATTR_CALL_UNKNOWN_FUNCTION_RETURN << "=\"" << static_cast<int>(callArgValue.unknownFunctionReturn) << "\"";
<< " " << ATTR_CALL_UNKNOWN_FUNCTION_RETURN << "=\"" << static_cast<unsigned>(callArgValue.unknownFunctionReturn) << "\"";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it important to use unsigned instead of int.

I am not against this and would likely accept such unsigned use if this was new code. But if you refactor you need to have some important reasons. We could get a commit battle where somebody else thinks that it should be plain int and send me a PR about that. I see no clear reasons to use neither int nor unsigned here.

Using unsigned just because the value is always positive is not a good reason in my opinion. That is dangerous approach and leads to conversion problems.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The underlying type of the enum is unsigned and we were casting it to a signed type. But it is uint8_t which will be treated like char in operator<< so we need to cast it to something not handled as a number. While int has a wide enough range even if we might move it up to a 16-bit type it would be cleaner to leave the signedness intact.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe unsigned integers should be avoided.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So numbers above 2^63-1 do not exist?

Copy link
Owner

@danmar danmar Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to care about that problem here.

  • if you do need such numbers then feel free to use unsigned.
  • if you need to perform bitoperations that might involve the "sign" bit then please use unsigned.

but in other cases, in general I feel that "unsigned" should just be avoided. If something can be signed then let's make it signed.

I agree with the Google C++ style guide says about unsigned integers:
https://google.github.io/styleguide/cppguide.html#Integer_Types

the section ends with:

... try not to mix signedness, and try to avoid unsigned types (except for representing bitfields or modular arithmetic). Do not use an unsigned type merely to assert that a variable is non-negative.

The enum has type uint8_t it could be int8_t instead. but as far as I see it makes no technical difference as we know the values. So I don't care a lot in this case.

if (warning)
out << " " << ATTR_WARNING << "=\"true\"";
if (callValuePath.empty())
Expand Down
2 changes: 1 addition & 1 deletion lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ nonneg int Token::getStrArraySize(const Token *tok)
// cppcheck-suppress shadowFunction - TODO: fix this
const std::string str(getStringLiteral(tok->str()));
int sizeofstring = 1;
for (int i = 0; i < static_cast<int>(str.size()); i++) {
for (size_t i = 0; i < str.size(); i++) {
if (str[i] == '\\')
++i;
++sizeofstring;
Expand Down
4 changes: 2 additions & 2 deletions test/fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ TestFixture::SettingsBuilder& TestFixture::SettingsBuilder::library(const char l
// TODO: exename is not yet set
const Library::ErrorCode lib_error = settings.library.load(fixture.exename.c_str(), lib).errorcode;
if (lib_error != Library::ErrorCode::OK)
throw std::runtime_error("loading library '" + std::string(lib) + "' failed - " + std::to_string(static_cast<int>(lib_error)));
throw std::runtime_error("loading library '" + std::string(lib) + "' failed - " + std::to_string(static_cast<unsigned>(lib_error)));
// strip extension
std::string lib_s(lib);
const std::string ext(".cfg");
Expand Down Expand Up @@ -520,6 +520,6 @@ TestFixture::SettingsBuilder& TestFixture::SettingsBuilder::libraryxml(const cha
throw std::runtime_error(std::string("loading library XML data failed - ") + tinyxml2::XMLDocument::ErrorIDToName(xml_error));
const Library::ErrorCode lib_error = LibraryHelper::loadxmldoc(settings.library, doc).errorcode;
if (lib_error != Library::ErrorCode::OK)
throw std::runtime_error("loading library XML failed - " + std::to_string(static_cast<int>(lib_error)));
throw std::runtime_error("loading library XML failed - " + std::to_string(static_cast<unsigned>(lib_error)));
return *this;
}
4 changes: 2 additions & 2 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1973,7 +1973,7 @@ class TestCmdlineParser : public TestFixture {
void suppressionTwo() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--suppress=uninitvar,noConstructor", "file.cpp"};
TODO_ASSERT_EQUALS(static_cast<int>(CmdLineParser::Result::Success), static_cast<int>(CmdLineParser::Result::Fail), static_cast<int>(parseFromArgs(argv)));
TODO_ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, CmdLineParser::Result::Fail, parseFromArgs(argv));
TODO_ASSERT_EQUALS(true, false, supprs->nomsg.isSuppressed(errorMessage("uninitvar", "file.cpp", 1U)));
TODO_ASSERT_EQUALS(true, false, supprs->nomsg.isSuppressed(errorMessage("noConstructor", "file.cpp", 1U)));
TODO_ASSERT_EQUALS("", "cppcheck: error: Failed to add suppression. Invalid id \"uninitvar,noConstructor\"\n", logger->str());
Expand Down Expand Up @@ -2056,7 +2056,7 @@ class TestCmdlineParser : public TestFixture {
void templatesNoPlaceholder() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--template=selfchek", "file.cpp"};
TODO_ASSERT_EQUALS(static_cast<int>(CmdLineParser::Result::Fail), static_cast<int>(CmdLineParser::Result::Success), static_cast<int>(parseFromArgs(argv)));
TODO_ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, CmdLineParser::Result::Success, parseFromArgs(argv));
ASSERT_EQUALS("selfchek", settings->templateFormat);
ASSERT_EQUALS("", settings->templateLocation);
}
Expand Down
8 changes: 4 additions & 4 deletions test/testlibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1045,18 +1045,18 @@ class TestLibrary : public TestFixture {
}

void containerActionToFromString() const {
for (uint16_t i = 0; i < static_cast<uint16_t>(Library::Container::Action::NO_ACTION); ++i) {
for (uint8_t i = 0; i < static_cast<uint8_t>(Library::Container::Action::NO_ACTION); ++i) {
const auto a = static_cast<Library::Container::Action>(i);
const std::string& s = Library::Container::toString(a);
ASSERT_EQUALS(i, static_cast<uint16_t>(Library::Container::actionFrom(s)));
ASSERT_EQUALS_ENUM(a, Library::Container::actionFrom(s));
}
}

void containerYieldToFromString() const {
for (uint16_t i = 0; i < static_cast<uint16_t>(Library::Container::Yield::NO_YIELD); ++i) {
for (uint8_t i = 0; i < static_cast<uint8_t>(Library::Container::Yield::NO_YIELD); ++i) {
const auto y = static_cast<Library::Container::Yield>(i);
const std::string& s = Library::Container::toString(y);
ASSERT_EQUALS(i, static_cast<uint16_t>(Library::Container::yieldFrom(s)));
ASSERT_EQUALS_ENUM(y, Library::Container::yieldFrom(s));
}
}

Expand Down
Loading