Fixed #6214: non-static member initializer causes false positive
This commit is contained in:
parent
96e8b83bf8
commit
9dd3bce98b
|
@ -1136,6 +1136,17 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const
|
|||
return false;
|
||||
}
|
||||
|
||||
const Token * Variable::declEndToken() const
|
||||
{
|
||||
Token const * declEnd = typeStartToken();
|
||||
while (declEnd && !Token::Match(declEnd, "[;,)={]")) {
|
||||
if (declEnd->link() && Token::Match(declEnd,"(|["))
|
||||
declEnd = declEnd->link();
|
||||
declEnd = declEnd->next();
|
||||
}
|
||||
return declEnd;
|
||||
}
|
||||
|
||||
void Variable::evaluate()
|
||||
{
|
||||
const Token* tok = _start;
|
||||
|
@ -1204,8 +1215,9 @@ void Variable::evaluate()
|
|||
// type var = x or
|
||||
// type var = {x}
|
||||
// type var = x; gets simplified to: type var ; var = x ;
|
||||
if ((Token::Match(_name, "%var% ; %var% = ") && _name->strAt(2) == _name->str()) ||
|
||||
Token::Match(_name, "%var% {"))
|
||||
Token const * declEnd = declEndToken();
|
||||
if ((Token::Match(declEnd, "; %var% = ") && declEnd->strAt(1) == _name->str()) ||
|
||||
Token::Match(declEnd, "=|{"))
|
||||
setFlag(fHasDefault, true);
|
||||
}
|
||||
|
||||
|
|
|
@ -215,6 +215,15 @@ public:
|
|||
return _end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get end token of variable declaration
|
||||
* E.g.
|
||||
* int i[2][3] = ...
|
||||
* end token ^
|
||||
* @return variable declaration end token
|
||||
*/
|
||||
const Token *declEndToken() const;
|
||||
|
||||
/**
|
||||
* Get name string.
|
||||
* @return name string
|
||||
|
|
|
@ -385,8 +385,24 @@ private:
|
|||
" int x = 0;\n"
|
||||
" int y = f();\n"
|
||||
" int z{0};\n"
|
||||
" int (*pf[2])(){nullptr, nullptr};\n"
|
||||
" int a[2][3] = {{1,2,3},{4,5,6}};\n"
|
||||
" int d, e{3};\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::d' is not initialized in the constructor.\n", errout.str());
|
||||
|
||||
check("class Fred {\n"
|
||||
"public:\n"
|
||||
" Fred() {}\n"
|
||||
"private:\n"
|
||||
" int b{1}, c{2};\n"
|
||||
" int d, e{3};\n"
|
||||
" int f{4}, g;\n"
|
||||
"};");
|
||||
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::d' is not initialized in the constructor.\n"
|
||||
"[test.cpp:3]: (warning) Member variable 'Fred::g' is not initialized in the constructor.\n",
|
||||
"[test.cpp:3]: (warning) Member variable 'Fred::d' is not initialized in the constructor.\n",
|
||||
errout.str()); // fails due to missing varid
|
||||
}
|
||||
|
||||
void simple12() { // ticket #4620
|
||||
|
|
Loading…
Reference in New Issue