Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New lint to remove if (true) #5111

Open
FMorschel opened this issue Oct 17, 2024 · 3 comments
Open

New lint to remove if (true) #5111

FMorschel opened this issue Oct 17, 2024 · 3 comments

Comments

@FMorschel
Copy link
Contributor

FMorschel commented Oct 17, 2024

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 dart-lang/sdk#56715

@pq
Copy link
Member

pq commented Oct 17, 2024

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
Copy link
Contributor Author

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
Copy link
Member

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants