Skip to content

proposal: Consider named constructors as nonobvious for "obvious type" lints #61901

@loic-sharma

Description

@loic-sharma

Problem

It is not obvious from a call site whether Foo.bar(...) is a static method or a named constructor. As a result, the return type of Foo.bar(...) is non-obvious.

Proposal

Update the following lints to consider named constructors to have non-obvious types:

  1. omit_obvious_local_variable_types
  2. omit_obvious_property_types
  3. specify_nonobvious_local_variable_types
  4. specify_nonobvious_property_types

Example

class MediaQuery {
  MediaQuery.removeViewPadding(...) {}
  static Widget fromView(...) => ...;
}

// Explicit local variables - types are obvious
final Widget one = MediaQuery.fromView(...);
final MediaQuery two = MediaQuery.removeViewPadding(...);

// After - what's the type of `one`? It's not obvious
final one = MediaQuery.fromView(...);
final MediaQuery two = MediaQuery.removeViewPadding(...);

See also: MediaQuery.fromView, MediaQuery.removeViewPadding.

Counterarguments

Changing the behavior of an existing lint

Changing existing lints can be painful. For example, this can cause your project to fail to build when you update your Dart SDK.

However, I suspect that changing this behavior is acceptable as:

  1. All affected lints are marked experimental.
  2. All affected lints are used by less than 1% of all users. Google internal dashboard.

Current behavior is beneficial for more experienced developer

For someone aware that the lint is active, the very absence of a type declaration signals that the right-hand side must be a named constructor call whose type is considered unambiguous by the tool. This provides a subtle informational cue, but it was acknowledged that this is an "advanced" reading of the code that relies on prior knowledge of the linting rules.

While this might be true, I'd argue this is orthogonal to the lints' goal of specifying non-obvious types. This named constructor behavior is non-obvious! :)

cc @eernstg

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 packagetype-questionA question about expected behavior or functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions