From 52453947c8bbb995f095cbe6afe0ab54bfaa4ff7 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 6 Jun 2022 11:17:36 +0200 Subject: [PATCH] Don't warn for 'x | 0' in macro (#4172) * Fix #11082 FN badBitmaskCheck for binary or with 0 * Add test for #10703 * Don't warn for 'x | 0' in macro * Add test for #10876 --- lib/checkcondition.cpp | 2 +- test/testcondition.cpp | 6 ++++++ test/testincompletestatement.cpp | 10 ++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index ca7ea708f..85e60ac18 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -314,7 +314,7 @@ void CheckCondition::checkBadBitmaskCheck() const bool isNoOp = (tok->astOperand1()->hasKnownIntValue() && tok->astOperand1()->values().front().intvalue == 0) || (tok->astOperand2()->hasKnownIntValue() && tok->astOperand2()->values().front().intvalue == 0); - if (isNoOp) + if (isNoOp && !tok->isExpandedMacro()) badBitmaskCheckError(tok, isNoOp); } } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 4c11d5bdb..0af7221d7 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -893,6 +893,12 @@ private: " if (i | j) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:3]: (warning) Operator '|' with one operand equal to zero is redundant.\n", errout.str()); + + check("#define EIGHTTOIS(x) (((x) << 8) | (x))\n" + "int f() {\n" + " return EIGHTTOIS(0);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index ab9bd7deb..f595a0b42 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -646,6 +646,16 @@ private: " };\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("namespace N {\n" // #10876 + " template \n" + " inline void f() {}\n" + " template\n" + " void g(T& c) {\n" + " for (typename T::iterator v = c.begin(); v != c.end(); ++v) {}\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void vardecl() {