dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
629 stars 170 forks source link

Dual rule for initialize formals #485

Open alexeieleusis opened 7 years ago

alexeieleusis commented 7 years ago

If a private field is initialized in the body of a constructor and could be replaced by a formal, a lint should be produce to move it to an initializer.

class MyClass {
    Foo _foo;

    MyClass.incorrect(Foo otherFoo) {
        _foo = otherFoo; // LINT
        ...
    }

    // This could also enable other lints like declare _foo as final or remove constructor body.
    MyClass.correct(Foo otherFoo) : _foo = otherFoo {
        ...
    }
}
srawlins commented 1 year ago

Kind of surprising that we don't have a rule yet for this; I guess it would be tricky to calculate whether the value (the right side) is something accessible in initializers.