dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
627 stars 172 forks source link

proposal: `unneccessary_cascade` #3960

Open jacob314 opened 1 year ago

jacob314 commented 1 year ago

unneccessary_cascade

Description

.. was used where . would have the same effect.

Details

The cascade operator should only be used if you are performing a sequence of operations on the same object. If you are only performing one operator, you should use . instead of ...

Kind

Enforces general style advice to avoid unnecessary code.

Bad Examples

serviceManager.appState..setPausedOnBreakpoint(false);

Good Examples

serviceManager.appState.setPausedOnBreakpoint(false);
var myAppState = serviceManager.appState..setPausedOnBreakpoint(false); // Here the cascade served a purpose.

Discussion

This lint is mostly useful to avoid issues sometimes only caught in code review when refactoring code that used a cascade. Example: https://github.com/flutter/devtools/pull/5004/files#r1061936361

Discussion checklist

pq commented 1 year ago

You may be proposing a broader rule, but maybe that would have been flagged by avoid_single_cascade_in_expression_statements?