Fixed #2713 (False positive (Redundant assignment))
This commit is contained in:
parent
cc41da1dc4
commit
739b6a93e2
|
@ -6213,6 +6213,22 @@ void Tokenizer::simplifyIfNotNull()
|
|||
{
|
||||
Token *deleteFrom = NULL;
|
||||
|
||||
// Remove 'x = (x != 0)'
|
||||
if (Token::simpleMatch(tok, "= ("))
|
||||
{
|
||||
if (Token::Match(tok->tokAt(-2), "[;{}] %var%"))
|
||||
{
|
||||
const unsigned int varid = tok->previous()->varId();
|
||||
if (Token::Match(tok, "= ( %varid% != 0 ) ;", varid) ||
|
||||
Token::Match(tok, "= ( 0 != %varid% ) ;", varid))
|
||||
{
|
||||
tok = tok->tokAt(-2);
|
||||
Token::eraseTokens(tok, tok->tokAt(9));
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Token::Match(tok, "(|&&|%oror%"))
|
||||
{
|
||||
tok = tok->next();
|
||||
|
|
|
@ -253,7 +253,8 @@ public:
|
|||
|
||||
/**
|
||||
* simplify if-not NULL
|
||||
* - "if(0!=x);" => "if(x);"
|
||||
* Example: "if(0!=x);" => "if(x);"
|
||||
* Special case: 'x = (0 != x);' is removed.
|
||||
*/
|
||||
void simplifyIfNotNull();
|
||||
|
||||
|
|
|
@ -1582,6 +1582,18 @@ private:
|
|||
" Fred fred; fred = fred;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("void f(int x) {\n"
|
||||
" x = (x == 0);"
|
||||
" func(x);\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("void f(int x) {\n"
|
||||
" x = (x != 0);"
|
||||
" func(x);\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void testScanf1()
|
||||
|
|
|
@ -6727,12 +6727,23 @@ private:
|
|||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void simplifyIfNotNull() // ticket # 2601 segmentation fault
|
||||
void simplifyIfNotNull()
|
||||
{
|
||||
{
|
||||
// ticket # 2601 segmentation fault
|
||||
const char code[] = "|| #if #define <=";
|
||||
tok(code, false);
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
{
|
||||
const char code[] = "void f(int x) {\n"
|
||||
" x = (x != 0);\n"
|
||||
"}";
|
||||
ASSERT_EQUALS("void f ( int x ) { }", tok(code, false));
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
REGISTER_TEST(TestSimplifyTokens)
|
||||
|
|
Loading…
Reference in New Issue