Added test cases for catching when assignment comes after an operator, compound assignment, or comparison for ticket #7429 and fixed the tests.
This commit is contained in:
parent
18adb97873
commit
b58562fc7d
|
@ -565,7 +565,12 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
|
|||
}
|
||||
|
||||
// Before a new statement there is "[{};()=[]" or ::
|
||||
if (! Token::Match(ftok, "{|}|;|(|)|=|[|::"))
|
||||
// We can also have a new statement after any operators or comparisons
|
||||
if (! Token::Match(ftok, "%op%|%comp%|{|}|;|(|)|=|[|::"))
|
||||
continue;
|
||||
|
||||
// If assignment comes after an && or || this is really inconclusive because of short circuiting
|
||||
if (Token::Match(ftok, "%oror%|&&"))
|
||||
continue;
|
||||
|
||||
if (Token::simpleMatch(ftok, "( !"))
|
||||
|
|
|
@ -186,6 +186,10 @@ private:
|
|||
TEST_CASE(constructors_crash1); // ticket #5641
|
||||
TEST_CASE(classWithOperatorInName);// ticket #2827
|
||||
TEST_CASE(templateConstructor); // ticket #7942
|
||||
|
||||
TEST_CASE(uninitAssignmentWithOperator); // ticket #7429
|
||||
TEST_CASE(uninitCompoundAssignment); // ticket #7429
|
||||
TEST_CASE(uninitComparisonAssignment); // ticket #7429
|
||||
}
|
||||
|
||||
|
||||
|
@ -3361,6 +3365,314 @@ private:
|
|||
"Containter<int> intContainer;");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void uninitAssignmentWithOperator() {
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" bool b = false;\n"
|
||||
" b = b && SetValue();\n"
|
||||
" }\n"
|
||||
" bool SetValue() {\n"
|
||||
" x = 1;\n"
|
||||
" return true;\n"
|
||||
" }\n"
|
||||
"};", true);
|
||||
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Member variable 'C::x' is not initialized in the constructor.\n",
|
||||
"[test.cpp:3]: (warning) Member variable 'C::x' is not initialized in the constructor.\n", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" bool b = false;\n"
|
||||
" b = true || SetValue();\n"
|
||||
" }\n"
|
||||
" bool SetValue() {\n"
|
||||
" x = 1;\n"
|
||||
" return true;\n"
|
||||
" }\n"
|
||||
"};", true);
|
||||
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Member variable 'C::x' is not initialized in the constructor.\n",
|
||||
"[test.cpp:3]: (warning) Member variable 'C::x' is not initialized in the constructor.\n", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" bool b = true;\n"
|
||||
" b = b & SetValue();\n"
|
||||
" }\n"
|
||||
" bool SetValue() {\n"
|
||||
" x = 1;\n"
|
||||
" return true;\n"
|
||||
" }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" bool b = false;\n"
|
||||
" b = true | SetValue();\n"
|
||||
" }\n"
|
||||
" bool SetValue() {\n"
|
||||
" x = 1;\n"
|
||||
" return true;\n"
|
||||
" }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i = i * SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i = i / SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i = i % SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i = i + SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i = i - SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i = i << SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i = i >> SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i = i ^ SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void uninitCompoundAssignment() {
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" bool b = true;\n"
|
||||
" b &= SetValue();\n"
|
||||
" }\n"
|
||||
" bool SetValue() {\n"
|
||||
" x = 1;\n"
|
||||
" return true;\n"
|
||||
" }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" bool b = false;\n"
|
||||
" b |= SetValue();\n"
|
||||
" }\n"
|
||||
" bool SetValue() {\n"
|
||||
" x = 1;\n"
|
||||
" return true;\n"
|
||||
" }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i *= SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i /= SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i %= SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i += SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i -= SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i <<= SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i >>= SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" int i = 0;\n"
|
||||
" i ^= SetValue();\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void uninitComparisonAssignment() {
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" bool b = true;\n"
|
||||
" b = (true == SetValue());\n"
|
||||
" }\n"
|
||||
" bool SetValue() {\n"
|
||||
" x = 1;\n"
|
||||
" return true;\n"
|
||||
" }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" bool b = false;\n"
|
||||
" b |= (true != SetValue());\n"
|
||||
" }\n"
|
||||
" bool SetValue() {\n"
|
||||
" x = 1;\n"
|
||||
" return true;\n"
|
||||
" }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" bool b = (0 < SetValue());\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" bool b = (0 <= SetValue());\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" bool b = (0 > SetValue());\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("struct C {\n"
|
||||
" int x;\n"
|
||||
" C() {\n"
|
||||
" bool b = (0 >= SetValue());\n"
|
||||
" }\n"
|
||||
" int SetValue() { return x = 1; }\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
};
|
||||
|
||||
REGISTER_TEST(TestConstructors)
|
||||
|
|
Loading…
Reference in New Issue