diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 8271bfb7a..f140f8869 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2914,6 +2914,9 @@ void CheckOther::checkSuspiciousStringCompare() if (varTok->strAt(-1) == "+" || litTok->strAt(1) == "+") continue; + // rough filter for index access (#5734). Might cause false negatives in multidimensional structures + if (Token::simpleMatch(varTok->tokAt(1), "[") || Token::simpleMatch(litTok->tokAt(1), "[")) + continue; if ((varTok->type() == Token::eString || varTok->type() == Token::eVariable) && (litTok->type() == Token::eString || litTok->type() == Token::eVariable) && litTok->type() != varTok->type()) { if (varTok->type() == Token::eString) diff --git a/test/testother.cpp b/test/testother.cpp index f3262ac19..150082844 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5107,6 +5107,20 @@ private: TODO_ASSERT_EQUALS("[test.cpp:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead?\n", "", errout.str()); + + // Ticket #5734 + check("int foo(char c) {\n" + "return c == '42';}", "test.cpp"); + ASSERT_EQUALS("", errout.str()); + check("int foo(char c) {\n" + "return c == '42';}", "test.c"); + ASSERT_EQUALS("", errout.str()); + check("int foo(char c) {\n" + "return c == \"42\"[0];}", "test.cpp", false, true, false, false); + ASSERT_EQUALS("", errout.str()); + check("int foo(char c) {\n" + "return c == \"42\"[0];}", "test.c", false, true, false, false); + ASSERT_EQUALS("", errout.str()); } void check_signOfUnsignedVariable(const char code[], bool inconclusive=false) {