From a6c5bb28c164450f53c47ff3245b44819df3f633 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 13 Mar 2023 15:44:34 +0100 Subject: [PATCH] Fix crash in LoopAnalyzer (#4888) --- lib/checkstl.cpp | 2 +- test/teststl.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index dd59f682a..108e528d9 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -2781,7 +2781,7 @@ namespace { if (var->declarationId() == loopVar->varId()) return false; const Scope* scope = var->scope(); - return scope->isNestedIn(bodyTok->scope()); + return scope && scope->isNestedIn(bodyTok->scope()); } private: diff --git a/test/teststl.cpp b/test/teststl.cpp index 9766c0d1a..7009e8e44 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -5476,6 +5476,23 @@ private: true); ASSERT_EQUALS("[test.cpp:2]: (style) Consider using std::all_of or std::none_of algorithm instead of a raw loop.\n", errout.str()); + + check("class C {\n" + "private:\n" + " QString s;\n" + "public:\n" + " C(QString);\n" + "private slots:\n" + " void f() {\n" + " QVERIFY(QDir(s).exists());\n" + " }\n" + " void f(const QStringList& d) {\n" + " for (QString f : d)\n" + " QDir(s);\n" + " }\n" + "};\n", + true); + ASSERT_EQUALS("", errout.str()); } void invalidContainer() {