Message refactorization: checkassignif.cpp, checkautovariables.cpp
This commit is contained in:
parent
43c060b630
commit
0f1cb4c98c
|
@ -72,7 +72,7 @@ void CheckAssignIf::assignIfError(const Token *tok, bool result)
|
|||
{
|
||||
reportError(tok, Severity::style,
|
||||
"assignIfError",
|
||||
"Mismatching assignment and comparison, comparison is always " + std::string(result ? "true" : "false"));
|
||||
"Mismatching assignment and comparison, comparison is always " + std::string(result ? "true" : "false") + ".");
|
||||
}
|
||||
|
||||
|
||||
|
@ -117,7 +117,7 @@ void CheckAssignIf::comparisonError(const Token *tok, MathLib::bigint value1, co
|
|||
std::ostringstream expression;
|
||||
expression << std::hex << "(X & 0x" << value1 << ") " << op << " 0x" << value2;
|
||||
|
||||
const std::string errmsg("Expression '" + expression.str() + "' is always " + (result?"true":"false") + "\n"
|
||||
const std::string errmsg("Expression '" + expression.str() + "' is always " + (result?"true":"false") + ".\n"
|
||||
"The expression '" + expression.str() + "' is always " + (result?"true":"false") +
|
||||
". Check carefully constants and operators used, these errors might be hard to "
|
||||
"spot sometimes. In case of complex expression it might help to split it to "
|
||||
|
@ -177,8 +177,8 @@ void CheckAssignIf::multiCondition()
|
|||
void CheckAssignIf::multiConditionError(const Token *tok, unsigned int line1)
|
||||
{
|
||||
std::ostringstream errmsg;
|
||||
errmsg << "'else if' condition matches previous condition at line "
|
||||
<< line1;
|
||||
errmsg << "Expression is always false because 'else if' condition matches previous condition at line "
|
||||
<< line1 << ".";
|
||||
|
||||
reportError(tok, Severity::style, "multiCondition", errmsg.str());
|
||||
}
|
||||
|
|
|
@ -188,28 +188,28 @@ void CheckAutoVariables::returnPointerToLocalArray()
|
|||
|
||||
void CheckAutoVariables::errorReturnAddressToAutoVariable(const Token *tok)
|
||||
{
|
||||
reportError(tok, Severity::error, "returnAddressOfAutoVariable", "Return of the address of an auto-variable");
|
||||
reportError(tok, Severity::error, "returnAddressOfAutoVariable", "Address of an auto-variable returned.");
|
||||
}
|
||||
|
||||
void CheckAutoVariables::errorReturnPointerToLocalArray(const Token *tok)
|
||||
{
|
||||
reportError(tok, Severity::error, "returnLocalVariable", "Returning pointer to local array variable");
|
||||
reportError(tok, Severity::error, "returnLocalVariable", "Pointer to local array variable returned.");
|
||||
}
|
||||
|
||||
void CheckAutoVariables::errorAutoVariableAssignment(const Token *tok, bool inconclusive)
|
||||
{
|
||||
if (!inconclusive) {
|
||||
reportError(tok, Severity::error, "autoVariables",
|
||||
"Assigning address of local auto-variable to a function parameter.\n"
|
||||
"Dangerous assignment - function parameter takes the address of a local "
|
||||
"auto-variable. Local auto-variables are reserved from the stack. And the "
|
||||
"stack is freed when the function ends. So the pointer to a local variable "
|
||||
"Address of local auto-variable assigned to a function parameter.\n"
|
||||
"Dangerous assignment - function parameter is assigned the address of a local "
|
||||
"auto-variable. Local auto-variables are reserved from the stack which "
|
||||
"is freed when the function ends. So the pointer to a local variable "
|
||||
"is invalid after the function ends.");
|
||||
} else {
|
||||
reportError(tok, Severity::error, "autoVariables",
|
||||
"Assigning address of local auto-variable to a function parameter.\n"
|
||||
"Function parameter takes the address of a local auto-variable. "
|
||||
"Local auto-variables are reserved from the stack. And the stack is freed when "
|
||||
"Address of local auto-variable assigned to a function parameter.\n"
|
||||
"Function parameter is assigned the address of a local auto-variable. "
|
||||
"Local auto-variables are reserved from the stack which is freed when "
|
||||
"the function ends. The address is invalid after the function ends and it "
|
||||
"might 'leak' from the function through the parameter.", true);
|
||||
}
|
||||
|
@ -218,9 +218,10 @@ void CheckAutoVariables::errorAutoVariableAssignment(const Token *tok, bool inco
|
|||
void CheckAutoVariables::errorReturnAddressOfFunctionParameter(const Token *tok, const std::string &varname)
|
||||
{
|
||||
reportError(tok, Severity::error, "returnAddressOfFunctionParameter",
|
||||
"Return the address of function parameter '" + varname + "'\n"
|
||||
"Address of the function parameter '" + varname + "' is invalid after the function exits. "
|
||||
"Function parameters are created into the stack. When the function exits the stack is deleted.");
|
||||
"Address of function parameter '" + varname + "' returned.\n"
|
||||
"Address of the function parameter '" + varname + "' becomes invalid after the function exits because "
|
||||
"function parameters are stored on the stack which is freed when the function exits. Thus the returned "
|
||||
"value is invalid.");
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
@ -288,12 +289,12 @@ void CheckAutoVariables::returnReference()
|
|||
|
||||
void CheckAutoVariables::errorReturnReference(const Token *tok)
|
||||
{
|
||||
reportError(tok, Severity::error, "returnReference", "Returning reference to auto variable");
|
||||
reportError(tok, Severity::error, "returnReference", "Reference to auto variable returned.");
|
||||
}
|
||||
|
||||
void CheckAutoVariables::errorReturnTempReference(const Token *tok)
|
||||
{
|
||||
reportError(tok, Severity::error, "returnTempReference", "Returning reference to temporary");
|
||||
reportError(tok, Severity::error, "returnTempReference", "Reference to temporary returned.");
|
||||
}
|
||||
|
||||
void CheckAutoVariables::errorInvalidDeallocation(const Token *tok)
|
||||
|
@ -301,8 +302,8 @@ void CheckAutoVariables::errorInvalidDeallocation(const Token *tok)
|
|||
reportError(tok,
|
||||
Severity::error,
|
||||
"autovarInvalidDeallocation",
|
||||
"Deallocating auto-variable is invalid\n"
|
||||
"Deallocating an auto-variable is invalid. You should only free memory "
|
||||
"Deallocation of an auto-variable results in undefined behaviour.\n"
|
||||
"Deallocation of an auto-variable results in undefined behaviour. You should only free memory "
|
||||
"that has been allocated dynamically.");
|
||||
}
|
||||
|
||||
|
@ -341,5 +342,5 @@ void CheckAutoVariables::returncstr()
|
|||
|
||||
void CheckAutoVariables::errorReturnTempPointer(const Token *tok)
|
||||
{
|
||||
reportError(tok, Severity::error, "returnTempPointer", "Returning pointer to temporary");
|
||||
reportError(tok, Severity::error, "returnTempPointer", "Pointer to temporary returned.");
|
||||
}
|
||||
|
|
|
@ -65,21 +65,21 @@ private:
|
|||
" int y = x & 4;\n"
|
||||
" if (y == 3);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always false\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always false.\n", errout.str());
|
||||
|
||||
check("void foo(int x)\n"
|
||||
"{\n"
|
||||
" int y = x & 4;\n"
|
||||
" if (y != 3);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always true\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always true.\n", errout.str());
|
||||
|
||||
// |
|
||||
check("void foo(int x) {\n"
|
||||
" int y = x | 0x14;\n"
|
||||
" if (y == 0x710);\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Mismatching assignment and comparison, comparison is always false\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Mismatching assignment and comparison, comparison is always false.\n", errout.str());
|
||||
|
||||
check("void foo(int x) {\n"
|
||||
" int y = x | 0x14;\n"
|
||||
|
@ -93,19 +93,19 @@ private:
|
|||
"{\n"
|
||||
" if (x & 4 == 3);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X & 0x4) == 0x3' is always false\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X & 0x4) == 0x3' is always false.\n", errout.str());
|
||||
|
||||
check("void foo(int x)\n"
|
||||
"{\n"
|
||||
" if ((x & 4) == 3);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X & 0x4) == 0x3' is always false\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X & 0x4) == 0x3' is always false.\n", errout.str());
|
||||
|
||||
check("void foo(int x)\n"
|
||||
"{\n"
|
||||
" if (x & 4 != 3);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X & 0x4) != 0x3' is always true\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X & 0x4) != 0x3' is always true.\n", errout.str());
|
||||
}
|
||||
|
||||
void multicompare() {
|
||||
|
@ -114,14 +114,14 @@ private:
|
|||
" if (x & 7);\n"
|
||||
" else if (x == 1);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) 'else if' condition matches previous condition at line 3\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Expression is always false because 'else if' condition matches previous condition at line 3.\n", errout.str());
|
||||
|
||||
check("void foo(int x)\n"
|
||||
"{\n"
|
||||
" if (x & 7);\n"
|
||||
" else if (x & 1);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) 'else if' condition matches previous condition at line 3\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Expression is always false because 'else if' condition matches previous condition at line 3.\n", errout.str());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -106,7 +106,7 @@ private:
|
|||
" int num = 2;\n"
|
||||
" *res = #\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
|
||||
|
||||
check("void func1(int **res)\n"
|
||||
"{\n"
|
||||
|
@ -132,7 +132,7 @@ private:
|
|||
" int num = 2;\n"
|
||||
" *res = #\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:7]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:7]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
|
||||
|
||||
check("class Fred {\n"
|
||||
" void func1(int **res);\n"
|
||||
|
@ -161,7 +161,7 @@ private:
|
|||
" int x[100];\n"
|
||||
" *p = x;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
|
||||
}
|
||||
|
||||
void testautovar4() { // ticket #2928
|
||||
|
@ -186,7 +186,7 @@ private:
|
|||
" char a;\n"
|
||||
" ab->a = &a;\n"
|
||||
"}", true);
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error, inconclusive) Assigning address of local auto-variable to a function parameter.\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error, inconclusive) Address of local auto-variable assigned to a function parameter.\n", errout.str());
|
||||
}
|
||||
|
||||
void testautovar6() { // ticket #2931
|
||||
|
@ -202,7 +202,7 @@ private:
|
|||
" char a[10];\n"
|
||||
" x->str = a;\n"
|
||||
"}", true);
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error, inconclusive) Assigning address of local auto-variable to a function parameter.\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error, inconclusive) Address of local auto-variable assigned to a function parameter.\n", errout.str());
|
||||
}
|
||||
|
||||
void testautovar7() { // ticket #3066
|
||||
|
@ -220,7 +220,7 @@ private:
|
|||
" int i = 0;\n"
|
||||
" p = &i;\n"
|
||||
"}", false);
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
|
||||
}
|
||||
|
||||
void testautovar9() {
|
||||
|
@ -233,7 +233,7 @@ private:
|
|||
" p = &p_fp->i;\n"
|
||||
" p = &fp.f->i;\n"
|
||||
"}", false);
|
||||
ASSERT_EQUALS("[test.cpp:6]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:6]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
|
||||
}
|
||||
|
||||
void testautovar_array1() {
|
||||
|
@ -242,7 +242,7 @@ private:
|
|||
" int num=2;"
|
||||
" arr[0]=#\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
|
||||
}
|
||||
|
||||
void testautovar_array2() {
|
||||
|
@ -254,7 +254,7 @@ private:
|
|||
" int num=2;"
|
||||
" arr[0]=#\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:6]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:6]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
|
||||
}
|
||||
|
||||
void testautovar_return1() {
|
||||
|
@ -263,7 +263,7 @@ private:
|
|||
" int num=2;"
|
||||
" return #"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Return of the address of an auto-variable\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Address of an auto-variable returned.\n", errout.str());
|
||||
}
|
||||
|
||||
void testautovar_return2() {
|
||||
|
@ -275,7 +275,7 @@ private:
|
|||
" int num=2;"
|
||||
" return #"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:6]: (error) Return of the address of an auto-variable\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:6]: (error) Address of an auto-variable returned.\n", errout.str());
|
||||
}
|
||||
|
||||
void testautovar_return3() {
|
||||
|
@ -295,7 +295,7 @@ private:
|
|||
" char q[] = \"AAAAAAAAAAAA\";\n"
|
||||
" return &q[1];\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Return of the address of an auto-variable\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Address of an auto-variable returned.\n", errout.str());
|
||||
|
||||
check("char *foo()\n"
|
||||
"{\n"
|
||||
|
@ -327,11 +327,11 @@ private:
|
|||
" char tmp5[256];\n"
|
||||
" delete[] tmp5;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Deallocating auto-variable is invalid\n"
|
||||
"[test.cpp:5]: (error) Deallocating auto-variable is invalid\n"
|
||||
"[test.cpp:7]: (error) Deallocating auto-variable is invalid\n"
|
||||
"[test.cpp:9]: (error) Deallocating auto-variable is invalid\n"
|
||||
"[test.cpp:11]: (error) Deallocating auto-variable is invalid\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Deallocation of an auto-variable results in undefined behaviour.\n"
|
||||
"[test.cpp:5]: (error) Deallocation of an auto-variable results in undefined behaviour.\n"
|
||||
"[test.cpp:7]: (error) Deallocation of an auto-variable results in undefined behaviour.\n"
|
||||
"[test.cpp:9]: (error) Deallocation of an auto-variable results in undefined behaviour.\n"
|
||||
"[test.cpp:11]: (error) Deallocation of an auto-variable results in undefined behaviour.\n", errout.str());
|
||||
|
||||
check("void func1() {\n"
|
||||
" char* tmp1[256];\n"
|
||||
|
@ -374,7 +374,7 @@ private:
|
|||
" char str[100] = {0};\n"
|
||||
" return str;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Returning pointer to local array variable\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Pointer to local array variable returned.\n", errout.str());
|
||||
|
||||
check("class Fred {\n"
|
||||
" char *foo();\n"
|
||||
|
@ -384,7 +384,7 @@ private:
|
|||
" char str[100] = {0};\n"
|
||||
" return str;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:7]: (error) Returning pointer to local array variable\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:7]: (error) Pointer to local array variable returned.\n", errout.str());
|
||||
}
|
||||
|
||||
void returnLocalVariable2() {
|
||||
|
@ -412,14 +412,14 @@ private:
|
|||
" std::string s;\n"
|
||||
" return s;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Returning reference to auto variable\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Reference to auto variable returned.\n", errout.str());
|
||||
|
||||
check("std::vector<int> &foo()\n"
|
||||
"{\n"
|
||||
" std::vector<int> v;\n"
|
||||
" return v;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Returning reference to auto variable\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Reference to auto variable returned.\n", errout.str());
|
||||
|
||||
check("std::vector<int> &foo()\n"
|
||||
"{\n"
|
||||
|
@ -437,7 +437,7 @@ private:
|
|||
"{\n"
|
||||
" return hello();\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:8]: (error) Returning reference to temporary\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:8]: (error) Reference to temporary returned.\n", errout.str());
|
||||
}
|
||||
|
||||
void returnReference2() {
|
||||
|
@ -449,7 +449,7 @@ private:
|
|||
" std::string s;\n"
|
||||
" return s;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:7]: (error) Returning reference to auto variable\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:7]: (error) Reference to auto variable returned.\n", errout.str());
|
||||
|
||||
check("class Fred {\n"
|
||||
" std::vector<int> &foo();\n"
|
||||
|
@ -459,7 +459,7 @@ private:
|
|||
" std::vector<int> v;\n"
|
||||
" return v;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:7]: (error) Returning reference to auto variable\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:7]: (error) Reference to auto variable returned.\n", errout.str());
|
||||
|
||||
check("class Fred {\n"
|
||||
" std::vector<int> &foo();\n"
|
||||
|
@ -482,7 +482,7 @@ private:
|
|||
"{\n"
|
||||
" return hello();\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:10]: (error) Returning reference to temporary\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:10]: (error) Reference to temporary returned.\n", errout.str());
|
||||
|
||||
check("class Fred {\n"
|
||||
" std::string hello();\n"
|
||||
|
@ -496,7 +496,7 @@ private:
|
|||
"{\n"
|
||||
" return hello();\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:11]: (error) Returning reference to temporary\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:11]: (error) Reference to temporary returned.\n", errout.str());
|
||||
}
|
||||
|
||||
void returnReference3() {
|
||||
|
@ -556,7 +556,7 @@ private:
|
|||
"{\n"
|
||||
" return hello().c_str();\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:8]: (error) Returning pointer to temporary\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:8]: (error) Pointer to temporary returned.\n", errout.str());
|
||||
|
||||
check("class Fred {\n"
|
||||
" std::string hello();\n"
|
||||
|
@ -570,7 +570,7 @@ private:
|
|||
"{\n"
|
||||
" return hello().c_str();\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:11]: (error) Returning pointer to temporary\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:11]: (error) Pointer to temporary returned.\n", errout.str());
|
||||
}
|
||||
|
||||
|
||||
|
@ -594,14 +594,14 @@ private:
|
|||
" return &y;\n"
|
||||
"}\n");
|
||||
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Return the address of function parameter 'y'\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Address of function parameter 'y' returned.\n", errout.str());
|
||||
|
||||
check("int ** foo(int * y)\n"
|
||||
"{\n"
|
||||
" return &y;\n"
|
||||
"}\n");
|
||||
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Return the address of function parameter 'y'\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Address of function parameter 'y' returned.\n", errout.str());
|
||||
|
||||
check("const int * foo(const int & y)\n"
|
||||
"{\n"
|
||||
|
|
Loading…
Reference in New Issue