Skip to content
Merged
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
3 changes: 2 additions & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,8 @@ std::vector<ValueType> getParentValueTypes(const Token* tok, const Settings* set
return {std::move(vtParent)};
}
// The return type of a function is not the parent valuetype
if (Token::simpleMatch(tok->astParent(), "(") && ftok && !tok->isCast() && ftok->tokType() != Token::eType)
if (Token::simpleMatch(tok->astParent(), "(") && ftok && !tok->astParent()->isCast() &&
ftok->tokType() != Token::eType)
return {};
if (Token::Match(tok->astParent(), "return|(|{|%assign%") && parent) {
*parent = tok->astParent();
Expand Down
6 changes: 5 additions & 1 deletion lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1468,8 +1468,12 @@ void CheckCondition::alwaysTrueFalse()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Scope * scope : symbolDatabase->functionScopes) {
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (Token::simpleMatch(tok, "<") && tok->link()) // don't write false positives when templates are used
// don't write false positives when templates are used or inside of asserts or non-evaluated contexts
if (tok->link() && (Token::simpleMatch(tok, "<") ||
Token::Match(tok->previous(), "static_assert|assert|ASSERT|sizeof|decltype ("))) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should probably make a helper function for that. We already have isUnevaluatedOperator() and isUnevaluated() (2x) in various places.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

And there also is isSizeOfEtc().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well this is checking for unevaluated and for asserts.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Seems like our checks for asserts aren't consistent everywhere either.

tok = tok->link();
continue;
}
if (!tok->hasKnownIntValue())
continue;
if (Token::Match(tok->previous(), "%name% (") && tok->previous()->function()) {
Expand Down
6 changes: 6 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4527,6 +4527,12 @@ class TestCondition : public TestFixture {
ASSERT_EQUALS("[test.cpp:5]: (style) Condition 'i==7' is always false\n"
"[test.cpp:6]: (style) Condition 'p==nullptr' is always false\n",
errout.str());

check("enum E { E0, E1 };\n"
"void f() {\n"
" static_assert(static_cast<int>(E::E1) == 1);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void alwaysTrueSymbolic()
Expand Down
6 changes: 6 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11362,6 +11362,12 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("", errout.str());

check("void f() {\n"
" const int* x = nullptr;\n"
" std::empty(const_cast<int*>(x));\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("struct A { bool x; };\n"
"bool f(A* a) {\n"
" if (a) {\n"
Expand Down