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() ); + } + };