Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12519 FP variableScope with variable in macro #6131

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading