Skip to content

[Linter] Proposal: prefer_wildcard_variables lint rule #59638

@ricardodalarme

Description

@ricardodalarme

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.

Metadata

Metadata

Assignees

Labels

P2A bug or feature request we're likely to work ondevexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.linter-lint-proposaltype-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions