From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 853EF3861035; Tue, 13 Oct 2020 07:30:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 853EF3861035 From: "Ulrich.Windl at rz dot uni-regensburg.de" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/97398] New: Enhancement request: Warning when multiply assigning to same struct field Date: Tue, 13 Oct 2020 07:30:44 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 4.8.5 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: Ulrich.Windl at rz dot uni-regensburg.de X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Oct 2020 07:30:44 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D97398 Bug ID: 97398 Summary: Enhancement request: Warning when multiply assigning to same struct field Product: gcc Version: 4.8.5 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: Ulrich.Windl at rz dot uni-regensburg.de Target Milestone: --- Created attachment 49360 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D49360&action=3Dedit Simple test case that doesn't trigger any warning with gcc 7.5.0 and "-O -W= all" After having found some minor mistake in my C code for "gcc -O2 -Wall" (gcc 4.8.5), I verified that gcc 7.5.0 reacts similar in a test case. I had (re-)initialized several fields of a structure in a function that got= the pointer to the struct. The code looks like this (S is the pointer to the struct, and the real code= is way more complex): s->a =3D s->b =3D s->c; s->d =3D s->e =3D s->c; ... s->f =3D s->e =3D s->c ... So s->e is set to s->c multiple times, and gcc does not warn about that. In this case s->c is even set to the same value. To help avoiding this type of mistake, I suggest that gcc should warn when a structure field is set more then once without reading the value before re-setting it. Note: In the simple test program gcc optimizes away everything, but I still think that it could warn about the mistake. At least you should get the id= ea.=