diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 78351a229c0..c197dfb451b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -633,7 +633,7 @@ void CheckOtherImpl::checkRedundantAssignment() if (Token::Match(rhs, ":: %name%") && rhs->hasKnownIntValue()) return ChildrenToVisit::none; if (rhs->isCast()) - return ChildrenToVisit::op2; + return rhs->astOperand2() ? ChildrenToVisit::op2 : ChildrenToVisit::op1; trivial = false; return ChildrenToVisit::done; }); diff --git a/test/testother.cpp b/test/testother.cpp index 1c1c8640461..886c19fc0aa 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -11462,6 +11462,23 @@ class TestOther : public TestFixture { " return i;\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("int f(char c) {\n" // #15037 + " int i = (int)c;\n" + " i = 3;\n" + " return i;\n" + "}\n" + "int g(char c) {\n" + " int i = static_cast(c);\n" + " i = 3;\n" + " return i;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3:7]: style: Redundant initialization for 'i'. The initialized value is overwritten before it is read. [redundantInitialization]\n" + "[test.cpp:2:11]: note: i is initialized\n" + "[test.cpp:3:7]: note: i is overwritten\n" + "[test.cpp:8:8]: style: Redundant initialization for 'i'. The initialized value is overwritten before it is read. [redundantInitialization]\n" + "[test.cpp:7:12]: note: i is initialized\n" + "[test.cpp:8:8]: note: i is overwritten\n", errout_str()); } // cppcheck-suppress unusedPrivateFunction