Skip to content

Commit

Permalink
Fix #12519 FP variableScope with variable in macro
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 15, 2024
1 parent 7b63279 commit 482909f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
11 changes: 11 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 482909f

Please sign in to comment.