unnecessary_cast
false positive when cast affects inferred type
#52086
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
Describe the issue
x as T
seems to trigger anunnecessary_cast
lint if the static type ofx
is statically known to be a subtype ofT
. Although such a cast superficially seems pointless, there can be cases where the cast makes a difference. For example, consider:The inferred type of
record
is(int, int, int)
. If I want one of the members to be anum
, I could explicitly specify theRecord
type:but that becomes awkward, tedious, and potentially error-prone if the
Record
has many members. I alternatively could do:which I suppose is okay, but it's more verbose and requires naming more things. I'd rather do:
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.
The text was updated successfully, but these errors were encountered: