Skip to main content

avoid-banned-types

added in: 1.4.0
⚙️
Pro+

Warns against using banned types.

note

This rule requires configuration in order to highlight any issues.

⚙️ Config

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

Each entry is an object with 4 fields (paths, types, 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 types (can be a regular expression) to configure the list of types 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.
  • Set positions (optional) to scope the check to a particular expression (current only is is supported).
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-banned-types:
entries:
- paths: ['some/folder/.*\.dart', 'another/folder/.*\.dart']
types: ['SomeType']
message: 'Do not use SomeType here.'
severity: error
- paths: ['core/.*\.dart']
types: ['CounterBloc']
message: 'State management should be not used inside "core" folder.'
- paths: ['some/folder/.*\.dart']
types: ['AnotherType']
positions: ['is']
message: 'Do not type check with "is", use checkForType() instead.'
note

By default, the 'SomeType' RegExp will match any type 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 type is not allowed (Do not use SomeType here.).
void function(SomeType someParam) {
// ...
}

// LINT: Use of this type is not allowed (Do not use SomeType here.).
if (value case SomeType(: final field)) {
// ...
}

✅ Good:

// Correct, different type
void function(SomeOtherType someParam) {
// ...
}

if (value case SomeOtherType(: final field)) {
// ...
}