From bf98b14b4f7bfb3ac0fd3eebe2a98748bec8ea7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 4 Jan 2009 17:41:59 +0000 Subject: [PATCH] unused variable: Fixed false positive when using '|=' assignment --- checkother.cpp | 2 +- testunusedvar.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/checkother.cpp b/checkother.cpp index 7fd1a7a5c..c0c026d71 100644 --- a/checkother.cpp +++ b/checkother.cpp @@ -869,7 +869,7 @@ void CheckOther::functionVariableUsage() if ((Token::Match(tok,"[(=&!]") || isOp(tok)) && Token::Match(tok->next(), "%var%")) varUsage[ tok->strAt(1) ] |= USAGE_READ; - if (Token::Match(tok, "-=|+=|*=|/= %var%")) + if (Token::Match(tok, "-=|+=|*=|/= %var%") || Token::Match(tok, "|= %var%")) varUsage[ tok->strAt(1) ] |= USAGE_READ; if (Token::Match(tok, "%var%") && (tok->next()->str()==")" || isOp(tok->next()))) diff --git a/testunusedvar.cpp b/testunusedvar.cpp index 85042fd25..12ffc8041 100644 --- a/testunusedvar.cpp +++ b/testunusedvar.cpp @@ -71,6 +71,7 @@ private: TEST_CASE( localvarInvert ); // Usage with inverted variable TEST_CASE( localvarIf ); // Usage in if TEST_CASE( localvarIfElse ); // return tmp1 ? tmp2 : tmp3; + TEST_CASE( localvarOpAssign ); // a |= b; } void structmember1() @@ -264,6 +265,17 @@ private: ASSERT_EQUALS( std::string(""), errout.str() ); } + void localvarOpAssign() + { + functionVariableUsage( "void foo()\n" + "{\n" + " int a = 1;\n" + " int b = 2;\n" + " a |= b;\n" + "}\n" ); + ASSERT_EQUALS( std::string("[test.cpp:2]: Variable 'a' is assigned a value that is never used\n"), errout.str() ); + } + };