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

unnecessary_cast false positive when cast affects inferred type #52086

Closed
jamesderlin opened this issue Apr 14, 2023 · 3 comments
Closed

unnecessary_cast false positive when cast affects inferred type #52086

jamesderlin opened this issue Apr 14, 2023 · 3 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request

Comments

@jamesderlin
Copy link
Contributor

Describe the issue

x as T seems to trigger an unnecessary_cast lint if the static type of x is statically known to be a subtype of T. Although such a cast superficially seems pointless, there can be cases where the cast makes a difference. For example, consider:

var record = (1, 2, 3);

The inferred type of record is (int, int, int). If I want one of the members to be a num, I could explicitly specify the Record type:

(int, num, int) record = (1, 2, 3);

but that becomes awkward, tedious, and potentially error-prone if the Record has many members. I alternatively could do:

num x = 2;
var record = (1, x, 3);

which I suppose is okay, but it's more verbose and requires naming more things. I'd rather do:

var record = (1, 2 as num, 3);

The last version, however, triggers the unnecessary_cast lint (using DartPad based on Flutter 3.10.0-4.0.pre Dart SDK 3.0.0-431.0.dev).

To Reproduce

See above.

Expected behavior

Ideally the unnecessary_cast lint would trigger if and only if removing the cast has no effect.

I admit that this sort of situation probably isn't likely to occur much in practice.

@srawlins srawlins transferred this issue from dart-lang/linter Apr 18, 2023
@srawlins
Copy link
Member

If I want one of the members to be a num,

Can you give an example of when you want this? There are many examples of unnecessary_cast false positives (#44411, #48984, #49269, #49550), but I'm looking for some code that becomes an error without that as num.

@eernstg
Copy link
Member

eernstg commented Apr 18, 2023

class A<X extends A<X>> {}
class B extends A<B> {}
class C extends B {}

void f<X extends A<X>>(X x) {}

void main() {
  f(B()); // OK.
  f(C()); // Couldn't infer type parameter `X`.
  f(C() as B); // OK, but 'unnecessary_cast'.
}

@jamesderlin
Copy link
Contributor Author

jamesderlin commented Apr 19, 2023

Oops, I didn't see those other issues since I was searching in the wrong repository.

Can you give an example of when you want this?

A trivial example:

void main() {
  var record = (1, 2 as num);
  record = (1, 3.1415);
}

It doesn't seem too far-fetched that such a re-assignment might come about in something like:

final recordStore = <(int, num)>[];

bool somePredicate(int x) => false;

void main() {
  var record = (0, 0 as num); // Default value.
  for (var r in recordStore) {
    if (somePredicate(r.$1)) {
      record = r;
      break;
    }
  }

  // Do something with the found `record`.
}

@devoncarew devoncarew added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Apr 24, 2023
@bwilkerson bwilkerson added the P3 A lower priority bug or feature request label May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request
Projects
None yet
Development

No branches or pull requests

5 participants