Skip to content

Commit 306f1a2

Browse files
Fix #15037 FN redundantInitialization with cast (#8854)
1 parent f939d7b commit 306f1a2

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎lib/checkother.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ void CheckOtherImpl::checkRedundantAssignment()
633633
if (Token::Match(rhs, ":: %name%") && rhs->hasKnownIntValue())
634634
return ChildrenToVisit::none;
635635
if (rhs->isCast())
636-
return ChildrenToVisit::op2;
636+
return rhs->astOperand2() ? ChildrenToVisit::op2 : ChildrenToVisit::op1;
637637
trivial = false;
638638
return ChildrenToVisit::done;
639639
});

‎test/testother.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11502,6 +11502,23 @@ class TestOther : public TestFixture {
1150211502
" return i;\n"
1150311503
"}\n");
1150411504
ASSERT_EQUALS("", errout_str());
11505+
11506+
check("int f(char c) {\n" // #15037
11507+
" int i = (int)c;\n"
11508+
" i = 3;\n"
11509+
" return i;\n"
11510+
"}\n"
11511+
"int g(char c) {\n"
11512+
" int i = static_cast<int>(c);\n"
11513+
" i = 3;\n"
11514+
" return i;\n"
11515+
"}\n");
11516+
ASSERT_EQUALS("[test.cpp:3:7]: style: Redundant initialization for 'i'. The initialized value is overwritten before it is read. [redundantInitialization]\n"
11517+
"[test.cpp:2:11]: note: i is initialized\n"
11518+
"[test.cpp:3:7]: note: i is overwritten\n"
11519+
"[test.cpp:8:8]: style: Redundant initialization for 'i'. The initialized value is overwritten before it is read. [redundantInitialization]\n"
11520+
"[test.cpp:7:12]: note: i is initialized\n"
11521+
"[test.cpp:8:8]: note: i is overwritten\n", errout_str());
1150511522
}
1150611523

1150711524
// cppcheck-suppress unusedPrivateFunction

0 commit comments

Comments
 (0)