From a1528d31549086d28cd7277bfc09ff87a14f54d1 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 26 Mar 2010 16:30:30 +0100 Subject: [PATCH] Fixed #1522 (false positive: function can be const (assignment in return)) --- lib/checkclass.cpp | 2 +- test/testclass.cpp | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 455ed4008..fa11c094b 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1743,7 +1743,7 @@ bool CheckClass::sameFunc(int nest, const Token *firstEnd, const Token *secondEn bool CheckClass::isMemberVar(const Var *varlist, const Token *tok) { - while (tok->previous() && !Token::Match(tok->previous(), "}|{|;|public:|protected:|private:|return")) + while (tok->previous() && !Token::Match(tok->previous(), "}|{|;|public:|protected:|private:|return|:|?")) { if (Token::Match(tok->previous(), "* this")) return true; diff --git a/test/testclass.cpp b/test/testclass.cpp index 8c1e3c8fc..58c849938 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -94,7 +94,7 @@ private: TEST_CASE(const7); TEST_CASE(const8); // ticket #1517 TEST_CASE(const9); // ticket #1515 - TEST_CASE(const10); + TEST_CASE(const10); // ticket #1522 TEST_CASE(constoperator); // operator< can often be const TEST_CASE(constincdec); // increment/decrement => non-const TEST_CASE(constReturnReference); @@ -2158,6 +2158,7 @@ private: void const10() { + // ticket #1522 checkConst("class A {\n" "public:\n" " int foo() { return x = 0; }\n" @@ -2165,6 +2166,22 @@ private: " int x;\n" "}"); ASSERT_EQUALS("", errout.str()); + + checkConst("class A {\n" + "public:\n" + " int foo() { return (x ? x : x = 0); }\n" + "private:\n" + " int x;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + checkConst("class A {\n" + "public:\n" + " int foo() { return (x ? x = 0 : x); }\n" + "private:\n" + " int x;\n" + "}"); + ASSERT_EQUALS("", errout.str()); } // increment/decrement => not const