diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 799fadbcf..f75f5f83f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -317,12 +317,21 @@ void CheckOther::checkSelfAssignment() if (!_settings->_checkCodingStyle) return; + // POD variables.. + std::set pod; + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) + { + if (tok->isStandardType() && tok->next()->varId() && Token::Match(tok->tokAt(2), "[,);]")) + pod.insert(tok->next()->varId()); + } + const char selfAssignmentPattern[] = "%var% = %var% ;|=|)"; const Token *tok = Token::findmatch(_tokenizer->tokens(), selfAssignmentPattern); while (tok) { if (Token::Match(tok->previous(), "[;{}]") && - tok->varId() && tok->varId() == tok->tokAt(2)->varId()) + tok->varId() && tok->varId() == tok->tokAt(2)->varId() && + pod.find(tok->varId()) != pod.end()) { selfAssignmentError(tok, tok->str()); } diff --git a/test/testother.cpp b/test/testother.cpp index 1e7eca1d3..bdd9261af 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1157,14 +1157,12 @@ private: check("void foo()\n" "{\n" " int x = x;\n" - " return 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of \"x\" to itself\n", errout.str()); check("void foo()\n" "{\n" " std::string var = var = \"test\";\n" - " return 0;\n" "}\n"); TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of \"var\" to itself\n", "", errout.str()); @@ -1182,6 +1180,13 @@ private: " *x = x;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // non-primitive type -> there might be some side effects + check("void foo()\n" + "{\n" + " Fred fred; fred = fred;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void testScanf1()