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 ?? inside if null test when the body does more things #5109

Open
FMorschel opened this issue Oct 11, 2024 · 4 comments
Open
Labels
lint-request set-recommended Affects a rule in the recommended Dart rule set type-enhancement A request for a change that isn't a bug

Comments

@FMorschel
Copy link
Contributor

FMorschel commented Oct 11, 2024

Repro:

class A {
  int? value;
  void foo() {
    if (value == null) {
      value ??= 0;
    }
  }
}

This is a code that triggers prefer_conditional_assignment with a quick-fix that suggests removing the if.

class A {
  int? value;
  void foo() {
    if (value == null) {
      value ??= 0;
      print(value);
    }
  }
}

This code is the same but it does more things, so in this case I believe we should have a new lint + quick-fix for reminding the user to remove the ?? from the assignment expression since it doesn't do anything.

As the bot reminded me to be explicit: the second code has no lint today.

@dart-github-bot
Copy link

Summary: The prefer_conditional_assignment lint incorrectly suggests removing an if statement when the body contains additional code besides the conditional assignment. A new lint should be created to detect and suggest removing the ?? operator in such cases.

@dart-github-bot dart-github-bot added the type-enhancement A request for a change that isn't a bug label Oct 11, 2024
@pq pq transferred this issue from dart-lang/sdk Oct 15, 2024
@github-actions github-actions bot added the set-recommended Affects a rule in the recommended Dart rule set label Oct 15, 2024
@srawlins
Copy link
Member

Yeah not a bad idea. I think we need information about whether a promotable variable is definitely null (or Null type). But I think currently we don't promote on == null. I think there is a language issue for this proposal.

@FMorschel
Copy link
Contributor Author

FMorschel commented Oct 16, 2024

Do you mean dart-lang/language#3031 or dart-lang/language#3057?

@srawlins
Copy link
Member

This one: dart-lang/language#1505

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lint-request set-recommended Affects a rule in the recommended Dart rule set type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants