From 4d72e0f5b5b963a57f814b8ef9120bf5663aca13 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 30 Mar 2023 07:24:36 +0200 Subject: [PATCH] Fix #11625 FP constVariable when returning by reference (#4921) --- lib/checkother.cpp | 7 +++++-- test/testother.cpp | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 07e3cf50b..7193bc527 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1548,8 +1548,11 @@ void CheckOther::checkConstPointer() const Token* const gparent = parent->astParent(); if (Token::Match(gparent, "%cop%") && !gparent->isUnaryOp("&") && !gparent->isUnaryOp("*")) continue; - if (Token::simpleMatch(gparent, "return")) - continue; + if (Token::simpleMatch(gparent, "return")) { + const Function* function = gparent->scope()->function; + if (function && (!Function::returnsReference(function) || Function::returnsConst(function))) + continue; + } else if (Token::Match(gparent, "%assign%") && parent == gparent->astOperand2()) { bool takingRef = false, nonConstPtrAssignment = false; const Token *lhs = gparent->astOperand1(); diff --git a/test/testother.cpp b/test/testother.cpp index d122c4c55..f3679d080 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3066,6 +3066,13 @@ private: " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("int& g(int* p, int& r) {\n" // #11625 + " if (p)\n" + " return *p;\n" + " return r;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void constParameterCallback() {