From 4943771e41f76edc64b273c6b257ecc0ba778a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 19 Oct 2019 21:41:50 +0200 Subject: [PATCH] Fix #9262 (False positive on variable assignment when putting enum in namespace) --- lib/astutils.cpp | 4 ++-- test/testother.cpp | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index e27ab3fd5..efaff72a4 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1827,8 +1827,8 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const } } - if (Token::simpleMatch(tok, ") {")) { - if (Token::simpleMatch(tok->link()->previous(), "switch (")) + if (Token::Match(tok, ")|do {")) { + if (tok->str() == ")" && Token::simpleMatch(tok->link()->previous(), "switch (")) // TODO: parse switch return Result(Result::Type::BAILOUT); const Result &result1 = checkRecursive(expr, tok->tokAt(2), tok->linkAt(1), exprVarIds, local, inInnerClass); diff --git a/test/testother.cpp b/test/testother.cpp index 73ee0d56c..fc7f5d484 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -169,7 +169,7 @@ private: TEST_CASE(redundantVarAssignment_7133); TEST_CASE(redundantVarAssignment_stackoverflow); TEST_CASE(redundantVarAssignment_lambda); - TEST_CASE(redundantVarAssignment_for); + TEST_CASE(redundantVarAssignment_loop); TEST_CASE(redundantVarAssignment_after_switch); TEST_CASE(redundantVarAssignment_pointer); TEST_CASE(redundantVarAssignment_pointer_parameter); @@ -4586,7 +4586,9 @@ private: " const bool y = a.f(A::C);\n" " if(!x && !y) return;\n" "}\n"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:8]: (style) Argument 'A::B' to function f is always 0\n" + "[test.cpp:9]: (style) Argument 'A::C' to function f is always 1\n", + errout.str()); check("void foo() { \n" " const bool x = a.f(A::B);\n" @@ -6575,7 +6577,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void redundantVarAssignment_for() { + void redundantVarAssignment_loop() { check("void f() {\n" " char buf[10];\n" " int i;\n" @@ -6584,6 +6586,16 @@ private: " buf[i] = 0;\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void bar() {\n" // #9262 do-while with break + " int x = 0;\n" + " x = 432;\n" + " do {\n" + " if (foo()) break;\n" + " x = 1;\n" + " } while (false);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void redundantVarAssignment_after_switch() {