[cfe-dev] [enum comparison] switch vs if

Daniel Marjamäki via cfe-dev cfe-dev at lists.llvm.org
Fri Apr 28 04:59:53 PDT 2017


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:                
Daniel.Marjamaki at evidente.se     

www.evidente.se


From: cfe-dev [cfe-dev-bounces at lists.llvm.org] on behalf of alex boros via cfe-dev [cfe-dev at lists.llvm.org]
Sent: 27 April 2017 15:54
To: cfe-dev at lists.llvm.org
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.



More information about the cfe-dev mailing list