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: `useless_async` #3777

Open DetachHead opened 1 year ago

DetachHead commented 1 year ago

useless_async

Description

warns when using the async keyword when the function doesn't need to

Details

using the async keyword where it's not needed can lead to confusion and misunderstandings of how Futures and async/await works.

eslint has a rule for this called require-await

Kind

from the eslint rule docs:

Asynchronous functions that don’t use await might not need to be asynchronous functions and could be the unintentional result of refactoring.

Good Examples

Future<String> getValue() async {
  // ...
}

Future<String> foo() {
  return getValue();
}

Bad Examples

i often see code like this in real life. and when pointing it out it's usually due to a lack of understanding of what exactly async does.

Future<String> getValue() async {
  // ...
}

Future<String> foo() async { // this `async` is not necessary
  return getValue();
}

Discussion checklist

incendial commented 1 year ago

@DetachHead have you tried https://dartcodemetrics.dev/docs/rules/common/avoid-redundant-async? 🙂

DetachHead commented 1 year ago

@incendial thanks! looks like that rule was added since i last looked at dart_code_metrics

incendial commented 1 year ago

@DetachHead hm, maybe you have suggestions how we can help discover new rules?

DetachHead commented 1 year ago

personally i like to set linters to be as strict as possible. that way, i can see the errors in my codebase and decide for myself easily which new rules i want to disable. maybe if there was an option to enable all rules by default and have a list of rules to explicitly disable

incendial commented 1 year ago

You can, DCM now supports presets https://dartcodemetrics.dev/docs/getting-started/configuration#extending-an-existing-configuration-preset and https://github.com/dart-code-checker/dart-code-metrics/tree/master/lib/presets, this should cover you use-case

and to disable a rule, just simply write "rule-name": false in the rules list

DetachHead commented 1 year ago

awesome, thanks!