From b12aebc81723751fc712393776ab29fc0e7921ac Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 26 Jan 2023 22:03:12 +0100 Subject: [PATCH] Fix #9353 FN (style) Condition '...' is always true, add test for #10508 (#4731) --- lib/tokenize.cpp | 2 +- test/testcondition.cpp | 16 ++++++++++++++++ test/testsimplifytypedef.cpp | 7 +++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2d6a698c5..19a3891f3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1389,7 +1389,7 @@ void Tokenizer::simplifyTypedef() // skip over class or struct in derived class declaration bool structRemoved = false; - if (isDerived && Token::Match(typeStart, "class|struct")) { + if ((isDerived || inTemplate) && Token::Match(typeStart, "class|struct")) { if (typeStart->str() == "struct") structRemoved = true; typeStart = typeStart->next(); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index d3deb0586..287abd62d 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4947,6 +4947,22 @@ private: " buffer.back() == '\\0') {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:5]: (style) Condition 'buffer.back()=='\\0'' is always false\n", errout.str()); + + // #9353 + check("typedef struct { std::string s; } X;\n" + "void f(const std::vector&v) {\n" + " for (std::vector::const_iterator it = v.begin(); it != v.end(); ++it)\n" + " if (!it->s.empty()) {\n" + " if (!it->s.empty()) {}\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (style) Condition '!it->s.empty()' is always true\n", errout.str()); + + // #10508 + check("bool f(const std::string& a, const std::string& b) {\n" + " return a.empty() || (b.empty() && a.empty());\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Return value 'a.empty()' is always false\n", errout.str()); } void alwaysTrueLoop() diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 513fb800d..78e5823ee 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -189,6 +189,7 @@ private: TEST_CASE(simplifyTypedef140); // #10798 TEST_CASE(simplifyTypedef141); // #10144 TEST_CASE(simplifyTypedef142); // T() when T is a pointer type + TEST_CASE(simplifyTypedef144); // #9353 TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -3070,6 +3071,12 @@ private: ASSERT_EQUALS("void f ( int * = ( int * ) 0 ) { }", tok(code2)); } + void simplifyTypedef144() { // #9353 + const char code[] = "typedef struct {} X;\n" + "std::vector v;\n"; + ASSERT_EQUALS("struct X { } ; std :: vector < X > v ;", tok(code)); + } + void simplifyTypedefFunction1() { { const char code[] = "typedef void (*my_func)();\n"