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

unused_element reports on unused parameters whose presence is idiomatic #49025

Open
maxlapides opened this issue May 12, 2022 · 32 comments
Open
Assignees
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. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@maxlapides
Copy link

Assumptions:

  • Flutter 3.0
  • user_super_parameters lint rule is enabled
  • unused_element analyzer rule is enabled

This code chunk produces an analyzer issue:

class _MyWidget extends StatelessWidget {
  const _MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

"A value for optional parameter 'key' isn't ever given. Try removing the unused parameter."

But, when the same widget is not private, there is no issue:

class MyWidget extends StatelessWidget {
  const MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

I would like to keep the super.key on the private widgets, but I also want to keep the unused_element rule enabled. Is there any way to achieve this?

❯ flutter doctor -v
[✓] Flutter (Channel stable, 3.0.0, on macOS 12.3.1 21E258 darwin-arm, locale en-US)
    • Flutter version 3.0.0 at /Users/maxlapides/fvm/versions/3.0.0
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ee4e09cce0 (2 days ago), 2022-05-09 16:45:18 -0700
    • Engine revision d1b9a6938a
    • Dart version 2.17.0
    • DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/maxlapides/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)

[✓] VS Code (version 1.67.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.40.0

[✓] Connected device (3 available)
    • iPhone 13 (mobile) • 8A362B17-9737-4B87-B0E4-77BF160D0A50 • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-15-4 (simulator)
    • macOS (desktop)    • macos                                • darwin-arm64   • macOS 12.3.1 21E258 darwin-arm
    • Chrome (web)       • chrome                               • web-javascript • Google Chrome 101.0.4951.64

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
@bwilkerson
Copy link
Member

@srawlins

@srawlins
Copy link
Member

More or less a duplicate of #48401

@srawlins srawlins added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label May 16, 2022
@srawlins srawlins transferred this issue from dart-lang/linter May 16, 2022
@srawlins
Copy link
Member

I would like to keep the super.key on the private widgets, but I also want to keep the unused_element rule enabled. Is there any way to achieve this?

No I don't think so. If you want to keep an unused parameter on your private widget, you will keep getting an unused_element report.

@srawlins
Copy link
Member

How does this relate to user_super_parameters? If you remove the named parameter (and pass null directly) do you get a user_super_parameters report?

@maxlapides
Copy link
Author

maxlapides commented May 16, 2022

@srawlins It's related to the super parameters because this breaks unused_element:

class _MyWidget extends StatelessWidget {
  const _MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

But this does not:

class _MyWidget extends StatelessWidget {
  const _MyWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

This doesn't really have much to do with the lint rule use_super_parameters, but rather just the new Dart 2.17 super parameters syntax.

@maxlapides maxlapides changed the title use_super_parameters and unused_element conflict in private Flutter widgets Dart 2.17 super parameters and unused_element conflict in private Flutter widgets May 16, 2022
@srawlins
Copy link
Member

Oops, that second example should also have an unused_element report, since no argument is ever given.

@maxlapides
Copy link
Author

It's super common in Flutter to have this key parameter on all widgets, regardless of whether the key parameter is ever used. It would be great to be able to disable unused_element just for the keys in Flutter.

@srawlins
Copy link
Member

I can update the title of this issue to make that the ask.

@maxlapides
Copy link
Author

@srawlins That would be great, thanks!

@srawlins srawlins changed the title Dart 2.17 super parameters and unused_element conflict in private Flutter widgets unused_element reports on unused parameters whose presence is idiomatic May 16, 2022
@srawlins srawlins added analyzer-warning Issues with the analyzer's Warning codes type-enhancement A request for a change that isn't a bug P4 labels May 16, 2022
@orevial
Copy link

orevial commented May 17, 2022

@srawlins @maxlapides this issue looks perfectly legit to me, why would you want to shut a warning about an unused element ?

Implementing super with key is legit on a public widget because you could want to export it as a library widget in which case you want to make sure you offer the possibility to give a key to anyone using your widget. However in the case of a private widget, if you are not using the key when calling your widget, why declare the key ?

To go further, I don't think that the new super parameters syntax has an issue, on the contrary I think that the old syntax should also trigger this unused_element warning on private widgets, to me that's the real issue 🙂

@goderbauer
Copy link
Contributor

I agree with the previous comment. There's no reason to have the key parameter on a private widget if nobody is using it. If you need to pass the key, you can always add it to the private widget since you're in full control of the code.

For a public widget (that may get exported from a library you don't have control over) you want to make sure it can always take a key. If the key parameter is omitted, you'd have to ask the library author to release a new version with the key parameter added.

@maxlapides
Copy link
Author

I'm not strongly opposed to removing the key parameters on the private widgets, but I do think there's a reasonable argument to be made that it is less work to have the super.key on all the private widgets because it's generated by a code snippet. Then, it's always available when you need it later, you don't need to go add it manually by editing a file that you may not have otherwise needed to touch.

@khobotin-elopage
Copy link

I agree with Max. It's common for widgets to have key parameter whether they are public or private.
And it seems there have been an exception for the key parameter in unused_element rule because this

const _MyWidget({Key? key}) : super(key: key);

looks legit for it.

Also it's more likely to forget to add the key whenever I want to make my private widget a public.

@srawlins
Copy link
Member

If there is not a report for unused_element for const _MyWidget({Key? key}) : super(key: key);, then I believe there is a bug in unused_element.

@orevial
Copy link

orevial commented May 19, 2022

I understand your point of view although I disagree :

  • IDE's snippets adding the key automatically when generating widgets is not a good reason IMO to implement an exception to this rule, the IDE snippet should follow language conventions and not the opposite
  • I understand the argument about the risk of forgetting to add key on public widgets but there is a linter rule specifically designed for this case : use_key_in_widget_constructors which will make sure you never forget a key on public widgets 😉

@srawlins
Copy link
Member

srawlins commented May 19, 2022

Generally speaking, the unused_* and unnecessary_* rules come from a policy of rejecting the "but I might need it later" argument in favor of highlighting all unused and unnecessary code. I think these rules will always report as much unused and unnecessary code as is technically correct.

If a dev or a team firmly believes some code should remain, however unused, they can disable the rules or suppress them by various means. If disabling "unused_element" across a codebase is too big a hammer, we may split a diagnostic out, as per #48401, for unused parameters, as this argument has come up a few times, "but I might need it later."

dmiedev added a commit to dmiedev/spacex that referenced this issue Jul 23, 2022
dmiedev added a commit to dmiedev/spacex that referenced this issue Jul 24, 2022
* feat: customize app theme

* feat: implement basic launch page

* feat: add launch state handling

* feat: implement launch card

* feat: add floating search bar to LaunchPage

* feat: add filtering chips

* feat: replace SliverList with SliverGrid

To support different screen sizes better.

* feat: add last launch page handling

* feat: add loading indicators to launch grid

* feat: add searching to LaunchPage

* fix: make search bar clear button work properly

* feat: add no items found indicator to grid of launch cards

* fix: ensure the binding is initialized before getting storage directory

* feat: add first page error indicator to launch grid

* feat: add new page error indicator to launch grid

* refactor: break up the launch page into different widgets

* feat: add filtering chip dialogs

* fix: make buttons in dialogs white

* feat: implement launch page sorting

* feat: add time filtering to launch page

* refactor: change launch state handling

* refactor: shorten launch bloc code

* feat: add launch successfulness filtering

* feat: add launch flight number filtering

* feat: add launch year filtering

* refactor: split up the launches feature into two features

* feat: add launch rocket filtering

* feat: add checkbox theme

* refactor: remove excessive event addition to launch bloc

* feat: create spacex_ui package with common widgets

* refactor: split up filtering chips file into multiple files

* refactor: use spacex_ui widgets in the rest of the app

* feat: add string localization

* docs: add missing project documentation

* refactor: remove key parameter from private widgets

Linter false positive: dart-lang/sdk#49025

* fix: fix FilteringChip layout

* refactor: obtain widget colors from Theme

* feat: change dialog text button color

* test: add basic app test
@rrousselGit
Copy link

Clearly, this behavior is controversial. The reactions speak for themselves I think. There's a decent amount of upvotes on both sides of the argument.

That's the issue.
This feature is controversial, yet it got added to one of the most used lints. As such, disabling the lint globally is unreasonable. Adding // ignore for all cases is unreasonable too.

This ends up negatively impacting a large number of people with no real solution.

Extracting this behavior into a new lint rule would lead to a happy ending for everyone. Those who like this can keep it. Those who don't can disable it.

@srawlins srawlins added P3 A lower priority bug or feature request and removed P4 labels Oct 27, 2022
@dnfield
Copy link
Contributor

dnfield commented Nov 10, 2022

In flutter/tests#172, I believe this is the cause of dart fix removing a parameter that never gets set, which results in compilation errors since the program is depending on the parameter to initialize a default value fo ra final variable.

@s0nerik
Copy link

s0nerik commented Mar 21, 2023

Any updates?

The IntelliJ IDEA plugin generates a widget with super.key for extracted widgets (Cmd + Ctrl + W), which I need to manually adjust each time now to Key? key just to bypass the lint.

I'd love to have either a separate lint that ignores this specific case, as mentioned above, or at the very least have an option for the IDEA plugin to generate Key? key in the widget constructors.

@srawlins
Copy link
Member

No updates.

I'd love to have either a separate lint that ignores this specific case, as mentioned above

That's probably the direction we'll go.

@srawlins
Copy link
Member

Oops, I think we implemented that. You can now // ignore_for_file: unused_element_parameter or ignore unused_element_parameter in your analysis_options.yaml, and you're good to go... I don't think there is anything else to do here.

@dnfield the dart fix issue would be a separate issue.

@maxlapides
Copy link
Author

@srawlins Great, unused_element_parameter is exactly what I'm looking for! Could you please show how I can use it in analysis_options.yaml to ignore this for the entire project? I can't seem to figure out the syntax :)

@srawlins
Copy link
Member

No worries, I always forget it too :)

Here's the syntax: https://dart.dev/guides/language/analysis-options#ignoring-rules

@maxlapides
Copy link
Author

@srawlins Strangely, I am getting this warning 'unused_element_parameter' isn't a recognized error code. dart(unrecognized_error_code):

analyzer:
  errors:
    unused_element_parameter: ignore

@srawlins
Copy link
Member

Ah ok if that doesn't work then there is remaining work. I have an idea.

@srawlins srawlins self-assigned this May 14, 2023
@xVemu
Copy link

xVemu commented Jul 12, 2023

Any news?

@srawlins
Copy link
Member

No news.

copybara-service bot pushed a commit that referenced this issue Jul 24, 2023
This reverts commit a458bab.

Reason for revert: broke flutter snippet tests, and maybe customer tests

Work towards flutter/flutter#131096

Original change's description:
> Separate out UNUSED_ELEMENT_PARAMETER from UNUSED_ELEMENT
>
> Fixes #49025
>
> Change-Id: I401093e5b76bcf707060ce022c346e26c6807aa0
> Tested: try-bots
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/303261
> Commit-Queue: Samuel Rawlins <srawlins@google.com>
> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
> Reviewed-by: Ben Konyi <bkonyi@google.com>
> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>

Change-Id: Ib0c9afc4911efd620013896fb895ab7825c50aa4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/315940
Commit-Queue: Siva Annamalai <asiva@google.com>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Siva Annamalai <asiva@google.com>
@srawlins srawlins reopened this Jul 24, 2023
@srawlins
Copy link
Member

srawlins commented Jul 24, 2023

Had to revert the fix; broke various flutter things. See flutter/flutter#131096

@antonxandre
Copy link

@srawlins Strangely, I am getting this warning 'unused_element_parameter' isn't a recognized error code. dart(unrecognized_error_code):

analyzer:
  errors:
    unused_element_parameter: ignore

Works to me with:

analyzer:
  errors:
    unused_element: ignore

@iska9der
Copy link

Don't worry, I'm just a reminder: we still need a solution to this problem

@kuyazee
Copy link

kuyazee commented Mar 27, 2024

@srawlins Strangely, I am getting this warning 'unused_element_parameter' isn't a recognized error code. dart(unrecognized_error_code):

analyzer:
  errors:
    unused_element_parameter: ignore

Works to me with:

analyzer:
  errors:
    unused_element: ignore

This worked for me on flutter version 3.19.4

@lore-co
Copy link

lore-co commented Aug 1, 2024

Any news?

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. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests