Skip to content

prefer_const_constructors_in_immutables: doesn't trigger if class has no constructor #58746

@goderbauer

Description

@goderbauer

prefer_const_constructors_in_immutables only triggers when an @immutable class has a non-const constructor. If no constructor is explicitly defined, the lint doesn't trigger and you - unfortunately - end up with an immutable class that cannot be const constructed. I think, the lint should remind people to add an explicit const constructor in those cases as well.

// 🔥 no reminder is given that this immutable class should have a const constructor.
@immutable
class Foo {

}

// lint triggers as expected.
@immutable
class Bar {
  Bar();
}

This could be relevant for some Flutter widgets that don't take in any arguments and because of this developers don't see a reason to add an explicit constructor. They are now missing an opportunity to create a const instance of their widget.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onarea-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-false-negativeIssues related to lint rules that fail to report a problem.linter-set-fluttertype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions