-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Unnecessary Cast detected when removing it would cause an error. #54439
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
Comments
@gspencergoog, I don't think this topic has been organized in a very strict manner, but it would probably be helpful if you file a similar issue in the linter repo at https://github.com/dart-lang/sdk/issues/new and label it as a false-positive. It is not very likely that this lint can (realistically) be changed such that it always avoids flagging a cast if the code has an error when the cast is removed, but it is useful to keep track of false positives that are encountered in real life because it makes us think about it. ;-)
Right, but with the cast there is a run-time failure!
The reason for this is that The variant that uses void main() {
var xs = <int>[1].cast<num>();
xs.add(1.5); // Throws.
} Here's a safe way to do it: class Job {}
class Worker extends Job {}
class Group extends Job {}
void main() {
List<Worker> workers = [Worker()];
List<Group> groups = [Group()];
Iterable<Job> all = [...groups, ...workers];
print(all);
} This is safe because the covariant requirement is enforced (that is, if we have an expression of type In any case, it is both syntactically and semantically useful to use the list literal and the spread operator ( |
Wow, thanks for the excellent explanation. The safe way also seems pretty clear, even clearer than the cast function. Is there already an analyzer lint for I'm happy to also file a separate issue, but I don't have the access to add labels, so it would basically just be a duplicate of this one. :-) |
Thanks for the kind words!
I don't think it would work very well, it's probably going to be quite difficult to detect the cases where it's useful to use the spread operator rather than an invocation of The situation where However, all the elements are OK, so we'd very much like to pretend that the list is a void wantsAnIntList(List<int> ys) {
for (int i in ys) print(i);
}
void main() {
List<num> xs = [1, 2, 3]; // This is a `List<num>`, filled with `int`s.
// wantsAnIntList(xs); // Compile-time error.
// wantsAnIntList(xs as dynamic); // Run-time error.
wantsAnIntList(xs.cast<int>()); // OK, at compile time and at run time.
} With
No problem, the important part is that its reported as a lint issue. But I can transfer this one and put on the label. |
What version of Dart are you running, Greg? The analyzer should not be reporting this any longer. |
I was using Dartpad, which is at Dart 3.2.3. Indeed, when I'm using 3.3.0-244.0.dev locally, I no longer get the warning. Sorry, I should have checked the latest build before filing... Even so, I learned something. Feel free to reopen this if you want it around to track something else. |
[Edit eernstg: I transferred this issue to the linter repo because it is yet another example showing that the advice given by 'unnecessary_cast' may be confusing.]
If I have the following code:
Then on the line defining
all
, there will be a lint warning for an unnecessary cast, but if that cast is removed, then there is an error:Because it's not exactly the right type.
This seems like the unnecessary cast lint is being too strict here, and should not fire in this case.
The code is probably better written like below, so perhaps there should be a separate lint to recommend it:
The text was updated successfully, but these errors were encountered: