Fix #10527 FP variableScope for if with init-statement (#3811)

This commit is contained in:
chrchr-github 2022-02-08 16:12:09 +01:00 committed by GitHub
parent 966dbb1990
commit e64ea20089
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 3 deletions

View File

@ -1066,8 +1066,13 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
if (tok->varId() == var->declarationId()) {
used = true;
if (scope->type == Scope::eSwitch && scope == tok->scope())
return false; // Used in outer switch scope - unsafe or impossible to reduce scope
if (scope == tok->scope()) {
if (scope->type == Scope::eSwitch)
return false; // Used in outer switch scope - unsafe or impossible to reduce scope
if (scope->bodyStart && scope->bodyStart->isSimplifiedScope())
return false; // simplified if/for/switch init statement
}
}
}

View File

@ -651,6 +651,13 @@ public:
setFlag(fIsTemplate, b);
}
bool isSimplifiedScope() const {
return getFlag(fIsSimplifedScope);
}
void isSimplifiedScope(bool b) {
setFlag(fIsSimplifedScope, b);
}
bool isBitfield() const {
return mImpl->mBits > 0;
}
@ -1254,7 +1261,8 @@ private:
fIsSplitVarDeclEq = (1 << 30), // set to true when variable declaration with initialization is split up ('int a=5;' => 'int a; a=5;')
fIsImplicitInt = (1U << 31), // Is "int" token implicitly added?
fIsInline = (1ULL << 32), // Is this a inline type
fIsTemplate = (1ULL << 33)
fIsTemplate = (1ULL << 33),
fIsSimplifedScope = (1ULL << 34), // scope added when simplifying e.g. if (int i = ...; ...)
};
Token::Type mTokType;

View File

@ -8833,6 +8833,7 @@ void Tokenizer::simplifyIfSwitchForInit()
tok->str("{");
endscope->insertToken("}");
Token::createMutualLinks(tok, endscope->next());
tok->isSimplifiedScope(true);
}
}

View File

@ -96,6 +96,7 @@ private:
TEST_CASE(varScope25); // time_t
TEST_CASE(varScope26); // range for loop, map
TEST_CASE(varScope27); // #7733 - #if
TEST_CASE(varScope28); // #10527
TEST_CASE(oldStylePointerCast);
TEST_CASE(invalidPointerCast);
@ -1310,6 +1311,14 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (style) The scope of the variable 'x' can be reduced.\n", errout.str());
}
void varScope28() {
check("void f() {\n" // #10527
" int i{};\n"
" if (double d = g(i); d == 1.0) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
#define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__)
void checkOldStylePointerCast_(const char code[], const char* file, int line) {
// Clear the error buffer..