Skip to content

Commit b58562f

Browse files
stbrownedanmar
authored andcommitted
Added test cases for catching when assignment comes after an operator, compound assignment, or comparison for ticket danmar#7429 and fixed the tests.
1 parent 18adb97 commit b58562f

File tree

2 files changed

+318
-1
lines changed

2 files changed

+318
-1
lines changed

lib/checkclass.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,12 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
565565
}
566566

567567
// Before a new statement there is "[{};()=[]" or ::
568-
if (! Token::Match(ftok, "{|}|;|(|)|=|[|::"))
568+
// We can also have a new statement after any operators or comparisons
569+
if (! Token::Match(ftok, "%op%|%comp%|{|}|;|(|)|=|[|::"))
570+
continue;
571+
572+
// If assignment comes after an && or || this is really inconclusive because of short circuiting
573+
if (Token::Match(ftok, "%oror%|&&"))
569574
continue;
570575

571576
if (Token::simpleMatch(ftok, "( !"))

test/testconstructors.cpp

Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ class TestConstructors : public TestFixture {
186186
TEST_CASE(constructors_crash1); // ticket #5641
187187
TEST_CASE(classWithOperatorInName);// ticket #2827
188188
TEST_CASE(templateConstructor); // ticket #7942
189+
190+
TEST_CASE(uninitAssignmentWithOperator); // ticket #7429
191+
TEST_CASE(uninitCompoundAssignment); // ticket #7429
192+
TEST_CASE(uninitComparisonAssignment); // ticket #7429
189193
}
190194

191195

