Function constness: I don't think it's necessary to show both definition and implementation location. I changed so only the location of the definition is shown. This fixes #1486.

This commit is contained in:
Daniel Marjamäki 2010-03-09 07:29:03 +01:00
parent a0d6da506b
commit dbc235bd39
3 changed files with 21 additions and 31 deletions

View File

@ -1593,7 +1593,7 @@ void CheckClass::checkConst()
for (int k = nestInfo.size() - 2; k >= 0; k--) for (int k = nestInfo.size() - 2; k >= 0; k--)
classname = std::string(nestInfo[k].className + "::" + classname); classname = std::string(nestInfo[k].className + "::" + classname);
checkConstError2(found, tok2, classname, functionName); checkConstError(found, classname, functionName);
} }
} }
} }
@ -1785,14 +1785,6 @@ void CheckClass::checkConstError(const Token *tok, const std::string &classname,
reportError(tok, Severity::style, "functionConst", "The function '" + classname + "::" + funcname + "' can be const"); 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<const Token *> 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) 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."); reportError(tok, Severity::style, "noConstructor", "The " + std::string(isStruct ? "struct" : "class") + " '" + classname + "' has no constructor. Member variables not initialized.");

View File

@ -169,7 +169,6 @@ private:
void operatorEqToSelfError(const Token *tok); void operatorEqToSelfError(const Token *tok);
void checkConstError(const Token *tok, const std::string &classname, const std::string &funcname); 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() void getErrorMessages()
{ {
@ -185,7 +184,6 @@ private:
operatorEqRetRefThisError(0); operatorEqRetRefThisError(0);
operatorEqToSelfError(0); operatorEqToSelfError(0);
checkConstError(0, "class", "function"); checkConstError(0, "class", "function");
checkConstError2(0, 0, "class", "function");
} }
std::string name() const std::string name() const

View File

@ -1778,20 +1778,20 @@ private:
" int getA();\n" " int getA();\n"
"};\n" "};\n"
"int Fred::getA() { return a; }"); "int Fred::getA() { return a; }");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::getA' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::getA' can be const\n", errout.str());
checkConst("class Fred {\n" checkConst("class Fred {\n"
" const std::string foo();\n" " const std::string foo();\n"
"};\n" "};\n"
"const std::string Fred::foo() { return ""; }"); "const std::string Fred::foo() { return ""; }");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (style) The function 'Fred::foo' can be const\n", errout.str());
checkConst("class Fred {\n" checkConst("class Fred {\n"
" std::string s;\n" " std::string s;\n"
" const std::string & foo();\n" " const std::string & foo();\n"
"};\n" "};\n"
"const std::string & Fred::foo() { return ""; }"); "const std::string & Fred::foo() { return ""; }");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str());
// constructors can't be const.. // constructors can't be const..
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1843,7 +1843,7 @@ private:
" void foo(std::string & a);\n" " void foo(std::string & a);\n"
"};\n" "};\n"
"void Fred::foo(std::string & a) { a = s; }"); "void Fred::foo(std::string & a) { a = s; }");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str());
// assignment to variable can't be const // assignment to variable can't be const
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1859,7 +1859,7 @@ private:
" void foo(std::string & a, std::string & b);\n" " void foo(std::string & a, std::string & b);\n"
"};\n" "};\n"
"void Fred::foo(std::string & a, std::string & b) { a = s; b = s; }"); "void Fred::foo(std::string & a, std::string & b) { a = s; b = s; }");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str());
// assignment to variable, can't be const // assignment to variable, can't be const
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1891,7 +1891,7 @@ private:
" void foo(int * a);\n" " void foo(int * a);\n"
"};\n" "};\n"
"void Fred::foo(int * a) { *a = s; }"); "void Fred::foo(int * a) { *a = s; }");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str());
// assignment to variable, can't be const // assignment to variable, can't be const
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1907,7 +1907,7 @@ private:
" void foo(std::string * a, std::string * b);\n" " void foo(std::string * a, std::string * b);\n"
"};\n" "};\n"
"void Fred::foo(std::string * a, std::string * b) { *a = s; *b = s; }"); "void Fred::foo(std::string * a, std::string * b) { *a = s; *b = s; }");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str());
// assignment to variable, can't be const // assignment to variable, can't be const
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1943,8 +1943,8 @@ private:
"void Fred::foo() { }" "void Fred::foo() { }"
"void Fred::foo(std::string & a) { a = s; }" "void Fred::foo(std::string & a) { a = s; }"
"void Fred::foo(const std::string & a) { s = a; }"); "void Fred::foo(const std::string & a) { s = a; }");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n" ASSERT_EQUALS("[test.cpp:7]: (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()); "[test.cpp:7]: (style) The function 'Fred::foo' can be const\n", errout.str());
// check functions with different or missing paramater names // check functions with different or missing paramater names
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1960,11 +1960,11 @@ private:
"void Fred::foo3(int a, int b) { }\n" "void Fred::foo3(int a, int b) { }\n"
"void Fred::foo4(int a, int b) { }\n" "void Fred::foo4(int a, int b) { }\n"
"void Fred::foo5(int, int) { }"); "void Fred::foo5(int, int) { }");
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (style) The function 'Fred::foo1' can be const\n" ASSERT_EQUALS("[test.cpp:9]: (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:10]: (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:11]: (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:12]: (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()); "[test.cpp:13]: (style) The function 'Fred::foo5' can be const\n", errout.str());
// check nested classes // check nested classes
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1982,7 +1982,7 @@ private:
" };\n" " };\n"
" int A::getA() { return a; }\n" " int A::getA() { return a; }\n"
"};"); "};");
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:4]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (style) The function 'Fred::A::getA' can be const\n", errout.str());
checkConst("class Fred {\n" checkConst("class Fred {\n"
" class A {\n" " class A {\n"
@ -1991,7 +1991,7 @@ private:
" };\n" " };\n"
"};\n" "};\n"
"int Fred::A::getA() { return a; }"); "int Fred::A::getA() { return a; }");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:4]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:7]: (style) The function 'Fred::A::getA' can be const\n", errout.str());
// check deeply nested classes // check deeply nested classes
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -2019,8 +2019,8 @@ private:
" };\n" " };\n"
" int B::getB() { return b; }\n" " int B::getB() { return b; }\n"
"};"); "};");
ASSERT_EQUALS("[test.cpp:11] -> [test.cpp:4]: (style) The function 'Fred::B::getB' can be const\n" ASSERT_EQUALS("[test.cpp:11]: (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()); "[test.cpp:9]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str());
checkConst("class Fred {\n" checkConst("class Fred {\n"
" class B {\n" " class B {\n"
@ -2034,8 +2034,8 @@ private:
"};\n" "};\n"
"int Fred::B::A::getA() { return a; }\n" "int Fred::B::A::getA() { return a; }\n"
"int Fred::B::getB() { return b; }\n"); "int Fred::B::getB() { return b; }\n");
ASSERT_EQUALS("[test.cpp:12] -> [test.cpp:4]: (style) The function 'Fred::B::getB' can be const\n" ASSERT_EQUALS("[test.cpp:12]: (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()); "[test.cpp:11]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str());
} }
// operator< can often be const // operator< can often be const