Fixed ticket 197 (false positive: condition is always true/false)
This commit is contained in:
parent
6580e0eb21
commit
da0b571aee
|
@ -218,7 +218,7 @@ void CheckOther::WarningIf()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ErrorLogger::conditionAlwaysTrueFalse(*_settings))
|
if (_settings->_checkCodingStyle)
|
||||||
{
|
{
|
||||||
// Search for 'a=b; if (a==b)'
|
// Search for 'a=b; if (a==b)'
|
||||||
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
||||||
|
@ -237,18 +237,24 @@ void CheckOther::WarningIf()
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// var1 = var2 ; if ( var3 cond var4 )
|
// var1 = var2 ; if ( var3 cond var4 )
|
||||||
const char *var1 = tok->strAt(0);
|
unsigned int var1 = tok->tokAt(0)->varId();
|
||||||
const char *var2 = tok->strAt(2);
|
unsigned int var2 = tok->tokAt(2)->varId();
|
||||||
const char *var3 = tok->strAt(6);
|
unsigned int var3 = tok->tokAt(6)->varId();
|
||||||
const char *cond = tok->strAt(7);
|
const char *cond = tok->strAt(7);
|
||||||
const char *var4 = tok->strAt(8);
|
unsigned int var4 = tok->tokAt(8)->varId();
|
||||||
|
|
||||||
|
if (var1 == 0 || var2 == 0 || var3 == 0 || var4 == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (var1 == var2 || var3 == var4)
|
||||||
|
continue;
|
||||||
|
|
||||||
// Check that var3 is equal with either var1 or var2
|
// Check that var3 is equal with either var1 or var2
|
||||||
if (strcmp(var1, var3) && strcmp(var2, var3))
|
if (var1 != var3 && var2 != var3)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Check that var4 is equal with either var1 or var2
|
// Check that var4 is equal with either var1 or var2
|
||||||
if (strcmp(var1, var4) && strcmp(var2, var4))
|
if (var1 != var4 && var2 != var4)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Check that there is a condition..
|
// Check that there is a condition..
|
||||||
|
@ -263,7 +269,20 @@ void CheckOther::WarningIf()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!iscond)
|
if (!iscond)
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
|
// If there are casting involved it's hard to know if the
|
||||||
|
// condition is true or false
|
||||||
|
const Token *vardecl1 = Token::findmatch(_tokenizer->tokens(), "; %type% %varid% ;", var1);
|
||||||
|
if (!vardecl1)
|
||||||
|
continue;
|
||||||
|
const Token *vardecl2 = Token::findmatch(_tokenizer->tokens(), "; %type% %varid% ;", var2);
|
||||||
|
if (!vardecl2)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// variable 1 & 2 must be the same type..
|
||||||
|
if (vardecl1->next()->str() != vardecl2->next()->str())
|
||||||
|
continue;
|
||||||
|
|
||||||
// we found the error. Report.
|
// we found the error. Report.
|
||||||
bool b = false;
|
bool b = false;
|
||||||
|
|
Loading…
Reference in New Issue