From d0614b9b36248164435ff8f0b56bb0ea1fe2fa12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 22 Jun 2018 22:51:03 +0200 Subject: [PATCH] varScope: Fix false negatives for references --- lib/checkother.cpp | 2 +- test/testother.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 8104a7b00..6e144ff64 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1097,7 +1097,7 @@ void CheckOther::checkVariableScope() const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); for (const Variable* var : symbolDatabase->variableList()) { - if (!var || !var->isLocal() || (!var->isPointer() && var->valueType()->type <= ValueType::Type::RECORD)) + if (!var || !var->isLocal() || (!var->isPointer() && !var->isReference() && var->valueType()->type <= ValueType::Type::RECORD)) continue; if (var->isConst()) diff --git a/test/testother.cpp b/test/testother.cpp index c13e64163..83babd60b 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -82,6 +82,7 @@ private: TEST_CASE(varScope21); // Ticket #5382 TEST_CASE(varScope22); // Ticket #5684 TEST_CASE(varScope23); // Ticket #6154 + TEST_CASE(varScope24); // pointer / reference TEST_CASE(oldStylePointerCast); TEST_CASE(invalidPointerCast); @@ -1140,6 +1141,24 @@ private: ASSERT_EQUALS("", errout.str()); } + void varScope24() { + check("void f(Foo x) {\n" + " Foo &r = x;\n" + " if (cond) {\n" + " r.dostuff();\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'r' can be reduced.\n", errout.str()); + + check("void f(Foo x) {\n" + " Foo foo = x;\n" + " if (cond) {\n" + " foo.dostuff();\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void checkOldStylePointerCast(const char code[]) { // Clear the error buffer.. errout.str("");