diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 48e268845..a17b2faf3 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1593,7 +1593,7 @@ void CheckClass::checkConst() for (int k = nestInfo.size() - 2; k >= 0; k--) classname = std::string(nestInfo[k].className + "::" + classname); - checkConstError(found, classname, functionName); + checkConstError2(found, tok2, classname, functionName); } } } @@ -1785,6 +1785,14 @@ void CheckClass::checkConstError(const Token *tok, const std::string &classname, reportError(tok, Severity::style, "functionConst", "The function '" + classname + "::" + funcname + "' can be const"); } +void CheckClass::checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname) +{ + std::list toks; + toks.push_back(tok1); + toks.push_back(tok2); + reportError(toks, Severity::style, "functionConst", "The function '" + classname + "::" + funcname + "' can be const"); +} + void CheckClass::noConstructorError(const Token *tok, const std::string &classname, bool isStruct) { reportError(tok, Severity::style, "noConstructor", "The " + std::string(isStruct ? "struct" : "class") + " '" + classname + "' has no constructor. Member variables not initialized."); diff --git a/lib/checkclass.h b/lib/checkclass.h index 087d5512a..e93cd93f5 100644 --- a/lib/checkclass.h +++ b/lib/checkclass.h @@ -169,6 +169,7 @@ private: void operatorEqToSelfError(const Token *tok); void checkConstError(const Token *tok, const std::string &classname, const std::string &funcname); + void checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname); void getErrorMessages() { diff --git a/test/testclass.cpp b/test/testclass.cpp index 3db64def9..24f0f4795 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -1778,20 +1778,20 @@ private: " int getA();\n" "};\n" "int Fred::getA() { return a; }"); - ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::getA' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::getA' can be const\n", errout.str()); checkConst("class Fred {\n" " const std::string foo();\n" "};\n" "const std::string Fred::foo() { return ""; }"); - ASSERT_EQUALS("[test.cpp:4]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (style) The function 'Fred::foo' can be const\n", errout.str()); checkConst("class Fred {\n" " std::string s;\n" " const std::string & foo();\n" "};\n" "const std::string & Fred::foo() { return ""; }"); - ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); // constructors can't be const.. checkConst("class Fred {\n" @@ -1843,7 +1843,7 @@ private: " void foo(std::string & a);\n" "};\n" "void Fred::foo(std::string & a) { a = s; }"); - ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); // assignment to variable can't be const checkConst("class Fred {\n" @@ -1859,7 +1859,7 @@ private: " void foo(std::string & a, std::string & b);\n" "};\n" "void Fred::foo(std::string & a, std::string & b) { a = s; b = s; }"); - ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); // assignment to variable, can't be const checkConst("class Fred {\n" @@ -1891,7 +1891,7 @@ private: " void foo(int * a);\n" "};\n" "void Fred::foo(int * a) { *a = s; }"); - ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); // assignment to variable, can't be const checkConst("class Fred {\n" @@ -1907,7 +1907,7 @@ private: " void foo(std::string * a, std::string * b);\n" "};\n" "void Fred::foo(std::string * a, std::string * b) { *a = s; *b = s; }"); - ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); // assignment to variable, can't be const checkConst("class Fred {\n" @@ -1943,8 +1943,8 @@ private: "void Fred::foo() { }" "void Fred::foo(std::string & a) { a = s; }" "void Fred::foo(const std::string & a) { s = a; }"); - ASSERT_EQUALS("[test.cpp:7]: (style) The function 'Fred::foo' can be const\n" - "[test.cpp:7]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n" + "[test.cpp:7] -> [test.cpp:4]: (style) The function 'Fred::foo' can be const\n", errout.str()); // check functions with different or missing paramater names checkConst("class Fred {\n" @@ -1960,11 +1960,11 @@ private: "void Fred::foo3(int a, int b) { }\n" "void Fred::foo4(int a, int b) { }\n" "void Fred::foo5(int, int) { }"); - ASSERT_EQUALS("[test.cpp:9]: (style) The function 'Fred::foo1' can be const\n" - "[test.cpp:10]: (style) The function 'Fred::foo2' can be const\n" - "[test.cpp:11]: (style) The function 'Fred::foo3' can be const\n" - "[test.cpp:12]: (style) The function 'Fred::foo4' can be const\n" - "[test.cpp:13]: (style) The function 'Fred::foo5' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (style) The function 'Fred::foo1' can be const\n" + "[test.cpp:10] -> [test.cpp:4]: (style) The function 'Fred::foo2' can be const\n" + "[test.cpp:11] -> [test.cpp:5]: (style) The function 'Fred::foo3' can be const\n" + "[test.cpp:12] -> [test.cpp:6]: (style) The function 'Fred::foo4' can be const\n" + "[test.cpp:13] -> [test.cpp:7]: (style) The function 'Fred::foo5' can be const\n", errout.str()); // check nested classes checkConst("class Fred {\n" @@ -1982,7 +1982,7 @@ private: " };\n" " int A::getA() { return a; }\n" "};"); - ASSERT_EQUALS("[test.cpp:6]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:4]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); checkConst("class Fred {\n" " class A {\n" @@ -1991,7 +1991,7 @@ private: " };\n" "};\n" "int Fred::A::getA() { return a; }"); - ASSERT_EQUALS("[test.cpp:7]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:4]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); // check deeply nested classes checkConst("class Fred {\n" @@ -2019,8 +2019,8 @@ private: " };\n" " int B::getB() { return b; }\n" "};"); - ASSERT_EQUALS("[test.cpp:11]: (style) The function 'Fred::B::getB' can be const\n" - "[test.cpp:9]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:11] -> [test.cpp:4]: (style) The function 'Fred::B::getB' can be const\n" + "[test.cpp:9] -> [test.cpp:7]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str()); checkConst("class Fred {\n" " class B {\n" @@ -2034,8 +2034,8 @@ private: "};\n" "int Fred::B::A::getA() { return a; }\n" "int Fred::B::getB() { return b; }\n"); - ASSERT_EQUALS("[test.cpp:12]: (style) The function 'Fred::B::getB' can be const\n" - "[test.cpp:11]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:12] -> [test.cpp:4]: (style) The function 'Fred::B::getB' can be const\n" + "[test.cpp:11] -> [test.cpp:7]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str()); } // operator< can often be const