parent
07b802fd2a
commit
f04d47ac61
|
@ -922,12 +922,14 @@ void CheckOther::checkVariableScope()
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const Token* tok = var->nameToken()->next();
|
const Token* tok = var->nameToken()->next();
|
||||||
|
bool isConstructor = false;
|
||||||
if (Token::Match(tok, "; %varid% =", var->declarationId())) { // bailout for assignment
|
if (Token::Match(tok, "; %varid% =", var->declarationId())) { // bailout for assignment
|
||||||
tok = tok->tokAt(2)->astOperand2();
|
tok = tok->tokAt(2)->astOperand2();
|
||||||
if (!isSimpleExpr(tok, var, mSettings))
|
if (!isSimpleExpr(tok, var, mSettings))
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if (Token::Match(tok, "{|(")) { // bailout for constructor
|
else if (Token::Match(tok, "{|(")) { // bailout for constructor
|
||||||
|
isConstructor = true;
|
||||||
const Token* argTok = tok->astOperand2();
|
const Token* argTok = tok->astOperand2();
|
||||||
bool bail = false;
|
bool bail = false;
|
||||||
while (argTok) {
|
while (argTok) {
|
||||||
|
@ -946,7 +948,7 @@ void CheckOther::checkVariableScope()
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// bailout if initialized with function call that has possible side effects
|
// bailout if initialized with function call that has possible side effects
|
||||||
if (Token::Match(tok, "[(=]") && Token::simpleMatch(tok->astOperand2(), "("))
|
if (!isConstructor && Token::Match(tok, "[(=]") && Token::simpleMatch(tok->astOperand2(), "("))
|
||||||
continue;
|
continue;
|
||||||
bool reduce = true;
|
bool reduce = true;
|
||||||
bool used = false; // Don't warn about unused variables
|
bool used = false; // Don't warn about unused variables
|
||||||
|
|
|
@ -1587,9 +1587,11 @@ private:
|
||||||
"void f(const S& s) {\n"
|
"void f(const S& s) {\n"
|
||||||
" std::string str = s.getStr();\n"
|
" std::string str = s.getStr();\n"
|
||||||
" std::string str2{ s.getStr() };\n"
|
" std::string str2{ s.getStr() };\n"
|
||||||
|
" std::string str3(s.getStr());\n"
|
||||||
" if (s.getB()) {\n"
|
" if (s.getB()) {\n"
|
||||||
" if (str == \"abc\") {}\n"
|
" if (str == \"abc\") {}\n"
|
||||||
" if (str2 == \"abc\") {}\n"
|
" if (str2 == \"abc\") {}\n"
|
||||||
|
" if (str3 == \"abc\") {}\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"void f(const char* s, bool b) {\n"
|
"void f(const char* s, bool b) {\n"
|
||||||
|
@ -1606,8 +1608,9 @@ private:
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:21]: (style) The scope of the variable 'str' can be reduced.\n"
|
ASSERT_EQUALS("[test.cpp:21]: (style) The scope of the variable 'str' can be reduced.\n"
|
||||||
"[test.cpp:22]: (style) The scope of the variable 'str2' can be reduced.\n"
|
"[test.cpp:22]: (style) The scope of the variable 'str2' can be reduced.\n"
|
||||||
"[test.cpp:29]: (style) The scope of the variable 'i' can be reduced.\n"
|
"[test.cpp:23]: (style) The scope of the variable 'str3' can be reduced.\n"
|
||||||
"[test.cpp:35]: (style) The scope of the variable 'k' can be reduced.\n",
|
"[test.cpp:31]: (style) The scope of the variable 'i' can be reduced.\n"
|
||||||
|
"[test.cpp:37]: (style) The scope of the variable 'k' can be reduced.\n",
|
||||||
errout.str());
|
errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6014,6 +6014,15 @@ private:
|
||||||
" if (tmp[0]) {}\n"
|
" if (tmp[0]) {}\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
// #11055
|
||||||
|
valueFlowUninit("void g(int*);\n"
|
||||||
|
"void f(bool b) {\n"
|
||||||
|
" int i;\n"
|
||||||
|
" int* p = b ? &i : nullptr;\n"
|
||||||
|
" g(p);\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value
|
void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value
|
||||||
|
|
Loading…
Reference in New Issue