public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/114763] New: Wduplicated-branches just check last else if-else case?
@ 2024-04-18  7:47 hanwei62 at huawei dot com
  2024-04-18  8:16 ` [Bug c/114763] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: hanwei62 at huawei dot com @ 2024-04-18  7:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114763

            Bug ID: 114763
           Summary: Wduplicated-branches just check last else if-else
                    case?
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hanwei62 at huawei dot com
  Target Milestone: ---

This code:

void foo(int x) {
    if (x) {
        x -= 2;
    } else if (x > 50) {
        x -= 2;
    } else
        x += 2;
}

# gcc -c test.c -Wduplicated-branches

not warning, even if-else if branches is same.

void foo(int x) {
    if (x) {
        x -= 2;
    } else if (x > 50) {
        x -= 2;
    } else if (x < 20) {
        x += 2;
    } else {
        x += 2;
    }
}

warning.

Same like https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85094

What I find that Wduplicated-branches just check the last `else if-else`
branches.

https://godbolt.org/z/WP8v5P4Yf

It's misleading.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/114763] Wduplicated-branches just check last else if-else case?
  2024-04-18  7:47 [Bug c/114763] New: Wduplicated-branches just check last else if-else case? hanwei62 at huawei dot com
@ 2024-04-18  8:16 ` pinskia at gcc dot gnu.org
  2024-04-18  8:17 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-18  8:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114763

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note only the second case has if's 2 sides which are the same;
it is basically `a ? b : (c ? d : d)`.

While the first case you have `a ? b : (c ? d : b)` which is not supposed to
warn about at all because the 2 sides are not the same.

The reason is because that is how the grammar of C/C++ works.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/114763] Wduplicated-branches just check last else if-else case?
  2024-04-18  7:47 [Bug c/114763] New: Wduplicated-branches just check last else if-else case? hanwei62 at huawei dot com
  2024-04-18  8:16 ` [Bug c/114763] " pinskia at gcc dot gnu.org
@ 2024-04-18  8:17 ` pinskia at gcc dot gnu.org
  2024-04-18  8:31 ` hanwei62 at huawei dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-18  8:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114763

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/114763] Wduplicated-branches just check last else if-else case?
  2024-04-18  7:47 [Bug c/114763] New: Wduplicated-branches just check last else if-else case? hanwei62 at huawei dot com
  2024-04-18  8:16 ` [Bug c/114763] " pinskia at gcc dot gnu.org
  2024-04-18  8:17 ` pinskia at gcc dot gnu.org
@ 2024-04-18  8:31 ` hanwei62 at huawei dot com
  2024-04-18  8:32 ` hanwei62 at huawei dot com
  2024-04-18  8:45 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: hanwei62 at huawei dot com @ 2024-04-18  8:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114763

--- Comment #2 from hanwei (K) <hanwei62 at huawei dot com> ---
(In reply to Andrew Pinski from comment #1)
> Note only the second case has if's 2 sides which are the same;
> it is basically `a ? b : (c ? d : d)`.
> 
> While the first case you have `a ? b : (c ? d : b)` which is not supposed to
> warn about at all because the 2 sides are not the same.
> 
> The reason is because that is how the grammar of C/C++ works.

So, Wduplicated-branches just check bottom two branches actually.
Because only the bottom two branches will same if we check followed the grammar
of C/C++.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/114763] Wduplicated-branches just check last else if-else case?
  2024-04-18  7:47 [Bug c/114763] New: Wduplicated-branches just check last else if-else case? hanwei62 at huawei dot com
                   ` (2 preceding siblings ...)
  2024-04-18  8:31 ` hanwei62 at huawei dot com
@ 2024-04-18  8:32 ` hanwei62 at huawei dot com
  2024-04-18  8:45 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: hanwei62 at huawei dot com @ 2024-04-18  8:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114763

--- Comment #3 from hanwei (K) <hanwei62 at huawei dot com> ---
(In reply to Andrew Pinski from comment #1)
> Note only the second case has if's 2 sides which are the same;
> it is basically `a ? b : (c ? d : d)`.
> 
> While the first case you have `a ? b : (c ? d : b)` which is not supposed to
> warn about at all because the 2 sides are not the same.
> 
> The reason is because that is how the grammar of C/C++ works.

So, Wduplicated-branches just check bottom two branches actually.
Because only the bottom two branches will same if we check followed the grammar
of C/C++.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/114763] Wduplicated-branches just check last else if-else case?
  2024-04-18  7:47 [Bug c/114763] New: Wduplicated-branches just check last else if-else case? hanwei62 at huawei dot com
                   ` (3 preceding siblings ...)
  2024-04-18  8:32 ` hanwei62 at huawei dot com
@ 2024-04-18  8:45 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-04-18  8:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114763

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
-Wduplicate-branches compares the substatements of an if statement.
In C/C++, there is not a statement like if ... elseif ... elseif ... else, just
if statement.
In your first example, there are 2 if statements, one has
{ x -= 2; }
and
if (x > 50) { x -= 2; } else x += 2;
substatements (and those are not the same), and the other has
{ x -= 2; }
and
x += 2;
substatements and those aren't the same either.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-04-18  8:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-18  7:47 [Bug c/114763] New: Wduplicated-branches just check last else if-else case? hanwei62 at huawei dot com
2024-04-18  8:16 ` [Bug c/114763] " pinskia at gcc dot gnu.org
2024-04-18  8:17 ` pinskia at gcc dot gnu.org
2024-04-18  8:31 ` hanwei62 at huawei dot com
2024-04-18  8:32 ` hanwei62 at huawei dot com
2024-04-18  8:45 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).