From 0e147502cc218ebbdd573ebc0e5d871dd7edbd67 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 21 Mar 2022 16:05:38 +0100 Subject: [PATCH] Fix #10888 FN variableScope with enum and struct (#3920) * Fix #10888 FN variableScope with enum and struct * Scope reduction * Scope reduction --- lib/checkother.cpp | 6 ++++-- lib/valueflow.cpp | 7 ++++--- test/testother.cpp | 27 +++++++++++++++++++++++++++ tools/triage/mainwindow.cpp | 3 +-- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 3d7826652..7b59a9aa9 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -914,10 +914,12 @@ void CheckOther::checkVariableScope() return; for (const Variable* var : symbolDatabase->variableList()) { - if (!var || !var->isLocal() || (!var->isPointer() && !var->isReference() && !var->typeStartToken()->isStandardType())) + if (!var || !var->isLocal() || var->isConst()) continue; - if (var->isConst()) + const bool isPtrOrRef = var->isPointer() || var->isReference(); + const bool isSimpleType = var->typeStartToken()->isStandardType() || var->typeStartToken()->isEnumType() || (mTokenizer->isC() && var->type() && var->type()->isStructType()); + if (!isPtrOrRef && !isSimpleType && !astIsContainer(var->nameToken())) continue; if (mTokenizer->hasIfdef(var->nameToken(), var->scope()->bodyEnd)) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 286d82725..8329d0116 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1737,10 +1737,11 @@ static const std::string& invertAssign(const std::string& assign) {"<<=", ">>="}, {">>=", "<<="}, {"^=", "^="}}; - static std::string empty; auto it = lookup.find(assign); - if (it == lookup.end()) + if (it == lookup.end()) { + static std::string empty; return empty; + } else return it->second; } @@ -2988,8 +2989,8 @@ std::string lifetimeMessage(const Token *tok, const ValueFlow::Value *val, Error if (!classVar) errorPath.emplace_back(vartok, "Variable created here."); const Variable * var = vartok->variable(); - std::string submessage; if (var) { + std::string submessage; switch (val->lifetimeKind) { case ValueFlow::Value::LifetimeKind::SubObject: case ValueFlow::Value::LifetimeKind::Object: diff --git a/test/testother.cpp b/test/testother.cpp index 59e47130f..646c5cef4 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -96,6 +96,7 @@ private: TEST_CASE(varScope26); // range for loop, map TEST_CASE(varScope27); // #7733 - #if TEST_CASE(varScope28); // #10527 + TEST_CASE(varScope29); // #10888 TEST_CASE(oldStylePointerCast); TEST_CASE(invalidPointerCast); @@ -1320,6 +1321,32 @@ private: ASSERT_EQUALS("", errout.str()); } + void varScope29() { // #10888 + check("enum E { E0 };\n" + "struct S { int i; };\n" + "void f(int b) {\n" + " enum E e;\n" + " struct S s;\n" + " if (b) {\n" + " e = E0;\n" + " s.i = 0;\n" + " g(e, s);\n" + " }\n" + "}\n", "test.c"); + ASSERT_EQUALS("[test.c:4]: (style) The scope of the variable 'e' can be reduced.\n" + "[test.c:5]: (style) The scope of the variable 's' can be reduced.\n", + errout.str()); + + check("void f(bool b) {\n" + " std::string s;\n" + " if (b) {\n" + " s = \"abc\";\n" + " g(s);\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 's' can be reduced.\n", errout.str()); + } + #define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__) void checkOldStylePointerCast_(const char code[], const char* file, int line) { // Clear the error buffer.. diff --git a/tools/triage/mainwindow.cpp b/tools/triage/mainwindow.cpp index 64093b2ef..dcb2d577d 100644 --- a/tools/triage/mainwindow.cpp +++ b/tools/triage/mainwindow.cpp @@ -341,12 +341,11 @@ void MainWindow::findInFilesClicked() QFile file(fileName); if (file.open(QIODevice::ReadOnly)) { - QString line; int lineN = 0; QTextStream in(&file); while (!in.atEnd()) { ++lineN; - line = in.readLine(); + QString line = in.readLine(); if (line.contains(text, Qt::CaseInsensitive)) { ui->inFilesResult->addItem(fileName.mid(common_path_len) + QString{":"} + QString::number(lineN)); }