Skip to main content

avoid-banned-annotations

added in: 1.11.0
⚙️🛠
Pro+

Warns against using banned annotations.

note

This rule requires configuration in order to highlight any issues.

⚙️ Config

Set entries (default is empty) to configure the list of entries to ban.

Each entry is an object with 4 fields (paths, annotations, message and severity):

  • Set paths (can be a regular expression, optional) to configure the list of paths the entry will apply to. If unset, applies to all files.
  • Set annotations (can be a regular expression) to configure the list of annotations to ban.
  • Set message to configure a user-facing message for each issue created from this config entry.
  • Set severity (optional) to override default severity for the given entry.
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-banned-annotations:
entries:
- paths: ['some/folder/.*\.dart', 'another/folder/.*\.dart']
annotations: ['visibleForTesting']
message: 'Do not use visibleForTesting here.'
severity: error
note

By default, the 'visibleForTesting' RegExp will match any annotation name containing this string. For exact matches, use ^ and $.

warning

For Windows devices, ensure that the paths config works with Windows path separators.

Example

❌ Bad:

 // LINT: Use of this annotation is not allowed (Do not use visibleForTesting here).
class Person {
Person({
required this.value,
this.another,
});

final int value;
final int? another;

// LINT: Use of this annotation is not allowed (Do not use visibleForTesting here).
void method() {}
}

✅ Good:

class Person {
Person({
required this.value,
this.another,
});

final int value;
final int? another;

void method() {}
}