Open natebosch opened 2 hours ago
Summary: The unnecessary_cast
diagnostic incorrectly flags casts to Function
when used to allow dynamic calls while avoid_dynamic_calls
is enabled. This is because the cast is necessary to explicitly allow the dynamic call, even though it appears redundant.
The pattern for allowing a dynamic call when the
avoid_dynamic_calls
is enabled is to explicitly cast todynamic
orFunction
in the expression that receives the dynamic call. Such a cast should suppress theunnecessary_cast
diagnostic. When we cast toFunction
to allow the dynamic call, we end up with an unnecessary cast.Idiomatic usage of the lint is to keep references as
Object?
throughout as much of the code as possible, and introduce thedynamic
only within the expression where a dynamic call is made. We assume backends will optimize a cast fromObject?
todynamic
. Neitherdynamic
orObject?
is more specific than the other so we lose nothing when we change alldynamic
toObject?
.Function
is more specific thanObject?
, so we would lose information changing allFunction
toObject
orObject?
just to be allowed to cast.I think we should allow specifically casting a
Function
toFunction
whenavoid_dynamic_calls
is enabled.