linter
linter copied to clipboard
proposal: `null_aware_followed_by_null_assert`
null_aware_followed_by_null_assert
Description
This one is a little odd, but I've seen a few cases. In code like this:
foobarDateRange = (foobarDateRange
.clampSomething(Something.VALUE)!
.applySomething()
?.clampToSomething(Something.OTHER_VALUE)!)!;
It's a bit hard to follow the parens and bangs, but that last line essentially has ?.foo(bar)!
in it. I don't think there is ever any reason to have a null-aware method call or null aware property access (or chain of these) followed by a null-assert. It fails in the same way as !.foo(bar)
, with a more-or-equally informative stack trace, and is more readable.
Kind
Readability
Bad Examples
a.b?.c!;
a.b?.c()!;
a.b?.c.d().e!;
Not sure if this needs to be careful around cascades...
Good Examples
Add a few examples that demonstrate a “good” adoption of this lint’s principle.