abstract class BaseLoggingHandler {
LogRecordTransformer transformer;
void call(LogRecord logRecord);
}
class LogPrintHandler implements BaseLoggingHandler {
@override
LogRecordTransformer transformer;
}
complaints about "Do not override fields"
This rule was introduced here https://github.com/dart-lang/linter/pull/217 and requested here https://github.com/dart-lang/sdk/issues/25567, I think this is working as intended, below you can see the docs from the rule. Regardless, do you think the rule is wrong? How would you like it to be changed? It should be noted that the rule will only trigger if it is overriden with a field, if instead you have a getter and setter it won't complain.
http://dart-lang.github.io/linter/lints/overriden_field.html
DO Do not override fields.
BAD:
class Base {
Object field = 'lorem';
Object something = 'change';
}
class Bad1 extends Base {
@override
final field = 'ipsum'; // LINT
}
class Bad2 extends Base {
@override
Object something = 'done'; // LINT
}
GOOD:
class Base {
Object field = 'lorem';
Object something = 'change';
}
class Ok extends Base {
Object newField; // OK
final Object newFinal = 'ignore'; // OK
}
Thinking a bit more about it, I think it is a false positive (which I confirmed) if the implementing class (e.g. LogPrintHandler) has a field from a derived class, which would restrict the values that can be set on instances of such class. Is that what you had in mind?
AFAIR the intention of the rule was that it is inefficient when a subclass overrides a field with a field, because then there are two variables for one value (in superclass and subclass) while only one is actually used.
That is correct if I extend a class.
When I implement a class I only enforce the interface but don't get any implementation from the interface class, therefore there is still only one field (at least my understanding how implements works).
I don't know about that case, but as I said above, when you want to restrict the type in the child class, it makes sense to override, e.g.:
abstract class BaseLoggingHandler {
Base transformer;
}
class LogPrintHandler implements BaseLoggingHandler {
@override
Derived transformer; // OK
}
I am working on a PR for that case. While in review we can confirm what you mention above.
By the way, thanks for the report!
Here I got some great explanations to this topic https://github.com/dart-lang/sdk/issues/24928#issuecomment-157189016
Therefore it should only check for overridden fields of classes it extends but not the ones it implements.
This should be fixed now!
Thanks for the fix @alexeieleusis !
my pleasure!
Most helpful comment
Thanks for the fix @alexeieleusis !