diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 12c1e6188..0fa9d0c18 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -156,7 +156,7 @@ void CheckOther::clarifyCondition() tok2 = tok2->link(); else if (tok2->type() == Token::eComparisonOp) { // This might be a template - if (!isC && Token::Match(tok2->previous(), "%var% <")) + if (!isC && tok2->link()) break; clarifyConditionError(tok, tok->strAt(2) == "=", false); @@ -170,6 +170,9 @@ void CheckOther::clarifyCondition() // using boolean result in bitwise operation ! x [&|^] for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (Token::Match(tok, "!|<|<=|==|!=|>|>=")) { + if (tok->link()) // don't write false positives when templates are used + continue; + const Token *tok2 = tok->next(); // Todo: There are false positives if '(' if encountered. It @@ -186,8 +189,7 @@ void CheckOther::clarifyCondition() if (Token::Match(tok2, "[&|^]")) { // don't write false positives when templates are used - if (Token::Match(tok, "<|>") && (Token::Match(tok2, "& ,|>") || - Token::simpleMatch(tok2->previous(), "const &"))) + if (Token::Match(tok2, "&|* ,|>") || Token::simpleMatch(tok2->previous(), "const &")) continue; // #3609 - CWinTraits::.. diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 70cd4df77..1641915a8 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2935,6 +2935,9 @@ bool Tokenizer::createLinks() void Tokenizer::createLinks2() { + if (isC()) + return; + std::stack type; std::stack links; for (Token *token = list.front(); token; token = token->next()) { @@ -7836,7 +7839,7 @@ void Tokenizer::simplifyComma() void Tokenizer::removeExceptionSpecifications() { - for(Token* tok = list.front(); tok; tok = tok->next()) { + for (Token* tok = list.front(); tok; tok = tok->next()) { if (Token::Match(tok, ") const| throw (")) { if (tok->next()->str() == "const") { Token::eraseTokens(tok->next(), tok->linkAt(3)); diff --git a/test/testother.cpp b/test/testother.cpp index 006ff23d4..cb7555ece 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -124,6 +124,7 @@ private: TEST_CASE(clarifyCondition3); // if (! a & b) TEST_CASE(clarifyCondition4); // ticket #3110 TEST_CASE(clarifyCondition5); // #3609 CWinTraits.. + TEST_CASE(clarifyCondition6); // #3818 TEST_CASE(bitwiseOnBoolean); // if (bool & bool) TEST_CASE(comparisonOfBoolExpressionWithInt1); @@ -3657,6 +3658,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void clarifyCondition6() { + check("template\n" + "SharedPtr& operator=( SharedPtr const & r ) {\n" + " px = r.px;\n" + " return *this;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void bitwiseOnBoolean() { // 3062 check("void f(_Bool a, _Bool b) {\n" " if(a & b) {}\n"