Fix #9353 FN (style) Condition '...' is always true, add test for #10508 (#4731)

This commit is contained in:
chrchr-github 2023-01-26 22:03:12 +01:00 committed by GitHub
parent 84fdc08853
commit b12aebc817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 1 deletions

View File

@ -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();

View File

@ -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<X>&v) {\n"
" for (std::vector<X>::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()

View File

@ -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<X> v;\n";
ASSERT_EQUALS("struct X { } ; std :: vector < X > v ;", tok(code));
}
void simplifyTypedefFunction1() {
{
const char code[] = "typedef void (*my_func)();\n"