dart-lang / linter

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

New lint to remove `if (true)` #5111

Open FMorschel opened 2 weeks ago

FMorschel commented 2 weeks ago

If you have the following code:

if (true) {
  print('true');
} else {
  print('false');  // <-- dead_code
}

Today we have no lint besides dead_code with a quick-fix for removing it.

I'd like to ask for one and a quick-fix to remove the if (true) or whatever cases we know will always be true.

This could also be triggered on if (x case int when true) to only remove the when true or similar.

--

Inspired by https://github.com/dart-lang/sdk/issues/56715

pq commented 2 weeks ago

Relatedly, we did once have invariant_booleans but had to decommission it due to the high maintenance cost.

As for

I'd like to ask for one and a quick-fix to remove the if (true) or whatever cases we know will always be true.

Given the above code what would the quick-fix produce?

FMorschel commented 2 weeks ago

Given the above code what would the quick-fix produce?

For the full code as above (including the else), we could remove both the dead_code and also the if (true) { and closing braces for it. If there is no dead_code I think we could remove only if (true) { and the closing braces.

bwilkerson commented 23 hours ago

I would make the dead_code fix do what it's doing today, and I would have a separate assist for removing the if. (I assume it would need to be an assist because I don't think we have a lint to flag such a case. If I'm wrong, then we could make it a fix for that lint.)