Fix #10888 FN variableScope with enum and struct (#3920)

* Fix #10888 FN variableScope with enum and struct

* Scope reduction

* Scope reduction
This commit is contained in:
chrchr-github 2022-03-21 16:05:38 +01:00 committed by GitHub
parent 9d4fb16d7d
commit 0e147502cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 7 deletions

View File

@ -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))

View File

@ -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:

View File

@ -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..

View File

@ -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));
}