Skip to content

Add a warning against unnecessary use of "!" #58304

@rrousselGit

Description

@rrousselGit

When using non-nullable types, it is possible to write the following:

int? a;
int? b = a!;

While this is valid code, it doesn't make sense to do a! when a simple a would be enough.
Since b is nullable, there is no reason to assert that a is not null, as any usage of b would handle the null case.

This is problematic because as we refactor code, it is possible for b to change from non-nullable to nullable.
So while the ! used to make sense, after refactoring it is instead harmful.

This should also support function calls:

void function(int? b) {}

function(a!); // do function(a) instead

Metadata

Metadata

Assignees

No one assigned

    Labels

    devexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions