From b9645514243d872d0eb5cd826564d128809db56f Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sat, 24 Mar 2012 13:48:33 +0100 Subject: [PATCH] Fixed #3252 Make use of std::isupper instead of custom implementation --- lib/checkclass.cpp | 11 ++++++++--- test/testclass.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index f883ea4ba..ed45fb49d 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -26,6 +26,7 @@ #include #include +#include //--------------------------------------------------------------------------- @@ -1143,7 +1144,7 @@ void CheckClass::thisSubtractionError(const Token *tok) void CheckClass::checkConst() { - // This is an inconclusive check. False positives: #3252, #3322, #3360. + // This is an inconclusive check. False positives: #2340, #3322, #3360. if (!_settings->inconclusive) return; @@ -1192,15 +1193,19 @@ void CheckClass::checkConst() if (temp->str() != "const") continue; + } else if (func->isOperator && Token::Match(func->tokenDef->previous(), ";|{|}|public:|private:|protected:")) { // Operator without return type: conversion operator + const std::string& name = func->token->str(); + if (name.compare(8, 5, "const") != 0 && name[name.size()-1] == '&') + continue; } else { // don't warn for unknown types.. // LPVOID, HDC, etc if (previous->isName()) { bool allupper = true; - const std::string s(previous->str()); + const std::string& s(previous->str()); for (std::string::size_type pos = 0; pos < s.size(); ++pos) { const char ch = s[pos]; - if (!(ch == '_' || (ch >= 'A' && ch <= 'Z'))) { + if (ch != '_' && !std::isupper(ch)) { allupper = false; break; } diff --git a/test/testclass.cpp b/test/testclass.cpp index 9c9a38c95..1af801cff 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -196,6 +196,7 @@ private: TEST_CASE(constoperator2); // operator<< TEST_CASE(constoperator3); TEST_CASE(constoperator4); + TEST_CASE(constoperator5); // ticket #3252 TEST_CASE(constincdec); // increment/decrement => non-const TEST_CASE(constassign1); TEST_CASE(constassign2); @@ -4084,6 +4085,30 @@ private: ASSERT_EQUALS("", errout.str()); } + void constoperator5() { // ticket #3252 + checkConst("class A {\n" + " int c;\n" + "public:\n" + " operator int& () {return c}\n" + "};"); + ASSERT_EQUALS("", errout.str()); + + checkConst("class A {\n" + " int c;\n" + "public:\n" + " operator const int& () {return c}\n" + "};"); + ASSERT_EQUALS("[test.cpp:4]: (style) Technically the member function 'A::operatorconstint&' can be const.\n", errout.str()); + + checkConst("class A {\n" + " int c;\n" + "public:\n" + " operator int () {return c}\n" + "};"); + ASSERT_EQUALS("[test.cpp:4]: (style) Technically the member function 'A::operatorint' can be const.\n", errout.str()); + } + + void const5() { // ticket #1482 checkConst("class A {\n"