@@ -3361,6 +3365,314 @@ class TestConstructors : public TestFixture {
33613365
"Containter<int> intContainer;");
33623366
ASSERT_EQUALS("", errout.str());
33633367
}
3368+
3369+
void uninitAssignmentWithOperator() {
3370+
check("struct C {\n"
3371+
" int x;\n"
3372+
" C() {\n"
3373+
" bool b = false;\n"
3374+
" b = b && SetValue();\n"
3375+
" }\n"
3376+
" bool SetValue() {\n"
3377+
" x = 1;\n"
3378+
" return true;\n"
3379+
" }\n"
3380+
"};", true);
3381+
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Member variable 'C::x' is not initialized in the constructor.\n",
3382+
"[test.cpp:3]: (warning) Member variable 'C::x' is not initialized in the constructor.\n", errout.str());
3383+
3384+
check("struct C {\n"
3385+
" int x;\n"
3386+
" C() {\n"
3387+
" bool b = false;\n"
3388+
" b = true || SetValue();\n"
3389+
" }\n"
3390+
" bool SetValue() {\n"
3391+
" x = 1;\n"
3392+
" return true;\n"
3393+
" }\n"
3394+
"};", true);
3395+
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Member variable 'C::x' is not initialized in the constructor.\n",
3396+
"[test.cpp:3]: (warning) Member variable 'C::x' is not initialized in the constructor.\n", errout.str());
3397+
3398+
check("struct C {\n"
3399+
" int x;\n"
3400+
" C() {\n"
3401+
" bool b = true;\n"
3402+
" b = b & SetValue();\n"
3403+
" }\n"
3404+
" bool SetValue() {\n"
3405+
" x = 1;\n"
3406+
" return true;\n"
3407+
" }\n"
3408+
"};");
3409+
ASSERT_EQUALS("", errout.str());
3410+
3411+
check("struct C {\n"
3412+
" int x;\n"
3413+
" C() {\n"
3414+
" bool b = false;\n"
3415+
" b = true | SetValue();\n"
3416+
" }\n"
3417+
" bool SetValue() {\n"
3418+
" x = 1;\n"
3419+
" return true;\n"
3420+
" }\n"
3421+
"};");
3422+
ASSERT_EQUALS("", errout.str());
3423+
3424+
check("struct C {\n"
3425+
" int x;\n"
3426+
" C() {\n"
3427+
" int i = 0;\n"
3428+
" i = i * SetValue();\n"
3429+
" }\n"
3430+
" int SetValue() { return x = 1; }\n"
3431+
"};");
3432+
ASSERT_EQUALS("", errout.str());
3433+
3434+
check("struct C {\n"
3435+
" int x;\n"
3436+
" C() {\n"
3437+
" int i = 0;\n"
3438+
" i = i / SetValue();\n"
3439+
" }\n"
3440+
" int SetValue() { return x = 1; }\n"
3441+
"};");
3442+
ASSERT_EQUALS("", errout.str());
3443+
3444+
check("struct C {\n"
3445+
" int x;\n"
3446+
" C() {\n"
3447+
" int i = 0;\n"
3448+
" i = i % SetValue();\n"
3449+
" }\n"
3450+
" int SetValue() { return x = 1; }\n"
3451+
"};");
3452+
ASSERT_EQUALS("", errout.str());
3453+
3454+
check("struct C {\n"
3455+
" int x;\n"
3456+
" C() {\n"
3457+
" int i = 0;\n"
3458+
" i = i + SetValue();\n"
3459+
" }\n"
3460+
" int SetValue() { return x = 1; }\n"
3461+
"};");
3462+
ASSERT_EQUALS("", errout.str());
3463+
3464+
check("struct C {\n"
3465+
" int x;\n"
3466+
" C() {\n"
3467+
" int i = 0;\n"
3468+
" i = i - SetValue();\n"
3469+
" }\n"
3470+
" int SetValue() { return x = 1; }\n"
3471+
"};");
3472+
ASSERT_EQUALS("", errout.str());
3473+
3474+
check("struct C {\n"
3475+
" int x;\n"
3476+
" C() {\n"
3477+
" int i = 0;\n"
3478+
" i = i << SetValue();\n"
3479+
" }\n"
3480+
" int SetValue() { return x = 1; }\n"
3481+
"};");
3482+
ASSERT_EQUALS("", errout.str());
3483+
3484+
check("struct C {\n"
3485+
" int x;\n"
3486+
" C() {\n"
3487+
" int i = 0;\n"
3488+
" i = i >> SetValue();\n"
3489+
" }\n"
3490+
" int SetValue() { return x = 1; }\n"
3491+
"};");
3492+
ASSERT_EQUALS("", errout.str());
3493+
3494+
check("struct C {\n"
3495+
" int x;\n"
3496+
" C() {\n"
3497+
" int i = 0;\n"
3498+
" i = i ^ SetValue();\n"
3499+
" }\n"
3500+
" int SetValue() { return x = 1; }\n"
3501+
"};");
3502+
ASSERT_EQUALS("", errout.str());
3503+
}
3504+
3505+
void uninitCompoundAssignment() {
3506+
check("struct C {\n"
3507+
" int x;\n"
3508+
" C() {\n"
3509+
" bool b = true;\n"
3510+
" b &= SetValue();\n"
3511+
" }\n"
3512+
" bool SetValue() {\n"
3513+
" x = 1;\n"
3514+
" return true;\n"
3515+
" }\n"
3516+
"};");
3517+
ASSERT_EQUALS("", errout.str());
3518+
3519+
check("struct C {\n"
3520+
" int x;\n"
3521+
" C() {\n"
3522+
" bool b = false;\n"
3523+
" b |= SetValue();\n"
3524+
" }\n"
3525+
" bool SetValue() {\n"
3526+
" x = 1;\n"
3527+
" return true;\n"
3528+
" }\n"
3529+
"};");
3530+
ASSERT_EQUALS("", errout.str());
3531+
3532+
check("struct C {\n"
3533+
" int x;\n"
3534+
" C() {\n"
3535+
" int i = 0;\n"
3536+
" i *= SetValue();\n"
3537+
" }\n"
3538+
" int SetValue() { return x = 1; }\n"
3539+
"};");
3540+
ASSERT_EQUALS("", errout.str());
3541+
3542+
check("struct C {\n"
3543+
" int x;\n"
3544+
" C() {\n"
3545+
" int i = 0;\n"
3546+
" i /= SetValue();\n"
3547+
" }\n"
3548+
" int SetValue() { return x = 1; }\n"
3549+
"};");
3550+
ASSERT_EQUALS("", errout.str());
3551+
3552+
check("struct C {\n"
3553+
" int x;\n"
3554+
" C() {\n"
3555+
" int i = 0;\n"
3556+
" i %= SetValue();\n"
3557+
" }\n"
3558+
" int SetValue() { return x = 1; }\n"
3559+
"};");
3560+
ASSERT_EQUALS("", errout.str());
3561+
3562+
check("struct C {\n"
3563+
" int x;\n"
3564+
" C() {\n"
3565+
" int i = 0;\n"
3566+
" i += SetValue();\n"
3567+
" }\n"
3568+
" int SetValue() { return x = 1; }\n"
3569+
"};");
3570+
ASSERT_EQUALS("", errout.str());
3571+
3572+
check("struct C {\n"
3573+
" int x;\n"
3574+
" C() {\n"
3575+
" int i = 0;\n"
3576+
" i -= SetValue();\n"
3577+
" }\n"
3578+
" int SetValue() { return x = 1; }\n"
3579+
"};");
3580+
ASSERT_EQUALS("", errout.str());
3581+
3582+
check("struct C {\n"
3583+
" int x;\n"
3584+
" C() {\n"
3585+
" int i = 0;\n"
3586+
" i <<= SetValue();\n"
3587+
" }\n"
3588+
" int SetValue() { return x = 1; }\n"
3589+
"};");
3590+
ASSERT_EQUALS("", errout.str());
3591+
3592+
check("struct C {\n"
3593+
" int x;\n"
3594+
" C() {\n"
3595+
" int i = 0;\n"
3596+
" i >>= SetValue();\n"
3597+
" }\n"
3598+
" int SetValue() { return x = 1; }\n"
3599+
"};");
3600+
ASSERT_EQUALS("", errout.str());
3601+
3602+
check("struct C {\n"
3603+
" int x;\n"
3604+
" C() {\n"
3605+
" int i = 0;\n"
3606+
" i ^= SetValue();\n"
3607+
" }\n"
3608+
" int SetValue() { return x = 1; }\n"
3609+
"};");
3610+
ASSERT_EQUALS("", errout.str());
3611+
}
3612+
3613+
void uninitComparisonAssignment() {
3614+
check("struct C {\n"
3615+
" int x;\n"
3616+
" C() {\n"
3617+
" bool b = true;\n"
3618+
" b = (true == SetValue());\n"
3619+
" }\n"
3620+
" bool SetValue() {\n"
3621+
" x = 1;\n"
3622+
" return true;\n"
3623+
" }\n"
3624+
"};");
3625+
ASSERT_EQUALS("", errout.str());
3626+
3627+
check("struct C {\n"
3628+
" int x;\n"
3629+
" C() {\n"
3630+
" bool b = false;\n"
3631+
" b |= (true != SetValue());\n"
3632+
" }\n"
3633+
" bool SetValue() {\n"
3634+
" x = 1;\n"
3635+
" return true;\n"
3636+
" }\n"
3637+
"};");
3638+
ASSERT_EQUALS("", errout.str());
3639+
3640+
check("struct C {\n"
3641+
" int x;\n"
3642+
" C() {\n"
3643+
" bool b = (0 < SetValue());\n"
3644+
" }\n"
3645+
" int SetValue() { return x = 1; }\n"
3646+
"};");
3647+
ASSERT_EQUALS("", errout.str());
3648+
3649+
check("struct C {\n"
3650+
" int x;\n"
3651+
" C() {\n"
3652+
" bool b = (0 <= SetValue());\n"
3653+
" }\n"
3654+
" int SetValue() { return x = 1; }\n"
3655+
"};");
3656+
ASSERT_EQUALS("", errout.str());
3657+
3658+
check("struct C {\n"
3659+
" int x;\n"
3660+
" C() {\n"
3661+
" bool b = (0 > SetValue());\n"
3662+
" }\n"
3663+
" int SetValue() { return x = 1; }\n"
3664+
"};");
3665+
ASSERT_EQUALS("", errout.str());
3666+
3667+
check("struct C {\n"
3668+
" int x;\n"
3669+
" C() {\n"
3670+
" bool b = (0 >= SetValue());\n"
3671+
" }\n"
3672+
" int SetValue() { return x = 1; }\n"
3673+
"};");
3674+
ASSERT_EQUALS("", errout.str());
3675+
}
33643676
};
33653677

33663678
REGISTER_TEST(TestConstructors)

0 commit comments

Comments
 (0)