Rubocop: New Cop Idea: Explicit operator precedence

Created on 4 Apr 2020  路  3Comments  路  Source: rubocop-hq/rubocop

Is your feature request related to a problem? Please describe.

This is a request for new cop based on @bbatsov's comment here.

Describe the solution you'd like

The cop should check for explicit parens when operators with varying precedence are used in a statement.

# bad
a && b || c
10 + 20 * 3

# good
(a && b) || c
10 + (20 * 3)

Describe alternatives you've considered

NA

Additional context

We need to decide if the cop should consider all operators or only commonly used ones.

feature request good first issue

Most helpful comment

Well, I have to say I love the idea! 馃槈

All 3 comments

Well, I have to say I love the idea! 馃槈

Hi @bbatsov, I have made a PR for this feature. There is one issue I can fix in it.
The test case to fix a ** b * c / d % e + f - g << h >> i & j | k ^ l fails. Somehow the cop exercises the correction method only once.

It would be nice to have this depend somehow on the complexity of the expression. But then again, I think a ** b * c / d % e + f - g << h >> i & j | k ^ l would be equally hard to comprehend with parentheses.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Aqualon picture Aqualon  路  3Comments

printercu picture printercu  路  3Comments

benoittgt picture benoittgt  路  3Comments

ecbrodie picture ecbrodie  路  3Comments

Ana06 picture Ana06  路  3Comments