@@ -204,6 +204,7 @@ class TestOther : public TestFixture {
204204 TEST_CASE(redundantVarAssignment);
205205 TEST_CASE(redundantVarAssignment_trivial);
206206 TEST_CASE(redundantVarAssignment_struct);
207+ TEST_CASE(redundantVarAssignment_union);
207208 TEST_CASE(redundantVarAssignment_7133);
208209 TEST_CASE(redundantVarAssignment_stackoverflow);
209210 TEST_CASE(redundantVarAssignment_lambda);
@@ -4492,96 +4493,6 @@ class TestOther : public TestFixture {
44924493 " }\n"
44934494 "}");
44944495 ASSERT_EQUALS("", errout_str());
4495-
4496- // Ticket #5115 "redundantAssignment when using a union"
4497- check("void main(void)\n"
4498- "{\n"
4499- " short lTotal = 0;\n"
4500- " union\n"
4501- " {\n"
4502- " short l1;\n"
4503- " struct\n"
4504- " {\n"
4505- " unsigned char b1;\n"
4506- " unsigned char b2;\n"
4507- " } b;\n"
4508- " } u;\n"
4509- " u.l1 = 1;\n"
4510- " lTotal += u.b.b1;\n"
4511- " u.l1 = 2;\n" //Should not show RedundantAssignment
4512- "}", true, false, false);
4513- ASSERT_EQUALS("", errout_str());
4514-
4515- // Ticket #5115 "redundantAssignment when using a union"
4516- check("void main(void)\n"
4517- "{\n"
4518- " short lTotal = 0;\n"
4519- " union\n"
4520- " {\n"
4521- " short l1;\n"
4522- " struct\n"
4523- " {\n"
4524- " unsigned char b1;\n"
4525- " unsigned char b2;\n"
4526- " } b;\n"
4527- " } u;\n"
4528- " u.l1 = 1;\n"
4529- " u.l1 = 2;\n"
4530- "}", true, false, false);
4531- ASSERT_EQUALS("[test.cpp:13] -> [test.cpp:14]: (style) Variable 'u.l1' is reassigned a value before the old one has been used.\n", errout_str());
4532-
4533- // Ticket #10093 "redundantAssignment when using a union"
4534- check("typedef union fixed32_union {\n"
4535- " struct {\n"
4536- " unsigned32 abcd;\n"
4537- " } u32;\n"
4538- " struct {\n"
4539- " unsigned16 ab;\n"
4540- " unsigned16 cd;\n"
4541- " } u16;"
4542- " struct {\n"
4543- " unsigned8 a;\n"
4544- " unsigned8 b;\n"
4545- " unsigned8 c;\n"
4546- " unsigned8 d;\n"
4547- " } b;\n"
4548- "} fixed32;\n"
4549- "void func1(void) {\n"
4550- " fixed32 m;\n"
4551- " m.u16.ab = 47;\n"
4552- " m.u16.cd = 0;\n"
4553- " m.u16.ab = m.u32.abcd / 53;\n"
4554- "}", true, false, false);
4555- ASSERT_EQUALS("", errout_str());
4556-
4557- // Ticket #10093 "redundantAssignment when using a union"
4558- check("typedef union{\n"
4559- " char as_char[4];\n"
4560- " int as_int;\n"
4561- "} union_t;\n"
4562- "void fn(char *data, int len) {\n"
4563- " int i;\n"
4564- " for (i = 0; i < len; i++)\n"
4565- " data[i] = 'a';\n"
4566- "}\n"
4567- "int main(int argc, char *argv[]) {\n"
4568- " union_t u;\n"
4569- " u.as_int = 42;\n"
4570- " fn(&u.as_char[0], 4);\n"
4571- " u.as_int = 0;\n"
4572- "}", true, false, false);
4573- ASSERT_EQUALS("", errout_str());
4574-
4575- // Ticket #5115 "redundantAssignment when using a union"
4576- check("void foo(char *ptr) {\n"
4577- " union {\n"
4578- " char * s8;\n"
4579- " unsigned long long u64;\n"
4580- " } addr;\n"
4581- " addr.s8 = ptr;\n"
4582- " addr.u64 += 8;\n"
4583- "}", true, false, false);
4584- ASSERT_EQUALS("", errout_str());
45854496 }
45864497
45874498 void switchRedundantOperationTest() {
@@ -9790,6 +9701,110 @@ class TestOther : public TestFixture {
97909701 ASSERT_EQUALS("", errout_str());
97919702 }
97929703
9704+ void redundantVarAssignment_union() {
9705+ // Ticket #5115 "redundantAssignment when using a union"
9706+ check("void main(void)\n"
9707+ "{\n"
9708+ " short lTotal = 0;\n"
9709+ " union\n"
9710+ " {\n"
9711+ " short l1;\n"
9712+ " struct\n"
9713+ " {\n"
9714+ " unsigned char b1;\n"
9715+ " unsigned char b2;\n"
9716+ " } b;\n"
9717+ " } u;\n"
9718+ " u.l1 = 1;\n"
9719+ " lTotal += u.b.b1;\n"
9720+ " u.l1 = 2;\n" //Should not show RedundantAssignment
9721+ "}", true, false, false);
9722+ ASSERT_EQUALS("", errout_str());
9723+
9724+ // Ticket #5115 "redundantAssignment when using a union"
9725+ check("void main(void)\n"
9726+ "{\n"
9727+ " short lTotal = 0;\n"
9728+ " union\n"
9729+ " {\n"
9730+ " short l1;\n"
9731+ " struct\n"
9732+ " {\n"
9733+ " unsigned char b1;\n"
9734+ " unsigned char b2;\n"
9735+ " } b;\n"
9736+ " } u;\n"
9737+ " u.l1 = 1;\n"
9738+ " u.l1 = 2;\n"
9739+ "}", true, false, false);
9740+ ASSERT_EQUALS("[test.cpp:13] -> [test.cpp:14]: (style) Variable 'u.l1' is reassigned a value before the old one has been used.\n", errout_str());
9741+
9742+ // Ticket #10093 "redundantAssignment when using a union"
9743+ check("typedef union fixed32_union {\n"
9744+ " struct {\n"
9745+ " unsigned32 abcd;\n"
9746+ " } u32;\n"
9747+ " struct {\n"
9748+ " unsigned16 ab;\n"
9749+ " unsigned16 cd;\n"
9750+ " } u16;"
9751+ " struct {\n"
9752+ " unsigned8 a;\n"
9753+ " unsigned8 b;\n"
9754+ " unsigned8 c;\n"
9755+ " unsigned8 d;\n"
9756+ " } b;\n"
9757+ "} fixed32;\n"
9758+ "void func1(void) {\n"
9759+ " fixed32 m;\n"
9760+ " m.u16.ab = 47;\n"
9761+ " m.u16.cd = 0;\n"
9762+ " m.u16.ab = m.u32.abcd / 53;\n"
9763+ "}", true, false, false);
9764+ ASSERT_EQUALS("", errout_str());
9765+
9766+ // Ticket #10093 "redundantAssignment when using a union"
9767+ check("typedef union{\n"
9768+ " char as_char[4];\n"
9769+ " int as_int;\n"
9770+ "} union_t;\n"
9771+ "void fn(char *data, int len) {\n"
9772+ " int i;\n"
9773+ " for (i = 0; i < len; i++)\n"
9774+ " data[i] = 'a';\n"
9775+ "}\n"
9776+ "int main(int argc, char *argv[]) {\n"
9777+ " union_t u;\n"
9778+ " u.as_int = 42;\n"
9779+ " fn(&u.as_char[0], 4);\n"
9780+ " u.as_int = 0;\n"
9781+ "}", true, false, false);
9782+ ASSERT_EQUALS("", errout_str());
9783+
9784+ // Ticket #5115 "redundantAssignment when using a union"
9785+ check("void foo(char *ptr) {\n"
9786+ " union {\n"
9787+ " char * s8;\n"
9788+ " unsigned long long u64;\n"
9789+ " } addr;\n"
9790+ " addr.s8 = ptr;\n"
9791+ " addr.u64 += 8;\n"
9792+ "}", true, false, false);
9793+ ASSERT_EQUALS("", errout_str());
9794+
9795+ check("struct S {\n" // #12895
9796+ " int x, y;\n"
9797+ "};\n"
9798+ "union U {\n"
9799+ " S* s;\n"
9800+ "};\n"
9801+ "void f(const U& Src, const U& Dst) {\n"
9802+ " Dst.s->x = Src.s->x;\n"
9803+ " Dst.s->y = Src.s->y;\n"
9804+ "}\n");
9805+ ASSERT_EQUALS("", errout_str());
9806+ }
9807+
97939808 void redundantVarAssignment_7133() {
97949809 // #7133
97959810 check("sal_Int32 impl_Export() {\n"
0 commit comments