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 unrelated_type_checks like with unrelated_type_equality_checks #5052

Open
ltOgt opened this issue Aug 8, 2024 · 2 comments
Labels
P3 A lower priority bug or feature request set-core Affects a rule in the core Dart rule set type-question A question about expected behavior or functionality

Comments

@ltOgt
Copy link

ltOgt commented Aug 8, 2024

The following is covered by unrelated_type_equality_checks:

void someFunc(int number, String string) {
  if (number == string) {} // linter gives a warning for unrelated types
}

But the following does not give a warning:

void someFunc(int number, String string) {
  if (number is String) {} // no warning for unrelated type check
}
Screenshot 2024-08-08 at 10 46 38
@github-actions github-actions bot added the set-core Affects a rule in the core Dart rule set label Aug 8, 2024
@srawlins
Copy link
Member

srawlins commented Aug 8, 2024

My 2 cents: I think there enough occasions where you want to check if a is a type that is unrelated to it's static type, so I would not report the general case.

But you can still report cases that will never be true, in the cases of final classes. The example above, "is this int a String?" is a perfect example.

Given the expression x has static type T, and you're checking x is U (or x as U, I believe):

  • If T is final and U is neither a supertype of T nor a supertype of any of T's subtypes (which can only be declared in T's library), then the check will always be false.
  • If U is final and T is neither a supertype of U nor a supertype of any of U's subtypes (which can only be declared in U's library), then the check will always be false. (I don't think I have this backwards...)

The same does not hold for sealed classes, as their subclasses can be subclassed arbitrarily.

We can similarly report unnecessary checks, where U is a supertype of T.

@pq pq added type-question A question about expected behavior or functionality P3 A lower priority bug or feature request labels Aug 9, 2024
@FMorschel
Copy link
Contributor

Inspired by this issue I created #5062 about unrelated_type_casts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 A lower priority bug or feature request set-core Affects a rule in the core Dart rule set type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

4 participants