Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V2022. Implicit type conversion from in…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
Problems related to code analyzer
Additional information
toggle menu Contents

V2022. Implicit type conversion from integer type to enum type.

Dec 03 2024

The diagnostic rule has been added at users' request.

It helps identify implicit type conversions from integer types to enum types. The rule can assist in code refactoring and facilitate bug detection.

The diagnostic rule applies only to the C language.

Below are the examples of constructs for which the analyzer issues warnings.

Example N1:

typedef enum
{
  Horizontal = 0x1,
  Vertical = 0x2
} Orientation;

void foo1()
{
  int pos = 1;
  Orientation orientation = pos;   // V2022
}

In this example, the 'pos' integer value is implicitly converted to a variable of the 'Orientation' type.

Example N2:

Orientation GetOrientation (int pos)
{
  return pos;   // V2022
}

The 'GetOrientation' function returns a value of the 'Orientation' type and implicitly casts the 'pos' value to this type.

Example N3.

Orientation GetOrientation (bool b)
{
  int posOne = 1;
  int posTwo = 2;
  return b ? posOne : posTwo;    // V2022
}

In this example, the function uses the conditional operator (?:) to select between the two integer variables, 'posOne' and 'posTwo', which also results in the implicit type conversion.

Example N4:

void foo2(Orientation o){}

void foo3()
{
  foo2(Horizontal);                 //ok
  foo2(Horizontal | Vertical);      // V2022
}

The 'Horizontal | Vertical' operation results in an integer representing the bitwise result. Passing this value to the 'foo2' function, which takes an argument of the 'Orientation' type, results in the implicit type conversion and may lead to an invalid value being passed.

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam