Skip to content
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

[Super parameters] Super Key for Widgets #48985

Closed
AlexV525 opened this issue May 9, 2022 · 4 comments
Closed

[Super parameters] Super Key for Widgets #48985

AlexV525 opened this issue May 9, 2022 · 4 comments
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@AlexV525
Copy link
Contributor

AlexV525 commented May 9, 2022

Describe the issue

When I was trying to migrate the key parameter to a super parameter, I got a linter hint that the key is never used.

Before: image
After: image

Could it be a false-positive? But the _MethodListView is a private class here, so it could be somehow valid in this case.

Expected behavior
I expected the key here not to be detected as unused, as providing the key parameter is a preferred rule in Flutter. And the previous style is valid since the key is actually used in the super call.

@bwilkerson bwilkerson transferred this issue from dart-lang/linter May 9, 2022
@bwilkerson bwilkerson added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-warning Issues with the analyzer's Warning codes labels May 9, 2022
@bwilkerson
Copy link
Member

@srawlins

@keertip keertip added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) P2 A bug or feature request we're likely to work on labels May 9, 2022
@asashour
Copy link
Contributor

I am suspecting there are two things:

  • Is the parameter used in the method
  • Is the parameter being passed in the caller invocation as an argument

Consider

class A {
  A({this.s});

  String? s;
}

class _B extends A {
  _B({super.s});
}

class _C extends A {
  _C({String? s}) : super(s: s);
}

class _D extends A {
  _D({String? s}) : super();
}

void f() {
  _B();
  _C();
  _D();
//  _B(s: 'a'); // uncomment this
//  _C(s: 'a');
//  _D(s: 'a');
}

the super.s in _B is marked as unused, however once the _B(s: 'a') is used, the parameter is considered used.

_C has always s considered used, because it it used to call the super constructor.

_D behaves as _B

From the documentation of the diagnostic

Assuming that no code in the library passes a value for y in any invocation of _m, the following code produces this diagnostic:

I am guessing the behavior of _B and _D is correct, but _C should actually be changed, so that it is considered used only when the parameter is passed in an invocation, and not just being used in the method body.

@AlexV525
Copy link
Contributor Author

We're facing more of this when we apply the rule to production projects, as we have various file-read-only widgets that have the key argument.

I am guessing the behavior of _B and _D is correct, but _C should actually be changed, so that it is considered used only when the parameter is passed in an invocation, and not just being used in the method body.

Indeed. IMO private constructors have better callers finding like arguments usage. A super argument seems to be defined as a private field here.

@mraleph
Copy link
Member

mraleph commented May 18, 2022

Duplicate of #49025

@mraleph mraleph marked this as a duplicate of #49025 May 18, 2022
@mraleph mraleph closed this as completed May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants