Skip to content

Rule Req: recommend using a base type instead of var when LUB applies #57606

Open
@robbecker-wf

Description

@robbecker-wf

I would like to get a lint warning/hint for ternary operators when encountering code like this:

A -> SubClass1 -> BaseClass, with Element -> Object
B -> SubClass2 -> BaseClass, with Element -> Object

var shouldBeOfTypeBaseClass = someBoolCheck() ? new A() : new B();
// the analyzer will infer that shouldBeOfTypeBaseClass is of type Object instead of the 
// desired `BaseClass` class

// the same thing would apply to the ?? operator
A a;
B b = new B();
var shouldBeOfTypeBaseClass = a ?? b;

See #30079 (comment) for more info on motivation.

I'd like to see a lint warning on the var before shouldBeOfTypeBaseClass pointing out that the type of shouldBeOfTypeBaseClass will be inferred as Object and you should specify a base class type instead of using var.

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-requesttype-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