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

Dart fix for unused_element removes parameters when it shouldn't #54190

Open
gspencergoog opened this issue Nov 30, 2023 · 6 comments
Open

Dart fix for unused_element removes parameters when it shouldn't #54190

gspencergoog opened this issue Nov 30, 2023 · 6 comments
Labels
analyzer-dartfix Issues with the dartfix package 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

@gspencergoog
Copy link
Contributor

When applying dart fix --apply to the following code:

void main() {
  final _Foo foo = _Foo();
  print('$foo');
}

class _Foo {
  _Foo({this.bar}); // The "bar" parameter is never given a value, but it's nullable.

  final String? bar;
}

The bar parameter is never given a value, so the unused_element lint shows up for that line:

   info • bug.dart:7:14 • A value for optional parameter 'bar' isn't ever given. Try removing the unused parameter. • unused_element

Running dart fix --apply to the code yields:

void main() {
  final _Foo foo = _Foo();
  print('$foo');
}

class _Foo {
  _Foo();

  final String? bar;
}

Which yields the following error:

  error • bug.dart:7:3 • All final variables must be initialized, but 'bar' isn't. Try adding an initializer for the field. • final_not_initialized_constructor

The Dart Fix for unused_element shouldn't remove parameters that are nullable, since they have implicit default values. Alternatively, it could also remove the uninitialized variable, but I suspect that has tons of corner cases.

@gspencergoog
Copy link
Contributor Author

cc @goderbauer

@bwilkerson
Copy link
Member

I would go one step farther. I don't think that unused_element should flag that parameter because it's being used to initialize the field, even when no argument is passed in.

@srawlins

@bwilkerson bwilkerson added 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 labels Nov 30, 2023
@srawlins
Copy link
Member

I believe the fix for an unused field-formal parameter (this.) should remove the parameter, and add bar=null to the initializer list.

I would go one step farther. I don't think that unused_element should flag that parameter because it's being used to initialize the field, even when no argument is passed in.

I don't think that's a reason to pollute the parameter list of a constructor. The situation should still be reported, and the fix can be to move the initialization to the constructor initializer list. The goal is to indicate unnecessary complexity, perhaps unnecessary fields (such as the final field which is only ever initialized with a value of null).

@gspencergoog
Copy link
Contributor Author

I assume this is what the end result above would be:

void main() {
  final _Foo foo = _Foo();
  print('$foo');
}

class _Foo {
  _Foo() : bar = null;

  final String? bar; // Would this line get a lint warning that says it's always null?
}

So, in that case, would there also now be a lint warning for the final field that is always null? Presumably there's no dart fix for that because of the possible complexity involved in removing the member and all of its uses.

@srawlins
Copy link
Member

We actually don't produce any warnings on that last code. We also don't produce a warning if you have a final field with an initializer like final foo = 'hello';.

@gspencergoog
Copy link
Contributor Author

Oh, sure, I guess it could easily be the case that someone wants a base class to have a final field that is always null that they then override the getter for in a subclass.

@srawlins srawlins added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Apr 5, 2024
@keertip keertip added the analyzer-dartfix Issues with the dartfix package label Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-dartfix Issues with the dartfix package 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

4 participants