From 770abcf453fd9c21face3d7b28e11c42ea245767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 26 Dec 2016 14:14:40 +0100 Subject: [PATCH] Fixed #5398 (False positive: Scope of variable can be reduced does not account for other variables not reduceable) --- lib/checkother.cpp | 3 +++ test/testother.cpp | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 849b03566..37b9700f2 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1205,6 +1205,9 @@ void CheckOther::checkVariableScope() if (!tok->isNumber() && tok->tokType() != Token::eString && tok->tokType() != Token::eChar && !tok->isBoolean()) continue; } + // bailout if initialized with function call that has possible side effects + if (tok->str() == "(" && Token::simpleMatch(tok->astOperand2(), "(")) + continue; bool reduce = true; bool used = false; // Don't warn about unused variables for (; tok && tok != var->scope()->classEnd; tok = tok->next()) { diff --git a/test/testother.cpp b/test/testother.cpp index 52bc145ad..1fbca242a 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -719,6 +719,15 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + check("void f() {\n" // #5398 + " bool success = false;\n" + " int notReducable(someClass.getX(&success));\n" + " if (success) {\n" + " foo(notReducable);\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("void f()\n" "{\n" "int foo = 0;\n"