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.

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