AutosarC++18_10-A5.2.6

The operands of a logical && or || shall be parenthesized if the operands contain binary operators

Required inputs: IR

Rationale

Parentheses are required to add clarity in logical expressions making code easier to review versus code based only C++ operator precedence rules.

Example
// $Id: A5-2-6.cpp$

    #include <cstdint>

    void Fn(std::int32_t value) noexcept
    {
        if (value > 0 && value < 3) // Non-compliant
        {
             // do some work
        }
        else if ((value > 1) && (value < 2)) // Compliant
        {
             // do some work
        }
        else
        {
             // do some work
        }

          return;
    }
See Also
  • MISRA C++ 2008 [7]: M5-2-1: Each operand of a logical && or || shall be a postfix expression.
  • JSF December 2005 [8]: AV Rule 158: The operands of a logical && or \\ shall be parenthesized if the operands contain binary operators.
  • C++ Core Guidelines [11]: ES.41: If in doubt about operator precedence, parenthesize

Possible Messages

Key

Text

Severity

Disabled

missing_parens_for_logical_operator_operand

Operand of && or || must be parenthesized.

None

False

Options

allow_sequences

allow_sequences : bool = False

If set to True, sequences of && and sequences of || operators do not need to be individually parenthesized.