unnecessary_cast_pattern
Unnecessary cast pattern.
Description
#The analyzer produces this diagnostic when a cast pattern is used on a value that is known to be of the specified type.
Example
#The following code produces this diagnostic because the cast as num
is known to always succeed because the type of z
is int
:
dart
void f(int x) {
if (x case var z as num) {
print(z);
}
}
Common fixes
#Remove the cast pattern:
dart
void f(int x) {
if (x case var z) {
print(z);
}
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.9.0. Page last updated on 2025-08-25. View source or report an issue.