From 829c543331dff15c1f800aa1f4adf068bf9a8a3f Mon Sep 17 00:00:00 2001 From: Rikard Falkeborn Date: Wed, 11 Nov 2020 08:01:11 +0100 Subject: [PATCH] TestCondition: Don't warn when sizeof is involved (#2896) --- lib/checkcondition.cpp | 6 ++---- test/testcondition.cpp | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index df6a4e566..145789a23 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1472,7 +1472,6 @@ void CheckCondition::alwaysTrueFalse() // don't warn when condition checks sizeof result bool hasSizeof = false; - bool hasNonNumber = false; visitAstNodes(tok, [&](const Token * tok2) { if (!tok2) return ChildrenToVisit::none; @@ -1484,11 +1483,10 @@ void CheckCondition::alwaysTrueFalse() } if (tok2->isComparisonOp() || tok2->isArithmeticalOp()) { return ChildrenToVisit::op1_and_op2; - } else - hasNonNumber = true; + } return ChildrenToVisit::none; }); - if (!hasNonNumber && hasSizeof) + if (hasSizeof) continue; alwaysTrueFalseError(tok, &tok->values().front()); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index c105d8b7b..e5adefa77 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -700,6 +700,18 @@ private: " else if ( value & (int)Value2 ) {}\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void f(size_t x) {\n" + " if (x == sizeof(int)) {}\n" + " else { if (x == sizeof(long))} {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void f(size_t x) {\n" + " if (x == sizeof(long)) {}\n" + " else { if (x == sizeof(long long))} {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void oppositeElseIfCondition() { @@ -3002,8 +3014,8 @@ private: " if (sizeof(char) != x) {}\n" " if (x != sizeof(char)) {}\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'sizeof(char)!=x' is always true\n" - "[test.cpp:4]: (style) Condition 'x!=sizeof(char)' is always true\n", errout.str()); + TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'sizeof(char)!=x' is always true\n" + "[test.cpp:4]: (style) Condition 'x!=sizeof(char)' is always true\n", "", errout.str()); // Don't warn in assertions. Condition is often 'always true' by intention. // If platform,defines,etc cause an 'always false' assertion then that is not very dangerous neither