[enum comparison] switch vs if

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[enum comparison] switch vs if

Nat! via cfe-dev
Hi all!

I'm interested in developing a clang static checker for enum comparison in switch and throwing a warning.
I found out that it already exists in if comparison  in clang but my question is:
Is this on purpose that different enum types don't send warning or int and enum comparison in switches but send in if?
Example:
enum MIXED_ENUMS_e { E1, E2 };
enum MIXED_ENUMS_f { F0, F1, F2, F3 };

void mixed_enums(MIXED_ENUMS_e ee)
{
  int x;

  switch (ee)
  {
    case E1: x = 1; break;
    case F1: x = 2; break; // TODO warning: ee and F1 have different types.
  }

  switch (x)
  {
    case E1: ++x; break; // TODO warning: x and E1 have different types.
    case F3: --x; break; // TODO warning: x and F3 have different types.
  }
  if  (ee == F0) {} // Already exsisting warning: ee and F0 have different types.
  if  (ee == x) {} // Already exsisting warning: ee and x have different types.
}
BR Alex.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [enum comparison] switch vs if

Nat! via cfe-dev
Hello!

Sounds great! In my humble opinion that would be a great addition. And I would suggest it's added in Sema as a normal compiler warning (on by default).

Best regards,
Daniel Marjamäki

..................................................................................................................
Daniel Marjamäki
Senior Engineer
Evidente ES East AB
Warfvinges väg 34  SE-112 51 Stockholm  Sweden
 
Mobile:                
+46 (0)709 12 42 62
E-mail:                
[hidden email]    

www.evidente.se


From: cfe-dev [[hidden email]] on behalf of alex boros via cfe-dev [[hidden email]]
Sent: 27 April 2017 15:54
To: [hidden email]
Subject: [cfe-dev] [enum comparison] switch vs if

Hi all!

I'm interested in developing a clang static checker for enum comparison in switch and throwing a warning.

I found out that it already exists in if comparison  in clang but my question is:

Is this on purpose that different enum types don't send warning or int and enum comparison in switches but send in if?

Example:
enum MIXED_ENUMS_e { E1, E2 };
enum MIXED_ENUMS_f { F0, F1, F2, F3 };

void mixed_enums(MIXED_ENUMS_e ee)
{
  int x;

  switch (ee)
  {
    case E1: x = 1; break;
    case F1: x = 2; break; // TODO warning: ee and F1 have different types.
  }

  switch (x)
  {
    case E1: ++x; break; // TODO warning: x and E1 have different types.
    case F3: --x; break; // TODO warning: x and F3 have different types.
  }
  if  (ee == F0) {} // Already exsisting warning: ee and F0 have different types.
  if  (ee == x) {} // Already exsisting warning: ee and x have different types.
}

BR Alex.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev