Skip to content

Add unnecessary_non_nullable_cast #58613

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

Open
rrousselGit opened this issue Jan 11, 2022 · 4 comments
Open

Add unnecessary_non_nullable_cast #58613

rrousselGit opened this issue Jan 11, 2022 · 4 comments
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-linter Issues with the analyzer's support for the linter package linter-lint-request P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@rrousselGit
Copy link

rrousselGit commented Jan 11, 2022

Describe the rule you'd like to see implemented

Related to https://www.reddit.com/r/dartlang/comments/s1if4e/dart_null_safety_vs_type_safety_dart_linter_in

There is currently no warning for when we are casting a value as non-nullable when that value could be nullable.
An example would be:

int? value = obj as int; // should probably be "as int?"

While this cast is technically correct, casting as non-nullable instead of nullable brings no value and is instead a source of errror

Examples

This can happen in various situations:

void function(int? value) {...}

function(obj as int); // warn

int? a = obj as int; // warn

int? fn() {
  return obj as int; // warn
}
@rrousselGit rrousselGit added type-enhancement A request for a change that isn't a bug linter-lint-request labels Jan 11, 2022
@bwilkerson
Copy link
Member

Did you intend to include the ? on the next-to-last line?

I'm wondering whether other conditions should also be flagged. For example, is this fundamentally different than any other cast to a type that is narrower than the context would require, such as num n = obj as int;?

@srawlins Is this case covered by any of the existing or planned language options?

@srawlins
Copy link
Member

No I don't think this case is covered by any static analysis.

@rrousselGit
Copy link
Author

Did you intend to include the ? on the next-to-last line?

No my bad! Fixed

For example, is this fundamentally different than any other cast to a type that is narrower than the context would require, such as num n = obj as int;

That would make sense too yes!

@fishythefish
Copy link
Member

Adding a data point: this lint would probably have prevented #52403 (here's the fix CL).

I was about to file a lint request when I came across this issue. :)

Re: extending this lint to all casts - I was thinking about this when I was starting to write the lint request. My gut instinct is that nullability-related casts aren't inherently special, so extending to all casts should be fine. But I do suspect that a lot of the immediate value is in detecting bad migrations, so I would happily take a lint just restricted to this case if it turns out that the broader lint has some unforeseen edge cases.

@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 19, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 19, 2024
@pq pq added the P3 A lower priority bug or feature request label Nov 20, 2024
@bwilkerson bwilkerson added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. and removed legacy-area-analyzer Use area-devexp instead. labels Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-linter Issues with the analyzer's support for the linter package linter-lint-request P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants