Fix #10877 FP constStatement with ternary and comma operator (#3905)

This commit is contained in:
chrchr-github 2022-03-16 15:28:59 +01:00 committed by GitHub
parent 7cc45fb393
commit b8ba0ae00e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View File

@ -1763,8 +1763,10 @@ static bool isConstStatement(const Token *tok, bool cpp)
return isWithoutSideEffects(cpp, tok->astOperand1()) && isConstStatement(tok->astOperand1(), cpp);
if (Token::Match(tok, "( %type%"))
return isConstStatement(tok->astOperand1(), cpp);
if (Token::Match(tok, ",|."))
if (Token::simpleMatch(tok, "."))
return isConstStatement(tok->astOperand2(), cpp);
if (Token::simpleMatch(tok, ",")) // warn about const statement on rhs at the top level
return tok->astParent() ? isConstStatement(tok->astOperand1(), cpp) && isConstStatement(tok->astOperand2(), cpp) : isConstStatement(tok->astOperand2(), cpp);
if (Token::simpleMatch(tok, "?") && Token::simpleMatch(tok->astOperand2(), ":")) // ternary operator
return isConstStatement(tok->astOperand1(), cpp) && isConstStatement(tok->astOperand2()->astOperand1(), cpp) && isConstStatement(tok->astOperand2()->astOperand2(), cpp);
return false;

View File

@ -420,6 +420,12 @@ private:
check("void f(bool b) { b ? true : false; }\n"); // #10865
ASSERT_EQUALS("[test.cpp:1]: (warning) Redundant code: Found unused result of ternary operator.\n", errout.str());
check("struct S { void (*f)() = nullptr; };\n" // #10877
"void g(S* s) {\n"
" (s->f == nullptr) ? nullptr : (s->f(), nullptr);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(bool b) {\n"
" g() ? true : false;\n"
" true ? g() : false;\n"