Skip to content

[Linter] Proposal: prefer_wildcard_variables lint rule #59638

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

Closed
ricardodalarme opened this issue Nov 30, 2024 · 5 comments
Closed

[Linter] Proposal: prefer_wildcard_variables lint rule #59638

ricardodalarme opened this issue Nov 30, 2024 · 5 comments
Assignees
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. linter-lint-proposal P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@ricardodalarme
Copy link

Proposal for New Dart Lint Rule

TLDR

Add a lint rule to prefer wildcard variables (_) over multiple underscores (_, __, ___) in unused parameters.

Motivation

The upcoming Dart 3.7.0 release introduces support for wildcard variables (_) that can be reused for multiple unused parameters. Previously, developers commonly relied on distinct variables like _, __, ___ to indicate unused parameters. This practice is now redundant with the introduction of _, which improves clarity and reduces visual noise in the codebase.

Reference

Lint Rule Implementation

Rule Name: prefer_wildcard_variables
Description: Warns when distinct variable names such as __, ___, etc. are used for unused parameters, recommending the use of _ instead.
Severity Level: WARNING (as it’s a stylistic improvement and non-breaking).

Code Examples

Bad

ListView.builder(
  itemCount: 10,
  itemBuilder: (_, __) {
    return ListTile(
      title: Text('Item'),
    );
  },
);

Good

ListView.builder(
  itemCount: 10,
  itemBuilder: (_, _) {
    return ListTile(
      title: Text('Item'),
    );
  },
);

Behavior

  • Triggers a warning: When multiple distinct unused variable names (__, ___) are declared.
  • Does not trigger: For valid wildcard use cases, such as (_, _, _).

Benefits

  1. Improved Code Readability: Reduces unnecessary differentiation in unused parameter names.
  2. Consistent Dart Style: Encourages developers to adopt features introduced in newer Dart versions.
  3. Simpler Refactoring: Avoids subtle issues where _, __, and ___ might accidentally be referenced or misinterpreted.
@dart-github-bot
Copy link
Collaborator

Summary: This proposal suggests a new Dart lint rule, prefer_wildcard_variables, to warn against using multiple underscores (_, __, ___) for unused parameters, recommending the single underscore wildcard (_) instead. This improves code readability and consistency.

@dart-github-bot dart-github-bot added legacy-area-analyzer Use area-devexp instead. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Nov 30, 2024
@lrhn lrhn added devexp-linter Issues with the analyzer's support for the linter package linter-lint-proposal and removed triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Nov 30, 2024
@srawlins
Copy link
Member

srawlins commented Dec 2, 2024

CC @pq @kallentu sounds like a win to me!

@kallentu
Copy link
Member

kallentu commented Dec 2, 2024

Ah yes, we've agreed upon making a lint that's exactly this. We may call it unnecessary_underscores or something more specific than prefer_wildcard_variables though.

For more context: #56595 (comment)

@srawlins
Copy link
Member

srawlins commented Dec 2, 2024

I like that name. too_many_underscores. why_so_many_underscores. 😁

@pq pq self-assigned this Dec 11, 2024
@bwilkerson bwilkerson added the P2 A bug or feature request we're likely to work on label Dec 16, 2024
@pq
Copy link
Member

pq commented Jan 7, 2025

Hi! This has been implemented as unnecessary_underscores and should be in 3.7.

If it doesn't behave as expected, please raise issues.

Thanks!

@pq pq closed this as completed Jan 7, 2025
mockturtl added a commit to mockturtl/tidy that referenced this issue Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. linter-lint-proposal P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants