diff --git a/lib/token.cpp b/lib/token.cpp index 757e75e73..9e829e35c 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -2264,6 +2264,11 @@ std::pair Token::typeDecl(const Token * tok) if (r.first) return r; } + if (astIsRangeBasedForDecl(var->nameToken()) && astIsContainer(var->nameToken()->astParent()->astOperand2())) { // range-based for + const ValueType* vt = var->nameToken()->astParent()->astOperand2()->valueType(); + if (vt && vt->containerTypeToken) + return { vt->containerTypeToken, vt->containerTypeToken->linkAt(-1) }; + } } return {var->typeStartToken(), var->typeEndToken()->next()}; } else if (Token::simpleMatch(tok, "return")) { diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 753c8d4b9..585007362 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -1900,6 +1900,12 @@ private: "void g() { C::f(); }\n"); ASSERT_EQUALS("", errout.str()); + check("void f(const std::vector& v) {\n" // #11223 + " for (const auto& s : v)\n" + " s.find(\"\");\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function s.find() is not used.\n", errout.str()); + settings.severity = severity_old; settings.checkLibrary = false; }