From 9efedd6be90973ebb9cfd3a83e0ba0ad535ca33a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 2 Dec 2022 07:21:33 +0100 Subject: [PATCH] Fix #11223 checkLibraryFunction treats "auto" as type (#4605) * Fix #11223 checkLibraryFunction treats "auto" as type * Use utility function --- lib/token.cpp | 5 +++++ test/testfunctions.cpp | 6 ++++++ 2 files changed, 11 insertions(+) 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; }