-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
avoid_redundant_argument_values
lint rule false positives
#88
Comments
Freezed is able to get away with this, but I believe it is a coincidence. Here is a simplified view on the situation. Consider the following model for which we will generate a copyWith: class Model {
const Model({required this.field});
final String? field;
} Field is nullable so we should be able to copyWith with a null. The generated code for this is roughly (removed the field copyWiths): abstract class _$ModelCWProxy {
Model call({
String? field,
});
}
class _$ModelCWProxyImpl implements _$ModelCWProxy {
const _$ModelCWProxyImpl(this._value);
final Model _value;
@override
Model call({
Object? field = const $CopyWithPlaceholder(),
}) {
return Model(
field: field == const $CopyWithPlaceholder()
? _value.field
: field as String?,
);
}
}
extension $ModelCopyWith on Model {
_$ModelCWProxy get copyWith => _$ModelCWProxyImpl(this);
} Using After looking what Freezed does (I am not sure if they are doing it on purpose because of this lint, or they just happen to be doing something that works here), we can alter slightly the definition of abstract class _$ModelCWProxy<$Res> {
$Res call({
String? field,
});
}
class _$ModelCWProxyImpl implements _$ModelCWProxy<Model> {
// as before...
}
extension $ModelCopyWith on Model {
_$ModelCWProxy<Model> get copyWith => _$ModelCWProxyImpl(this);
} However, now Now, in the general case it is impossible to prevent this false positive. The linter cannot know what is the underlying implementation (after all this is the point of abstraction). If |
For future reference, I’ve noticed that the warning disappears in the simplest use case when following the suggested fix from #88 (comment). However, I need to double-check this solution and ensure it doesn’t cause any side effects in more complex scenarios. It’s concerning that the issue disappears for unknown reasons. It’s likely that it reaches the limits of introspection or something of this kind. Even removing the default value |
Describe the issue
avoid_redundant_argument_values
lint rule reports false positives when assigning null usingcopyWith
method.Environment details
Paste the flutter environment detail.
Paste the package version.
To Reproduce
Steps to reproduce the behaviour:
copy_with_test.dart
fileavoid_redundant_argument_values
lint ruleExpected behaviour
No info should be reported.
Additional context
I believe this is caused by
copyWith
getter being typed as_$TestClassCWProxy
.call
method in_$TestClassCWProxy
is defined aswhich in fact has
null
default value forfield
. Linter doesn't take into account the actual implementation which has a different default value so false positive info is reported.The text was updated successfully, but these errors were encountered: