AutosarC++18_03-A5.16.1ΒΆ

The ternary conditional operator shall not be used as a sub-expression

Required inputs: IR

Rationale

Using the result of the ternary conditional operator as an operand or nesting conditional operators makes the code less readable and more difficult to maintain.

Example
// $Id: A5-16-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
constexpr bool Fn1(std::int32_t x) {
  return (x > 0); // Compliant
}
std::int32_t Fn2(std::int32_t x) {
  std::int32_t i = (x >= 0 ? x : 0); // Compliant

  std::int32_t j =
      x + (i == 0 ? (x >= 0 ? x : -x) : i); //   Non-compliant - nested
                                            //   conditional operator
                                            //   and used as a
                                            //   sub-expression
  return (i > 0
              ? (j > 0 ? i + j : i)
              : (j > 0 ? j : 0)); // Non-compliant - nested conditional operator
}
See Also
  • HIC++ v4.0 [8]: 5.8.1 Do not use the conditional operator (?:) as a sub- expression.

Possible Messages

Key

Text

Severity

Disabled

use_of_nested_conditional_operator

Use of nested conditional operator.

None

False

Options