diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 1e0bd25a9..56abc37d4 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5257,7 +5257,7 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold // after assignment for (const Scope *functionScope : symboldatabase->functionScopes) { for (const Token *tok = functionScope->bodyStart; tok != functionScope->bodyEnd; tok = tok->next()) { - if (Token::Match(tok, "[;{}] %var% = %str% ;")) { + if (Token::Match(tok, "%name%|;|{|} %var% = %str% ;")) { const Token *containerTok = tok->next(); if (containerTok && containerTok->valueType() && containerTok->valueType()->container && containerTok->valueType()->container->stdStringLike) { ValueFlow::Value value(Token::getStrLength(containerTok->tokAt(2))); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 300439bb0..614869b6e 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -3176,6 +3176,19 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("void f() {\n" + " const std::string x=\"xyz\";\n" + " if(!x.empty()){}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) Condition '!x.empty()' is always true\n", errout.str()); + + check("std::string g();\n" + "void f() {\n" + " const std::string msg = g();\n" + " if(!msg.empty()){}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + check("void f(int *array, int size ) {\n" " for(int i = 0; i < size; ++i) {\n" " if(array == 0)\n" diff --git a/test/teststl.cpp b/test/teststl.cpp index 81839a155..dd4f8b300 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -290,6 +290,15 @@ private: "}\n"); ASSERT_EQUALS("test.cpp:3:error:Out of bounds access in expression 's.begin()+x' because 's' is empty and 'x' may be non-zero.\n", errout.str()); + checkNormal("char fstr1(){const std::string s = \"\"; return s[42]; }\n" + "wchar_t fwstr1(){const std::wstring s = L\"\"; return s[42]; }\n"); + ASSERT_EQUALS("test.cpp:1:error:Out of bounds access in 's[42]', if 's' size is 6 and '42' is 42\n" + "test.cpp:2:error:Out of bounds access in 's[42]', if 's' size is 6 and '42' is 42\n", errout.str()); + + checkNormal("char fstr1(){const std::string s = \"\"; return s[1]; }\n" + "wchar_t fwstr1(){const std::wstring s = L\"\"; return s[1]; }\n"); + ASSERT_EQUALS("", errout.str()); + checkNormal("int f() {\n" " std::vector v;\n" " std::vector * pv = &v;\n"