From d13f7945c273335975c87e4a8060371743884aca Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 21 Aug 2023 16:13:28 -0500 Subject: [PATCH 1/2] Fix 11887: FP knownPointerToBool with const_cast --- lib/astutils.cpp | 2 +- lib/checkcondition.cpp | 5 ++++- test/testcondition.cpp | 6 ++++++ test/testother.cpp | 6 ++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 4c7ad909834..0af9aaf6745 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -743,7 +743,7 @@ 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 715ec55afea..6d91ac430c8 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1468,8 +1468,11 @@ 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 34abf929b79..e3207495671 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -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(E::E1) == 1);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void alwaysTrueSymbolic() diff --git a/test/testother.cpp b/test/testother.cpp index b8c48f797a9..ecf9d983775 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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(x));\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + check("struct A { bool x; };\n" "bool f(A* a) {\n" " if (a) {\n" From 3893920c765bb4fa82d9b25dff689d07debcbee8 Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 21 Aug 2023 16:25:03 -0500 Subject: [PATCH 2/2] Format --- lib/astutils.cpp | 3 ++- lib/checkcondition.cpp | 5 +++-- test/testcondition.cpp | 10 +++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 0af9aaf6745..7a1bf4e283d 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->astParent()->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 6d91ac430c8..226e6c993a5 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1468,8 +1468,9 @@ 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()) { - // 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 ("))) { + // 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; } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index e3207495671..9db2ab90704 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4527,11 +4527,11 @@ 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(E::E1) == 1);\n" - "}\n"); + + check("enum E { E0, E1 };\n" + "void f() {\n" + " static_assert(static_cast(E::E1) == 1);\n" + "}\n"); ASSERT_EQUALS("", errout.str()); }