From 635b7d5a0edbd55177ad2ae26c321786cba7a363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=E4ki?= Date: Sat, 16 Feb 2013 16:07:05 +0100 Subject: [PATCH] Fixed #4423 (Variable is assigned a value that is never used.) --- lib/tokenize.cpp | 5 +++-- test/testtokenize.cpp | 7 +++++++ test/testunusedvar.cpp | 9 +++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 8b7d34079..3fad0a85e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 44fba5695..d16229e57 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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" diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 012e34ed5..c87d77e19 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -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::iterator x = foo.dostuff();\n" + " *(x) = 0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void localvarasm() { functionVariableUsage("void foo(int &b)\n" "{\n"