From 185c0c148828fb1868b99ad46e0af09041d55642 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 23 Feb 2026 10:09:34 +0100 Subject: [PATCH 1/2] Update checkclass.cpp --- lib/checkclass.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index f56c799b247..6321bf3c9ea 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2545,8 +2545,10 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member } } } else if (lhs->str() == ":" && lhs->astParent() && lhs->astParent()->str() == "(") { // range-based for-loop (C++11) - // TODO: We could additionally check what is done with the elements to avoid false negatives. Here we just rely on "const" keyword being used. - if (lhs->astParent()->strAt(1) != "const") + const Variable* loopVar = lhs->astOperand1()->variable(); + if (!loopVar || !loopVar->valueType()) + return false; + if (!loopVar->valueType()->isConst(loopVar->valueType()->pointer)) return false; } else { if (lhs->isAssignmentOp()) { From 306167bf6dcb7f5740c240fada50378c5f4a1d20 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 23 Feb 2026 10:14:50 +0100 Subject: [PATCH 2/2] Update testclass.cpp --- test/testclass.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/testclass.cpp b/test/testclass.cpp index 04f298774d1..ff486e3e450 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -7741,6 +7741,19 @@ class TestClass : public TestFixture { " }\n" "};"); ASSERT_EQUALS("[test.cpp:8:10]: (style, inconclusive) Technically the member function 'Fred::f2' can be const. [functionConst]\n", errout_str()); + + checkConst("struct T {\n" // #14390 + " std::vector v;\n" + " void f() {\n" + " for (const auto& p : v)\n" + " *p = 0;\n" + " }\n" + " void g() {\n" + " for (auto* p : v)\n" + " *p = 0;\n" + " }\n" + "};\n"); + ASSERT_EQUALS("", errout_str()); } void const_shared_ptr() { // #8674