From 59c1c76a978b5253f1d8671b93e3d42138e7983e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 3 Mar 2017 19:07:00 +0100 Subject: [PATCH] Fixed #7466 (Reference variable does not count side-effects (FP variableScope)) --- lib/checkother.cpp | 4 +++- test/testother.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index ba0383de4..5d4eafa5e 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1193,7 +1193,7 @@ void CheckOther::checkVariableScope() if (tok->str() == "(") { forHead = true; break; - } else if (tok->str() == "{" || tok->str() == ";" || tok->str() == "}") + } else if (Token::Match(tok, "[;{}]")) break; } if (forHead) @@ -1208,6 +1208,8 @@ void CheckOther::checkVariableScope() // bailout if initialized with function call that has possible side effects if (tok->str() == "(" && Token::simpleMatch(tok->astOperand2(), "(")) continue; + if (Token::simpleMatch(tok, "=") && 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 7be95ee33..419c666aa 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -732,6 +732,13 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + check("void foo(Test &test) {\n" + " int& x = test.getData();\n" + " if (test.process())\n" + " x = 0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("void f()\n" "{\n" "int foo = 0;\n"