Skip to content

Add unnecessary_non_nullable_cast #58613

@rrousselGit

Description

@rrousselGit

Describe the rule you'd like to see implemented

Related to https://www.reddit.com/r/dartlang/comments/s1if4e/dart_null_safety_vs_type_safety_dart_linter_in

There is currently no warning for when we are casting a value as non-nullable when that value could be nullable.
An example would be:

int? value = obj as int; // should probably be "as int?"

While this cast is technically correct, casting as non-nullable instead of nullable brings no value and is instead a source of errror

Examples

This can happen in various situations:

void function(int? value) {...}

function(obj as int); // warn

int? a = obj as int; // warn

int? fn() {
  return obj as int; // warn
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-proposaltype-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