Skip to content

Lint when member might need to be this.member in constructor bodies #58358

@jamesderlin

Description

@jamesderlin

The other day I modified some non-null-safe code from:

class Foo {
  int x;

  Foo({int x = 42}) {
    // Other work.
  }
}

to:

class Foo {
  int x;

  Foo({int x})
    : x = x ?? 42 {
    // Other work.
  }
}

It turns out that this was wrong because that "Other work" portion in the constructor body referenced x, and because the local x parameter understandably shadows the x member, it therefore was not using the expected default value. Oops. I didn't realize this mistake until I later attempted to migrate the code for null-safety.

This is probably less of a problem with null-safety enabled, although it could still happen. For example:

class Foo {
  int x;
  late String string;

  Foo({int? x}) : x = x ?? 42 {
    string = x.toString();
  }
  
  @override
  String toString() => string;
}

void main() {
  print(Foo()); // Want '42', but prints 'null'.
}

Or it could just be something like:

class Foo {
  int x;

  Foo({int? x}) : x = x ?? 42 {
    int updatedValue = someMemberFunction();
    x = updatedValue;
  }

It would be nice if there were some lint that suggested using this.x instead of x in the constructor body for these situations. In the specific case of constructors, if a parameter name and a member name are the same, it seems rare to me that someone would prefer the parameter; I think it would almost always be a mistake. This isn't fully baked, but I think criteria would be something like:

  • Class T has a member variable x and a constructor with a parameter named x. That constructor has an initializer list that initializes x that is not x = x or this.x = x. (Intention is to lint only if the member and parameter potentially have different values.)
  • Class T has a setter named x and a constructor with a parameter named x. That constructor has a body that assigns to x.

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