* Fix #10888 FN variableScope with enum and struct * Scope reduction * Scope reduction
This commit is contained in:
parent
9d4fb16d7d
commit
0e147502cc
|
@ -914,10 +914,12 @@ void CheckOther::checkVariableScope()
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (const Variable* var : symbolDatabase->variableList()) {
|
for (const Variable* var : symbolDatabase->variableList()) {
|
||||||
if (!var || !var->isLocal() || (!var->isPointer() && !var->isReference() && !var->typeStartToken()->isStandardType()))
|
if (!var || !var->isLocal() || var->isConst())
|
||||||
continue;
|
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;
|
continue;
|
||||||
|
|
||||||
if (mTokenizer->hasIfdef(var->nameToken(), var->scope()->bodyEnd))
|
if (mTokenizer->hasIfdef(var->nameToken(), var->scope()->bodyEnd))
|
||||||
|
|
|
@ -1737,10 +1737,11 @@ static const std::string& invertAssign(const std::string& assign)
|
||||||
{"<<=", ">>="},
|
{"<<=", ">>="},
|
||||||
{">>=", "<<="},
|
{">>=", "<<="},
|
||||||
{"^=", "^="}};
|
{"^=", "^="}};
|
||||||
static std::string empty;
|
|
||||||
auto it = lookup.find(assign);
|
auto it = lookup.find(assign);
|
||||||
if (it == lookup.end())
|
if (it == lookup.end()) {
|
||||||
|
static std::string empty;
|
||||||
return empty;
|
return empty;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
@ -2988,8 +2989,8 @@ std::string lifetimeMessage(const Token *tok, const ValueFlow::Value *val, Error
|
||||||
if (!classVar)
|
if (!classVar)
|
||||||
errorPath.emplace_back(vartok, "Variable created here.");
|
errorPath.emplace_back(vartok, "Variable created here.");
|
||||||
const Variable * var = vartok->variable();
|
const Variable * var = vartok->variable();
|
||||||
std::string submessage;
|
|
||||||
if (var) {
|
if (var) {
|
||||||
|
std::string submessage;
|
||||||
switch (val->lifetimeKind) {
|
switch (val->lifetimeKind) {
|
||||||
case ValueFlow::Value::LifetimeKind::SubObject:
|
case ValueFlow::Value::LifetimeKind::SubObject:
|
||||||
case ValueFlow::Value::LifetimeKind::Object:
|
case ValueFlow::Value::LifetimeKind::Object:
|
||||||
|
|
|
@ -96,6 +96,7 @@ private:
|
||||||
TEST_CASE(varScope26); // range for loop, map
|
TEST_CASE(varScope26); // range for loop, map
|
||||||
TEST_CASE(varScope27); // #7733 - #if
|
TEST_CASE(varScope27); // #7733 - #if
|
||||||
TEST_CASE(varScope28); // #10527
|
TEST_CASE(varScope28); // #10527
|
||||||
|
TEST_CASE(varScope29); // #10888
|
||||||
|
|
||||||
TEST_CASE(oldStylePointerCast);
|
TEST_CASE(oldStylePointerCast);
|
||||||
TEST_CASE(invalidPointerCast);
|
TEST_CASE(invalidPointerCast);
|
||||||
|
@ -1320,6 +1321,32 @@ private:
|
||||||
ASSERT_EQUALS("", errout.str());
|
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__)
|
#define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__)
|
||||||
void checkOldStylePointerCast_(const char code[], const char* file, int line) {
|
void checkOldStylePointerCast_(const char code[], const char* file, int line) {
|
||||||
// Clear the error buffer..
|
// Clear the error buffer..
|
||||||
|
|
|
@ -341,12 +341,11 @@ void MainWindow::findInFilesClicked()
|
||||||
|
|
||||||
QFile file(fileName);
|
QFile file(fileName);
|
||||||
if (file.open(QIODevice::ReadOnly)) {
|
if (file.open(QIODevice::ReadOnly)) {
|
||||||
QString line;
|
|
||||||
int lineN = 0;
|
int lineN = 0;
|
||||||
QTextStream in(&file);
|
QTextStream in(&file);
|
||||||
while (!in.atEnd()) {
|
while (!in.atEnd()) {
|
||||||
++lineN;
|
++lineN;
|
||||||
line = in.readLine();
|
QString line = in.readLine();
|
||||||
if (line.contains(text, Qt::CaseInsensitive)) {
|
if (line.contains(text, Qt::CaseInsensitive)) {
|
||||||
ui->inFilesResult->addItem(fileName.mid(common_path_len) + QString{":"} + QString::number(lineN));
|
ui->inFilesResult->addItem(fileName.mid(common_path_len) + QString{":"} + QString::number(lineN));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue