Dead Code/Declarations Due to Never #60338
Labels
area-devexp
For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.
devexp-warning
Issues with the analyzer's Warning codes
P2
A bug or feature request we're likely to work on
type-enhancement
A request for a change that isn't a bug
Dart SDK version: 3.8.0-149.0.dev (dev) (Thu Feb 27 04:01:43 2025 -0800) on "macos_x64"
There are several ways to use
Never
to create dead code or declarations that are impossible to use, without getting any static warnings.Examples
All the dead code or declarations discussed here are not currently recognized by static analysis.
The following type cast is dead code. The
exit
is performed but the casting of the result is dead code.The following function can never run because it needs a
Never
instance as input.The following function can be run without issue because its type parameter is satisfied by
Never
.Static analysis can even be satisfied at the call site by using an expression which evaluates to
Never
as input. Since that expression evaluates toNever
the subsequent evaluation off
is impossible, and that call is dead code.Similarly I can define a class with an assignable
Never
field.This can never be instantiated, the entire declaration's instance members and constructors become dead declarations. Its static members would still be usable, as long as they don't require
Never
objects to be inputted. TheNever
object doesn't necessarily need to be declared as a stored field, the result is the same if the class can only be constructed via constructors which requireNever
input.It is possible for only constructors to be dead declarations and still have instance API be usable if there exists a way to construct without
Never
input.Similarly from #60251, impossible to use instance API can be declared.
In that issue, @FMorschel also pointed this out for extension types.
Expectation
Any function which requires a
Never
object to be specified as input should be marked as a dead declaration/code. Any type declaring aNever
object as a stored field or requiringNever
as input in order to be instantiated should have all its instance methods and constructors marked as dead declarations. Furthermore, any type that extends or mixes in such code would have to do the same. In the case of implements it is possible to override the field as a getter and therefore does not necessarily render the type nonusable.It can also be indicated that if the type is intended to only be implemented, it should be declared as an
abstract interface class
instead.Related Issues and Discussions
Related to the currently open issues
x=
operators invocation for the typeNever
#60318Related to the recently closed issue
There's also the discussion which
Never
analysis issues spawned from dart-lang/language#4279The text was updated successfully, but these errors were encountered: