File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1536,6 +1536,7 @@ void CheckConditionImpl::alwaysTrueFalse()
15361536 }
15371537 if (!tok->hasKnownIntValue ())
15381538 continue ;
1539+ const bool isZeroOrOne = (tok->getKnownIntValue () & ~MathLib::bigint (1 )) == 0 ;
15391540 const Token* condition = nullptr ;
15401541 {
15411542 // is this a condition..
@@ -1575,8 +1576,12 @@ void CheckConditionImpl::alwaysTrueFalse()
15751576 continue ;
15761577 if (Token::simpleMatch (tok->astParent (), " return" ) && Token::Match (tok, " .|%var%" ))
15771578 continue ;
1578- if (Token::Match (tok, " %num%|%bool%|%char%" ))
1579- continue ;
1579+ bool warnForNumber = false ;
1580+ if (Token::Match (tok, " %num%|%bool%|%char%" )) {
1581+ warnForNumber = !isZeroOrOne && tok->tokType () == Token::eNumber && tok->astParent () == condition->astParent ();
1582+ if (!warnForNumber)
1583+ continue ;
1584+ }
15801585 if (Token::Match (tok, " ! %num%|%bool%|%char%" ))
15811586 continue ;
15821587 if (Token::Match (tok, " %oror%|&&" )) {
@@ -1603,7 +1608,7 @@ void CheckConditionImpl::alwaysTrueFalse()
16031608 true ))
16041609 continue ;
16051610
1606- if (!pedantic && isConstVarExpression (tok, [](const Token* tok) {
1611+ if (!pedantic && !warnForNumber && isConstVarExpression (tok, [](const Token* tok) {
16071612 return Token::Match (tok, " [|(|&|+|-|*|/|%|^|>>|<<" ) && !Token::simpleMatch (tok, " ( )" );
16081613 }))
16091614 continue ;
Original file line number Diff line number Diff line change @@ -4935,6 +4935,20 @@ class TestCondition : public TestFixture {
49354935 " if (v > 0) {}\n "
49364936 " }\n " );
49374937 ASSERT_EQUALS (" " , errout_str ());
4938+
4939+ check (" void f() {\n "
4940+ " if (42) {}\n "
4941+ " if (42U) {}\n "
4942+ " if (42L) {}\n "
4943+ " if (42UL) {}\n "
4944+ " if (42LL) {}\n "
4945+ " }\n " );
4946+ ASSERT_EQUALS (" [test.cpp:2:9]: (style) Condition '42' is always true [knownConditionTrueFalse]\n "
4947+ " [test.cpp:3:9]: (style) Condition '42U' is always true [knownConditionTrueFalse]\n "
4948+ " [test.cpp:4:9]: (style) Condition '42L' is always true [knownConditionTrueFalse]\n "
4949+ " [test.cpp:5:9]: (style) Condition '42UL' is always true [knownConditionTrueFalse]\n "
4950+ " [test.cpp:6:9]: (style) Condition '42LL' is always true [knownConditionTrueFalse]\n " ,
4951+ errout_str ());
49384952 }
49394953
49404954 void alwaysTrueSymbolic ()
You can’t perform that action at this time.
0 commit comments