Consider a lint warning on uses of the null check operator on variables of type T?
#58231
Labels
devexp-linter
Issues with the analyzer's support for the linter package
legacy-area-analyzer
Use area-devexp instead.
linter-lint-request
type-enhancement
A request for a change that isn't a bug
Uh oh!
There was an error while loading. Please reload this page.
Given a generic type parameter
T
which has a nullable bound (e.g. the default bound ofObject?
), it is very easy to introduce erroneous null checks when working with a variable of typeT?
. Specifically, it is not uncommon to haveT? x;
and want to assert thatx
has been set to a valid value of typeT
. A common mistake is to do so usingx!
. This is almost always incorrect, since ifT
is a nullable type,x
may validly holdnull
as a value of typeT
. Example:A recommended pattern for writing this code is to instead to cast to
T
:I think the conditions under which the lint would fire would be:
T?
T
is a generic type parameterT
is potentially nullablecc @Hixie @matanlurey @lrhn @munificent @eernstg @natebosch @jakemac53 for any thoughts on this.
The text was updated successfully, but these errors were encountered: