Minor tweak for redundantVariableAssignment
This commit is contained in:
parent
e20079a5d9
commit
affd03f1d6
|
@ -560,13 +560,25 @@ void CheckOther::checkRedundantAssignment()
|
||||||
if (tok->astParent())
|
if (tok->astParent())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Do not warn about redundant initialization
|
// Do not warn about redundant initialization when rhs is trivial
|
||||||
// TODO : do not simplify the variable declarations
|
// TODO : do not simplify the variable declarations
|
||||||
if (Token::Match(tok->tokAt(-3), "%var% ; %var% =") && tok->previous()->variable() && tok->previous()->variable()->nameToken() == tok->tokAt(-3))
|
if (Token::Match(tok->tokAt(-3), "%var% ; %var% =") && tok->previous()->variable() && tok->previous()->variable()->nameToken() == tok->tokAt(-3) && tok->tokAt(-3)->linenr() == tok->previous()->linenr()) {
|
||||||
continue;
|
bool trivial = true;
|
||||||
|
visitAstNodes(tok->astOperand2(),
|
||||||
|
[&](const Token *rhs) {
|
||||||
|
if (Token::Match(rhs, "%str%|%num%|%name%"))
|
||||||
|
return ChildrenToVisit::op1_and_op2;
|
||||||
|
if (rhs->str() == "(" && !rhs->previous()->isName())
|
||||||
|
return ChildrenToVisit::op1_and_op2;
|
||||||
|
trivial = false;
|
||||||
|
return ChildrenToVisit::done;
|
||||||
|
});
|
||||||
|
if (trivial)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Do not warn about assignment with 0 / NULL
|
// Do not warn about assignment with 0 / NULL
|
||||||
if (Token::simpleMatch(tok->astOperand2(), "0"))
|
if (Token::Match(tok->astOperand2(), "0|NULL|nullptr"))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (tok->astOperand1()->variable() && tok->astOperand1()->variable()->isReference())
|
if (tok->astOperand1()->variable() && tok->astOperand1()->variable()->isReference())
|
||||||
|
|
|
@ -5602,15 +5602,13 @@ private:
|
||||||
|
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" int i;\n"
|
" int i;\n"
|
||||||
" dostuff();\n"
|
|
||||||
" i = 1;\n"
|
" i = 1;\n"
|
||||||
" i = 1;\n"
|
" i = 1;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (style) Variable 'i' is reassigned a value before the old one has been used.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) Variable 'i' is reassigned a value before the old one has been used.\n", errout.str());
|
||||||
|
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" static int i;\n"
|
" static int i;\n"
|
||||||
" dostuff();\n"
|
|
||||||
" i = 1;\n"
|
" i = 1;\n"
|
||||||
" i = 1;\n"
|
" i = 1;\n"
|
||||||
"}");
|
"}");
|
||||||
|
@ -5686,12 +5684,11 @@ private:
|
||||||
|
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" int i;\n"
|
" int i;\n"
|
||||||
" dostuff();\n"
|
|
||||||
" i = 1;\n"
|
" i = 1;\n"
|
||||||
" bar();\n"
|
" bar();\n"
|
||||||
" i = 1;\n"
|
" i = 1;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (style) Variable 'i' is reassigned a value before the old one has been used.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style) Variable 'i' is reassigned a value before the old one has been used.\n", errout.str());
|
||||||
|
|
||||||
check("void bar(int i) {}\n"
|
check("void bar(int i) {}\n"
|
||||||
"void f(int i) {\n"
|
"void f(int i) {\n"
|
||||||
|
@ -5778,17 +5775,15 @@ private:
|
||||||
// from #3103 (avoid a false negative)
|
// from #3103 (avoid a false negative)
|
||||||
check("int foo(){\n"
|
check("int foo(){\n"
|
||||||
" int x;\n"
|
" int x;\n"
|
||||||
" dostuff();\n"
|
|
||||||
" x = 1;\n"
|
" x = 1;\n"
|
||||||
" x = 1;\n"
|
" x = 1;\n"
|
||||||
" return x + 1;\n"
|
" return x + 1;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (style) Variable 'x' is reassigned a value before the old one has been used.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) Variable 'x' is reassigned a value before the old one has been used.\n", errout.str());
|
||||||
|
|
||||||
// from #3103 (avoid a false positive)
|
// from #3103 (avoid a false positive)
|
||||||
check("int foo(){\n"
|
check("int foo(){\n"
|
||||||
" int x;\n"
|
" int x;\n"
|
||||||
" dostuff();\n"
|
|
||||||
" x = 1;\n"
|
" x = 1;\n"
|
||||||
" if (y)\n" // <-- cppcheck does not know anything about 'y'
|
" if (y)\n" // <-- cppcheck does not know anything about 'y'
|
||||||
" x = 2;\n"
|
" x = 2;\n"
|
||||||
|
@ -6016,11 +6011,10 @@ private:
|
||||||
check("class C { void operator=(int x); };\n" // #8368 - assignment operator might have side effects => inconclusive
|
check("class C { void operator=(int x); };\n" // #8368 - assignment operator might have side effects => inconclusive
|
||||||
"void f() {\n"
|
"void f() {\n"
|
||||||
" C c;\n"
|
" C c;\n"
|
||||||
" dostuff();\n"
|
|
||||||
" c = x;\n"
|
" c = x;\n"
|
||||||
" c = x;\n"
|
" c = x;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:6]: (style, inconclusive) Variable 'c' is reassigned a value before the old one has been used if variable is no semaphore variable.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (style, inconclusive) Variable 'c' is reassigned a value before the old one has been used if variable is no semaphore variable.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void redundantVarAssignment_stackoverflow() {
|
void redundantVarAssignment_stackoverflow() {
|
||||||
|
|
Loading…
Reference in New Issue