Skip to content

Add a new lint to require types on variables but not collections #58015

Closed
@Levi-Lesches

Description

@Levi-Lesches

Per Effective Dart:

AVOID redundant type arguments on generic invocations.
A type argument is redundant if inference would fill in the same type. If the invocation is the initializer for a type-annotated variable, or is an argument to a function, then inference usually fills in the type

Set<String> things = Set(); (good)
Set<String> things = Set<String>(); (bad)

Here, the type annotation on the variable is used to infer the type argument of constructor call in the initializer.

In other contexts, there isn’t enough information to infer the type and then you should write the type argument:

var things = Set<String>(); (good)
var things = Set(); (bad)

Here, since the variable has no type annotation, there isn’t enough context to determine what kind of Set to create, so the type argument should be provided explicitly.

But with the following code:

const Set<String> temp = {};

I get the following:
INFO|LINT|... Specify type annotations.

This comes from the rule always_specify_types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    devexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions