Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
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#)
OWASP errors (Java)
Problems related to code analyzer
Additional information
toggle menu Contents

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

03 Déc 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

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam