From 5cceacd935ddbe3f9caac5d9d3d4ec55469c82eb Mon Sep 17 00:00:00 2001 From: chrchr Date: Mon, 15 Jan 2024 17:44:26 +0100 Subject: [PATCH] Fix #12351 FN constParameterPointer when taking const reference --- lib/checkother.cpp | 2 +- test/testother.cpp | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 8e23a0617d9..4c88092638b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1547,7 +1547,7 @@ void CheckOther::checkConstPointer() else if (Token::Match(gparent, "%assign%") && parent == gparent->astOperand2()) { bool takingRef = false, nonConstPtrAssignment = false; const Token *lhs = gparent->astOperand1(); - if (lhs && lhs->variable() && lhs->variable()->isReference() && lhs->variable()->nameToken() == lhs) + if (lhs && lhs->variable() && lhs->variable()->isReference() && lhs->variable()->nameToken() == lhs && !lhs->variable()->isConst()) takingRef = true; if (lhs && lhs->valueType() && lhs->valueType()->pointer && (lhs->valueType()->constness & 1) == 0 && parent->valueType() && parent->valueType()->pointer) diff --git a/test/testother.cpp b/test/testother.cpp index c1b09ef2b4a..49899b6a5bc 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3480,7 +3480,7 @@ class TestOther : public TestFixture { "[test.cpp:8]: (style) Parameter 'v' can be declared as reference to const\n", errout.str()); - check("void cb(const std::string&);\n" // #12350 + check("void cb(const std::string&);\n" // #12350, #12351 "void f(std::string& s) {\n" " const std::string& str(s);\n" " cb(str);\n" @@ -3492,10 +3492,15 @@ class TestOther : public TestFixture { "void h(std::string* s) {\n" " const std::string& str(*s);\n" " cb(str);\n" + "}\n" + "void k(std::string* s) {\n" + " const std::string& str = *s;\n" + " cb(str);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 's' can be declared as reference to const\n" "[test.cpp:6]: (style) Parameter 's' can be declared as reference to const\n" - "[test.cpp:10]: (style) Parameter 's' can be declared as pointer to const\n", + "[test.cpp:10]: (style) Parameter 's' can be declared as pointer to const\n" + "[test.cpp:14]: (style) Parameter 's' can be declared as pointer to const\n", errout.str()); }