Closed ChaserVasya closed 8 months ago
unnecessary_cast
does indeed have false positives. However, in this particular situation you'd probably do the following anyway (which means that there is no cast, and hence no spurious messages from unnecessary_cast
):
Date copyWith({
int? year,
int? month,
int? day,
bool? isUtc,
}) {
return DateTimeCopyWith(this) // <-- Select the extension explicitly.
.copyWith(
year: year,
month: month,
day: day,
isUtc: isUtc,
)
.clipTime;
}
The point is that you can guide the resolution of an extension member invocation by modifying the type of the receiver (e.g., using a cast), but if you just want to ensure that the invocation will call a specific extension member then you might just as well perform the resolution explicitly. This will document the purpose much more directly.
This technique will force the resolution to choose the extension member that you want to invoke, even in the case where some of the conflicting declarations are modified (there are some conflicting declarations: you never need to influence the resolution of an extension member invocation unless there are other declarations with the same name, e.g., instance members and/or other extension members). This means that the manually resolved extension method invocation is more robust over time as your software (and the software that you import) evolves.
Of course, it's much more convenient if there is no need to specify the extension explicitly, but it is probably not possible to completely avoid this kind of name clash.
@ChaserVasya, I think I should close this issue because everything except possibly the unnecessary_cast
lint is working as intended. I don't see the lint issue locally (though I haven't tried to debug that), but it would be great if you can create a concise example and report it on the linter repository.
There is a code snippet.
There are 2
copyWith
: 1)extension DateTimeCopyWith on DateTime
2)Date.copyWith
method.Date.copyWith
should useDateTime.copyWith
. Analyzer alerts 'Unnecessary cast'. I remove(this as DateTime)
. Then there is a cycle dependency:Date.copyWith
usesDate.copyWith
:It is wrong behaviour.
Also I can't use
super.copyWith
:The method 'copyWith' isn't defined in a superclass of 'Date'.
Desired behavior
Analyzer doesn't alert
unnesessary_cast
in this scenario.General info
Project info
Process info