Skip to content

keyword_on_wildcard

Reports var or final before a wildcard of a pattern. Delete the keyword.

Tier
advisory
Scope
Dart
Lowest preset
recommended
Fix
deletes the keyword

var or final before a wildcard of a pattern: case final _:, if (value case var _), final int _ =>, Point(x: final _). The violation is on the keyword.

It does not report:

  • the loop variable for (final _ in items) or a declaration final _ = start();, which are not patterns;
  • a named pattern variable.

A wildcard binds no variable, so there is nothing for the keyword to make final. final _ matches what _ matches, and final int _ what int _ matches.

Violating
int kind(Object value) => switch (value) {
final int _ => 1,
_ => 0,
};
Corrected
int kind(Object value) => switch (value) {
int _ => 1,
_ => 0,
};

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// Why it stays as written here.
// ignore: keyword_on_wildcard

// ignore_for_file: assay/keyword_on_wildcard silences it in a whole file, and // ignore: assay/keyword_on_wildcard with the prefix works too. See Suppressions.