Fixed #4423 (Variable is assigned a value that is never used.)

This commit is contained in:
Daniel Marjamki 2013-02-16 16:07:05 +01:00
parent c64174ab36
commit 635b7d5a0e
3 changed files with 19 additions and 2 deletions

View File

@ -6664,14 +6664,15 @@ bool Tokenizer::simplifyRedundantParentheses()
ret = true;
}
if (Token::Match(tok->previous(), "[(!*;{}] ( %var% )") && tok->next()->varId() != 0) {
if (Token::Match(tok->previous(), "[(!*;{}] ( %var% )") &&
(tok->next()->varId() != 0 || Token::Match(tok->tokAt(3), "[+-/=]"))) {
// We have "( var )", remove the parentheses
tok->deleteThis();
tok->deleteNext();
ret = true;
}
while (Token::Match(tok->previous(), ";|{|}|[|]|(|)|.|,|! ( %var% .")) {
while (Token::Match(tok->previous(), "[;{}[]().,!*] ( %var% .")) {
Token *tok2 = tok->tokAt(2);
while (Token::Match(tok2, ". %var%")) {
tok2 = tok2->tokAt(2);

View File

@ -314,6 +314,7 @@ private:
TEST_CASE(removeParentheses13);
TEST_CASE(removeParentheses14); // Ticket #3309
TEST_CASE(removeParentheses15); // Ticket #4142
TEST_CASE(removeParentheses16); // Ticket #4423 '*(x.y)='
TEST_CASE(tokenize_double);
TEST_CASE(tokenize_strings);
@ -5006,6 +5007,12 @@ private:
ASSERT_EQUALS("a = b ? c : ( d = 1 , 0 ) ;", tokenizeAndStringify("a = b ? c : (d=1,0);", false));
}
void removeParentheses16() { // *(x.y)=
// #4423
ASSERT_EQUALS("* x = 0 ;", tokenizeAndStringify("*(x)=0;", false));
ASSERT_EQUALS("* x . y = 0 ;", tokenizeAndStringify("*(x.y)=0;", false));
}
void tokenize_double() {
const char code[] = "void f()\n"
"{\n"

View File

@ -102,6 +102,7 @@ private:
TEST_CASE(localvaralias8);
TEST_CASE(localvaralias9); // ticket #1996
TEST_CASE(localvaralias10); // ticket #2004
TEST_CASE(localvaralias11); // ticket #4423 - iterator
TEST_CASE(localvarasm);
TEST_CASE(localvarstatic);
TEST_CASE(localvarextern);
@ -2718,6 +2719,14 @@ private:
ASSERT_EQUALS("", errout.str());
}
void localvaralias11() { // #4423 - iterator
functionVariableUsage("void f(Foo &foo) {\n"
" std::set<int>::iterator x = foo.dostuff();\n"
" *(x) = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void localvarasm() {
functionVariableUsage("void foo(int &b)\n"
"{\n"