3

Like the title says, for those who already know starting with GCC-6 you can catch a duplicate in an ifstatement using this Flag -Wduplicated-cond like this:

#include <stdio.h>

int main(void){
    int a = 5;

    if( a == 5){
        printf("First  condition is True, A = %d\n", a);
    }else if( a == 5 ){
        printf("Second condition is True, A = %d\n", a);
    }
}

And the Output will be:

program.c:8:17: warning: duplicated ‘if’ condition [-Wduplicated-cond]
     }else if( a == 5 ){
               ~~^~~~
program.c:6:11: note: previously used here
     if( a == 5){
         ~~^~~~

Now I know that the following:

else if( (a > 4) && (a < 6) )

is not the same like

else if( a == 5 )

but there happens that I do a check for the same condition if a == 5.

My Question is, is there any chances to can catch (to avoid) this kind of duplicate?

Michi
  • 5,175
  • 7
  • 33
  • 58
  • 1
    I doubt it will get catched...the analysis is too complex for such a minor thing. – Eugene Sh. May 17 '16 at 18:01
  • I think that `if( a == 5)` and `else if( a == 5 )` produces the same instructions and `else if( (a > 4) && (a < 6) )` produces different instruction[s] which probably means that the compiler checks if the same instructions are taking place and not if the conditions are taking(produces) the same result. I'm not sure if I'm right. – Michi May 17 '16 at 18:07
  • Michi,open your eyes well while you are coding. – machine_1 May 17 '16 at 18:11
  • In this specific case `(a > 4) && (a < 6)` is the same as `(a == 5)` but that' s only true for `int` (and variations). – fpg1503 May 17 '16 at 18:12
  • @machine_1 I do this all the time, but even so I find `-Wduplicated-cond` a good thing – Michi May 17 '16 at 18:12
  • @fpg1503 You mean that produces the same result, or you mean that the Compiler will use the same Instructions? – Michi May 17 '16 at 18:14
  • @Michi I mean produces the same result, the generated instructions on GCC are completely different. The compiler is able to get that case: if you take a look at the generated assembly code using any optimization higher than `-O2` the second if branch is completely removed. – fpg1503 May 17 '16 at 18:31
  • @fpg1503 Yes, is true, but even with `-O3` the warning will still be there. – Michi May 17 '16 at 18:34
  • @Michi IMHO under `-O2` or `O3` there should be `-Wunreachable-code` or `-Wduplicated-cond` (probably both) in this case. I thinks it's a GCC bug. – fpg1503 May 17 '16 at 18:49
  • @fpg1503 It's not a bug but intended feature. See my answer. – uh oh somebody needs a pupper May 18 '16 at 07:23

1 Answers1

-1

-Wduplicated-cond does catch this case. You have to explicitly enable it.

main.cpp: In function 'int main()':

main.cpp:8:11: warning: duplicated 'if' condition [-Wduplicated-cond]

 }else if( a == 5 ){
       ^~

main.cpp:6:5: note: previously used here

 if( a == 5){

It was originally enabled by -Wall but not anymore because of PR67819:

Given that the new warning currently breaks bootstrap [*] and I see no simple way how to resolve PR67819 derived out of the bootstrap failure, I'm moving -Wduplicated-cond out of -Wall and skipping one of the tests for the time being. I'm also reverting the hack in genemit.c.

  • The OP is talking about duplicate detection when using different statements that are logically equivalent (i.e. `a == 5` and `a > 4 && a < 6`). – fpg1503 May 18 '16 at 12:11