org.sonar.l10n.java.rules.squid.S864.html Maven / Gradle / Ivy
The rules of operator precedence are complicated and can lead to errors. For this reason, parentheses should be used for clarification in complex statements. However, this does not mean that parentheses should be gratuitously added around every operation.
This rule raises issues when &&
and ||
are used in combination, when assignment and equality or relational operators are used in together in a condition, and for other operator combinations according to the following table:
+
, -
, *
, /
, %
<<
, >>
, >>>
&
^
|
+
, -
, *
, /
, %
x x x x
<<
, >>
, >>>
x x x x
&
x x x x
^
x x x x
|
x x x x
Noncompliant Code Example
x = a + b - c;
x = a + 1 << b; // Noncompliant
if ( a > b || c < d || a == d) {...}
if ( a > b && c < d || a == b) {...} // Noncompliant
if (a = f(b,c) == 1) { ... } // Noncompliant; == evaluated first
Compliant Solution
x = a + b - c;
x = (a + 1) << b;
if ( a > b || c < d || a == d) {...}
if ( (a > b && c < d) || a == b) {...}
if ( (a = f(b,c)) == 1) { ... }
See
- MISRA C:2004, 12.1 - Limited dependence should be placed on C's operator precedence rules in expressions
- MISRA C++:2008, 5-0-2 - Limited dependence should be placed on C++ operator precedence rules in expressions
- MISRA C:2012, 12.1 - The precedence of operators within expressions should be made explicit
- CERT EXP00-C - Use parentheses for precedence of operation
- CERT EXP00-CPP - Use parentheses for precedence of operation
- MITRE, CWE-783 - Operator Precedence Logic Error
© 2015 - 2025 Weber Informatics LLC | Privacy Policy