Skip to content

Avoid repeating default values for super parameters #58700

@goderbauer

Description

@goderbauer

If the default values for super parameters in a subclass are the same as in the super class, they don't have to be repeated as they will be copied from the super constructor. A lint could remind people that the repetition is unnecessary to keep it DRY.

Examples

Bad:

class Bar extends Foo {
  Bar({super.value = true}); // Lint on this line that "= true" isn't necessary.
}

class Foo {
  Foo({this.value = true});
  final bool value;
}

Good:

class Bar extends Foo {
  Bar({super.value}); 
}

class Foo {
  Foo({this.value = true});
  final bool value;
}

Also ok:

class Bar extends Foo {
  Bar({super.value = false}); // Don't lint because the default was changed.
}

class Foo {
  Foo({this.value = true});
  final bool value;
}

Additional context
Rules about copying the default value from the associated super-constructor: https://github.com/dart-lang/language/blob/master/working/1855%20-%20super%20parameters/proposal.md#parameter-types-and-default-values

Metadata

Metadata

Assignees

No one assigned

    Labels

    devexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.type-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