AutosarC++18_10-A5.2.6¶
The operands of a logical && or || shall be parenthesized if the operands contain binary operators
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
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¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
allow_sequences¶
allow_sequences : bool = False