Skip to content

Commit 0e14750

Browse files
Fix #10888 FN variableScope with enum and struct (#3920)
* Fix #10888 FN variableScope with enum and struct * Scope reduction * Scope reduction
1 parent 9d4fb16 commit 0e14750

4 files changed

Lines changed: 36 additions & 7 deletions

File tree

lib/checkother.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,10 +914,12 @@ void CheckOther::checkVariableScope()
914914
return;
915915

916916
for (const Variable* var : symbolDatabase->variableList()) {
917-
if (!var || !var->isLocal() || (!var->isPointer() && !var->isReference() && !var->typeStartToken()->isStandardType()))
917+
if (!var || !var->isLocal() || var->isConst())
918918
continue;
919919

920-
if (var->isConst())
920+
const bool isPtrOrRef = var->isPointer() || var->isReference();
921+
const bool isSimpleType = var->typeStartToken()->isStandardType() || var->typeStartToken()->isEnumType() || (mTokenizer->isC() && var->type() && var->type()->isStructType());
922+
if (!isPtrOrRef && !isSimpleType && !astIsContainer(var->nameToken()))
921923
continue;
922924

923925
if (mTokenizer->hasIfdef(var->nameToken(), var->scope()->bodyEnd))

lib/valueflow.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,10 +1737,11 @@ static const std::string& invertAssign(const std::string& assign)
17371737
{"<<=", ">>="},
17381738
{">>=", "<<="},
17391739
{"^=", "^="}};
1740-
static std::string empty;
17411740
auto it = lookup.find(assign);
1742-
if (it == lookup.end())
1741+
if (it == lookup.end()) {
1742+
static std::string empty;
17431743
return empty;
1744+
}
17441745
else
17451746
return it->second;
17461747
}
@@ -2988,8 +2989,8 @@ std::string lifetimeMessage(const Token *tok, const ValueFlow::Value *val, Error
29882989
if (!classVar)
29892990
errorPath.emplace_back(vartok, "Variable created here.");
29902991
const Variable * var = vartok->variable();
2991-
std::string submessage;
29922992
if (var) {
2993+
std::string submessage;
29932994
switch (val->lifetimeKind) {
29942995
case ValueFlow::Value::LifetimeKind::SubObject:
29952996
case ValueFlow::Value::LifetimeKind::Object:

test/testother.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class TestOther : public TestFixture {
9696
TEST_CASE(varScope26); // range for loop, map
9797
TEST_CASE(varScope27); // #7733 - #if
9898
TEST_CASE(varScope28); // #10527
99+
TEST_CASE(varScope29); // #10888
99100

100101
TEST_CASE(oldStylePointerCast);
101102
TEST_CASE(invalidPointerCast);
@@ -1320,6 +1321,32 @@ class TestOther : public TestFixture {
13201321
ASSERT_EQUALS("", errout.str());
13211322
}
13221323

1324+
void varScope29() { // #10888
1325+
check("enum E { E0 };\n"
1326+
"struct S { int i; };\n"
1327+
"void f(int b) {\n"
1328+
" enum E e;\n"
1329+
" struct S s;\n"
1330+
" if (b) {\n"
1331+
" e = E0;\n"
1332+
" s.i = 0;\n"
1333+
" g(e, s);\n"
1334+
" }\n"
1335+
"}\n", "test.c");
1336+
ASSERT_EQUALS("[test.c:4]: (style) The scope of the variable 'e' can be reduced.\n"
1337+
"[test.c:5]: (style) The scope of the variable 's' can be reduced.\n",
1338+
errout.str());
1339+
1340+
check("void f(bool b) {\n"
1341+
" std::string s;\n"
1342+
" if (b) {\n"
1343+
" s = \"abc\";\n"
1344+
" g(s);\n"
1345+
" }\n"
1346+
"}");
1347+
ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 's' can be reduced.\n", errout.str());
1348+
}
1349+
13231350
#define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__)
13241351
void checkOldStylePointerCast_(const char code[], const char* file, int line) {
13251352
// Clear the error buffer..

tools/triage/mainwindow.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,11 @@ void MainWindow::findInFilesClicked()
341341

342342
QFile file(fileName);
343343
if (file.open(QIODevice::ReadOnly)) {
344-
QString line;
345344
int lineN = 0;
346345
QTextStream in(&file);
347346
while (!in.atEnd()) {
348347
++lineN;
349-
line = in.readLine();
348+
QString line = in.readLine();
350349
if (line.contains(text, Qt::CaseInsensitive)) {
351350
ui->inFilesResult->addItem(fileName.mid(common_path_len) + QString{":"} + QString::number(lineN));
352351
}

0 commit comments

Comments
 (0)