codeql-coding-standards
codeql-coding-standards copied to clipboard
`M5-0-2`: triggers on expressions with no dependence on precedence rules
Affected rules
- M5-0-2
Description
M5-0-2 reports "Limited dependence should be placed on C++ operator precedence rules in expressions" on expressions which are fully parenthesized
Example
constexpr byte& operator&=(byte& lhs, const byte rhs) noexcept {
lhs = (lhs & rhs);
return lhs;
}
This is reported by a query called GratuitousUseOfParentheses.ql
, and should have been reported with the message:
Gratuitous use of parentheses around ...&...
The reason we have this query is that M5-0-2 discusses the addition of too many parenthesis:
...However, too many parentheses can clutter the code and make it unreadable.
On this specific case, it goes on to say:
Parentheses are not required for the right-hand operand of an assignment operator unless the right-hand side itself contains an assignment expression: x = a + b; // acceptable x = (a + b); // () not required
This query is therefore working as designed. However, I do think it's open to interpretation whether the rule requires that we flag cases where too many parentheses are used.
In this specific case the parenthesis are certainly redundant, but whether they are cluttering the code is a matter of personal opinion.
The main options here are:
- Leaving the query as-is and modifying your code to remove the redundant parentheses.
- Tweaking the query to avoid reporting this specific case, but still prohibiting other cases (such as
a * (-1)
). - Downgrading the query so that it is not run by default, but can be enabled optionally.