parent
7cc45fb393
commit
b8ba0ae00e
|
@ -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;
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue