From cd010725d31df3cc68f9cac329459c5aa818f55b Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 21 Mar 2024 12:17:15 +0100 Subject: [PATCH] Fix #12519 FP variableScope with variable in macro (#6131) --- lib/checkother.cpp | 3 +++ test/testother.cpp | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 1995519e66e..beff6996668 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -944,6 +944,9 @@ void CheckOther::checkVariableScope() if (!var || !var->isLocal() || var->isConst()) continue; + if (var->nameToken()->isExpandedMacro()) + continue; + const bool isPtrOrRef = var->isPointer() || var->isReference(); const bool isSimpleType = var->typeStartToken()->isStandardType() || var->typeStartToken()->isEnumType() || (var->typeStartToken()->isC() && var->type() && var->type()->isStructType()); if (!isPtrOrRef && !isSimpleType && !astIsContainer(var->nameToken())) diff --git a/test/testother.cpp b/test/testother.cpp index 8ad022906c3..03e349d8a48 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -100,6 +100,7 @@ class TestOther : public TestFixture { TEST_CASE(varScope35); TEST_CASE(varScope36); // #12158 TEST_CASE(varScope37); // #12158 + TEST_CASE(varScope38); TEST_CASE(oldStylePointerCast); TEST_CASE(invalidPointerCast); @@ -1688,6 +1689,16 @@ class TestOther : public TestFixture { ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'i' can be reduced.\n", errout_str()); } + void varScope38() { + checkP("bool dostuff();\n" // #12519 + "#define DOSTUFF(c) if (c < 5) { if (c) b = dostuff(); }\n" + "#define DOSTUFFEX(c) { bool b = false; DOSTUFF(c); }\n" + "void f(int a) {\n" + " DOSTUFFEX(a);\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + } + #define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__) void checkOldStylePointerCast_(const char code[], const char* file, int line) {