Possibly inappropriate data type for switch expression
switch expression has a data type other than char, short, int or
enum
Description
This defect occurs when a switch expression has a data type other than
char, short, int or
enum.
The checker flags other integer data types such as boolean types, bit fields, or
long.
Risk
It is preferred to use char, short,
int or enum in switch expressions
instead of:
Boolean types, because a
switchexpression with a boolean type can be replaced with anifcondition that evaluates the same expression. Aswitchexpression is too heavy for a simple control flow based on a boolean condition.Bit field types, because bit field types imply memory restrictions. If you just want to specify a variable with a finite number of values, enumerations are preferred since they enable a more readable code.
Types with size greater than
intbecause aswitchexpression that requires a type with size greater thanintimplies too many case labels and can be possibly redesigned.
Non-integer types are not supported in switch expressions.
Fix
Use variables of char, short,
int or enum data types in switch
expressions.
Examples
Result Information
| Group: Good practice |
| Language: C | C++ |
| Default: Off |
Command-Line Syntax:
INAPPROPRIATE_TYPE_IN_SWITCH |
| Impact: Low |
Version History
Introduced in R2020a
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)