linter icon indicating copy to clipboard operation
linter copied to clipboard

proposal: `null_aware_followed_by_null_assert`

Open srawlins opened this issue 2 months ago • 2 comments

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.

srawlins avatar May 21 '24 14:05 srawlins