Skip to content

Commit c50ae02

Browse files
committed
Fix #14371: FP redundantAssignment for nested union members
Reading another member of the same union in a subexpression of the rhs is a use of the previous value through aliasing, so the assignment is not redundant. Check the whole rhs expression for the union variable instead of only the leading member-access chain.
1 parent 678290c commit c50ae02

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

lib/fwdanalysis.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token *
261261
parent = parent->astParent();
262262
if (parent && parent->valueType() && Token::simpleMatch(parent->astParent(), "=") && !Token::Match(parent->astParent()->astParent(), "%assign%") && parent->astParent()->astOperand1() == parent) {
263263
const Token * assignment = parent->astParent()->astOperand2();
264+
if (hasOperand(assignment, expr))
265+
return Result(Result::Type::READ);
264266
while (Token::simpleMatch(assignment, ".") && assignment->varId() != expr->varId())
265267
assignment = assignment->astOperand1();
266268
if (assignment && assignment->varId() != expr->varId()) {

test/testother.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11063,6 +11063,23 @@ class TestOther : public TestFixture {
1106311063
" Dst.s->y = Src.s->y;\n"
1106411064
"}\n");
1106511065
ASSERT_EQUALS("", errout_str());
11066+
11067+
// Ticket #14371 "redundantAssignment when using a union"
11068+
check("union U {\n"
11069+
" struct {\n"
11070+
" unsigned int abcd;\n"
11071+
" } u32;\n"
11072+
" struct {\n"
11073+
" unsigned short ab;\n"
11074+
" unsigned short cd;\n"
11075+
" } u16;\n"
11076+
"};\n"
11077+
"void f() {\n"
11078+
" U m;\n"
11079+
" m.u32.abcd = 1234;\n"
11080+
" m.u32.abcd = 5 * m.u16.ab;\n"
11081+
"}\n", dinit(CheckOptions, $.inconclusive = false));
11082+
ASSERT_EQUALS("", errout_str());
1106611083
}
1106711084

1106811085
void redundantVarAssignment_7133() {

0 commit comments

Comments
 (0)