diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 4c7ad9098..7a1bf4e28 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -743,7 +743,8 @@ std::vector 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(); diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 715ec55af..226e6c993 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -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 ("))) { + tok = tok->link(); continue; + } if (!tok->hasKnownIntValue()) continue; if (Token::Match(tok->previous(), "%name% (") && tok->previous()->function()) { diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 34abf929b..9db2ab907 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4527,6 +4527,12 @@ private: 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(E::E1) == 1);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void alwaysTrueSymbolic() diff --git a/test/testother.cpp b/test/testother.cpp index b8c48f797..ecf9d9837 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -11362,6 +11362,12 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("void f() {\n" + " const int* x = nullptr;\n" + " std::empty(const_cast(x));\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + check("struct A { bool x; };\n" "bool f(A* a) {\n" " if (a) {\n"