Skip literals for always true/false (#1304)

This commit is contained in:
Paul Fultz II 2018-07-12 16:06:47 -05:00 committed by Daniel Marjamäki
parent 5cc8da2db4
commit 42f075c3fa
2 changed files with 22 additions and 1 deletions

View File

@ -1194,7 +1194,9 @@ void CheckCondition::alwaysTrueFalse()
continue;
if (!tok->hasKnownIntValue())
continue;
if (Token::Match(tok, "[01]"))
if (Token::Match(tok, "%num%|%bool%"))
continue;
if (Token::Match(tok, "! %num%|%bool%"))
continue;
const bool constIfWhileExpression =

View File

@ -2395,6 +2395,25 @@ private:
"[test.cpp:4]: (style) Condition '1&&'c'' is always true\n"
"[test.cpp:4]: (style) Condition ''c'' is always true\n"
"[test.cpp:5]: (style) Condition ''d'' is always true\n", errout.str());
// Skip literals
check("void f() { if(true) {} }");
ASSERT_EQUALS("", errout.str());
check("void f() { if(false) {} }");
ASSERT_EQUALS("", errout.str());
check("void f() { if(!true) {} }");
ASSERT_EQUALS("", errout.str());
check("void f() { if(!false) {} }");
ASSERT_EQUALS("", errout.str());
check("void f() { if(0) {} }");
ASSERT_EQUALS("", errout.str());
check("void f() { if(1) {} }");
ASSERT_EQUALS("", errout.str());
}
void checkInvalidTestForOverflow() {