Fix issue 8687: false positive with same expression (#1336)

This commit is contained in:
Paul Fultz II 2018-08-13 05:55:41 -05:00 committed by Daniel Marjamäki
parent 55ab842083
commit c5154286da
2 changed files with 40 additions and 1 deletions

View File

@ -178,7 +178,7 @@ static const Token * followVariableExpression(const Token * tok, bool cpp)
if (var->isArgument())
return tok;
// If this is in a loop then check if variables are modified in the entire scope
const Token * endToken = (isInLoopCondition(tok) || var->scope() != tok->scope()) ? var->scope()->bodyEnd : tok;
const Token * endToken = (isInLoopCondition(tok) || isInLoopCondition(varTok) || var->scope() != tok->scope()) ? var->scope()->bodyEnd : tok;
if (!var->isConst() && isVariableChanged(varTok, endToken, tok->varId(), false, nullptr, cpp))
return tok;
// Start at begining of initialization

View File

@ -4093,6 +4093,45 @@ private:
check("void f(int b) { int a = 1; while (b) { if ( a != 1){} b++; } a++; } \n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" for(int i = 0; i < 10;) {\n"
" if( i != 0 ) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Same expression on both sides of '!=' because the value of 'i' and '0' are the same.\n", errout.str());
check("void f() {\n"
" for(int i = 0; i < 10;) {\n"
" if( i != 0 ) {}\n"
" i++;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" for(int i = 0; i < 10;) {\n"
" if( i != 0 ) { i++; }\n"
" i++;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" for(int i = 0; i < 10;) {\n"
" if( i != 0 ) { i++; }\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int i = 0;\n"
" while(i < 10) {\n"
" if( i != 0 ) {}\n"
" i++;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(int b) {\n"
" while (b) {\n"
" int a = 1;\n"