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

Consider adding "why promoted" context information for warnings/errors caused by type promotion #45332

Open
stereotype441 opened this issue Mar 16, 2021 · 1 comment
Labels
area-devexp Developer experience items (DevTools, IDEs, analysis server, completions, refactorings, ...). devexp-ux legacy-area-front-end Legacy: Use area-dart-model instead. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@stereotype441
Copy link
Member

Inspired by this comment: dart-lang/language#1523 (comment)

Consider the following code:

f(Object? x) {
  g(x as int); // (1)
  if (x == null) { // (2)
    print('x was null');
  }
}
g(int? x) {}

Note that the user migrated this code incorrectly from a pre-null-safety version: at (1), they should have used x as int? to preserve the behavior that null is allowed.

Currently the analyzer generates the following warning at the line marked (2):

   info • test.dart:3:9 • The operand can't be null, so the condition is always false. Try removing the condition, an enclosing condition, or the whole
          conditional statement. • unnecessary_null_comparison

Based on this warning, it's not easy to figure out that the mistake was in the as expression at (1). A possible way to help this would be to add context information to the warning explaining the reason the operand can't be null: because it was promoted at line (1).

It should be possible to do this in a similar way to how we are adding "why not promoted" context information (#44897).

@stereotype441 stereotype441 added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. legacy-area-front-end Legacy: Use area-dart-model instead. labels Mar 16, 2021
@srawlins srawlins added P2 A bug or feature request we're likely to work on devexp-ux labels Mar 16, 2021
@a14n
Copy link
Contributor

a14n commented Mar 17, 2021

Note that there's the lint cast_nullable_to_non_nullable to catch (1)

@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Mar 14, 2024
@bwilkerson bwilkerson added area-devexp Developer experience items (DevTools, IDEs, analysis server, completions, refactorings, ...). and removed area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp Developer experience items (DevTools, IDEs, analysis server, completions, refactorings, ...). devexp-ux legacy-area-front-end Legacy: Use area-dart-model instead. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants