diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 814b8edd9..0aa218a23 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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; diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 7bac14db0..950268f93 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -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"