From 13c64895719fad78ed711a25675b2e8b6a4d1797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 27 Nov 2018 18:22:22 +0100 Subject: [PATCH] Redundant variable assignment: Fix FN for struct members --- lib/checkother.cpp | 3 ++- test/testother.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 9cb66b8e1..dde5c8a6b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -458,7 +458,8 @@ const Token *CheckOther::checkRedundantAssignmentRecursive(const Token *assign1, [&](const Token *tok) { if (tok->varId() > 0) { assign1LhsVarIds.insert(tok->varId()); - local &= !nonLocal(tok->variable()); + if (!Token::simpleMatch(tok->previous(), ".")) + local &= !nonLocal(tok->variable()); } return ChildrenToVisit::op1_and_op2; }); diff --git a/test/testother.cpp b/test/testother.cpp index 0bca64f58..d4d385503 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -159,6 +159,7 @@ private: TEST_CASE(incompleteArrayFill); TEST_CASE(redundantVarAssignment); + TEST_CASE(redundantVarAssignment_struct); TEST_CASE(redundantVarAssignment_7133); TEST_CASE(redundantVarAssignment_stackoverflow); TEST_CASE(redundantVarAssignment_lambda); @@ -5968,6 +5969,19 @@ private: ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) Variable 'memptr' is reassigned a value before the old one has been used.\n", errout.str()); } + void redundantVarAssignment_struct() { + check("struct foo {\n" + " int a,b;\n" + "};\n" + "\n" + "int main() {\n" + " struct foo x;\n" + " x.a = _mm_set1_ps(1.0);\n" + " x.a = _mm_set1_ps(2.0);\n" + "}"); + ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:8]: (style) Variable 'x.a' is reassigned a value before the old one has been used.\n", errout.str()); + } + void redundantVarAssignment_7133() { // #7133 check("sal_Int32 impl_Export() {\n"