AutosarC++17_03-A7.2.1

An expression with enum underlying type shall only have values corresponding to the enumerators of the enumeration

Required inputs: IR, StaticSemanticAnalysis

Rationale

It is unspecified behavior if the evaluation of an expression with enum underlying type yields a value which does not correspond to one of the enumerators of the enumeration.

Additionally, other rules in this standard assume that objects of enum type only contain values corresponding to the enumerators. This rule ensures the validity of these assumptions.

One way of ensuring compliance when converting to an enumeration is to use a switch statement.

Example
// $Id: A7-2-1.cpp 271687 2017-03-23 08:57:35Z piotr.tanski $
#include <cstdint>
enum class E : std::uint8_t { Ok = 0, Repeat, Error };
E convert1(std::uint8_t number) noexcept {
E result = E::Ok; // Compliant
switch (number) {
case 0: {
result = E::Ok; // Compliant
break;
}
case 1: {
result = E::Repeat; // Compliant
break;
}
case 2: {
result = E::Error; // Compliant
break;
}
case 3: {
constexpr std::int8_t val = 3;
result = static_cast<E>(val); // Non-compliant - value 3 does not
                          // correspond to any of E’s
                          // enumerators
break;
}
default: {
result = static_cast<E>(0); // Compliant - value 0 corresponds to E::Ok
break;
}
}
return result;
}
E convert2(std::uint8_t userInput) noexcept {
E result = static_cast<E>(userInput); // Non-compliant - the range of
                                // userInput may not correspond to
                                // any of E’s enumerators
return result;
}
E convert3(std::uint8_t userInput) noexcept {
E result = E::Error;
if (userInput < 3) {
result = static_cast<E>(userInput); // Compliant - the range of
                                // userInput checked before casting
                                // it to E enumerator
}
return result;
}
See Also
  • MISRA C++ 2008 [6]: Rule 7-2-1 An expression with enum underlying type shall only have values corresponding to the enumerators of the enumeration.

Possible Messages

Key

Text

Severity

Disabled

asgn_interval_not_an_enumerator

Possible assignment of value which does not correspond to an enumerator (values computed by the analysis are [{int1};{int2}], they contain e.g. {int0} which does not correspond to an enumerator)

None

False

asgn_interval_out_of_enum_range

Possible assignment of value outside the enumerators (values computed by the analysis are [{int0};{int1}])

None

False

asgn_not_an_enumerator

Assignment of value {int0} which does not correspond to an enumerator

None

False

asgn_out_of_enum_range

Assignment of value {int0} outside the range of the enumerators

None

False

conversion_creating_bad_enum_value

Expression does not correspond to an enumerator in {}

None

False

Options

any_enum

any_enum : bool = True

Whether to check any enumeration. If false, scoped or enumerations based on a fixed type are not checked.
 

enum_kinds

enum_kinds : EnumKinds = 'Any'

Allow to restrict the check to certain enumeration types. Apart from 'Any' other meaningful choices are 'Unscoped_Unbased' for enumerations based on a fixed type or 'Scoped' for scoped enumerations.
 

with_predicate_analysis

with_predicate_analysis : bool = True

For enum assignment check, use an additional analysis step based on predicate abstraction for the analysis of enum values. It can reduce the number of false positives, but is potentially costly.
 

Option Types

These types are used by options listed above:

EnumKinds

  • Any

  • Scoped

  • Unscoped_Unbased