From 17ec0af6a73dd785be9e0d1f56408a1ec3a105f3 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Sun, 27 Apr 2014 09:40:13 +0200 Subject: [PATCH] #5734 A FP literalWithCharPtrCompare was issued upon comparison with a char referenced within a string literal --- lib/checkother.cpp | 3 +++ test/testother.cpp | 14 ++++++++++++++ 2 files changed, 17 insertions(+) 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) {