Skip to main content

banned-usage

added in: 1.3.0
⚙️

Configure some usages that you want to ban.

Example: When you add some extra functionalities to built-in Flutter functions (such as logging for showDialog), you may want to ban the original Flutter function and use your own version.

warning

When trying to ban some methods in your package, it also triggers on imported from an external package code. But you can specify the name to ban (ex: 'DateTime.now' instead of 'now').

info

This rule is an improved version of ban-name and they should not be used together.

⚙️ Config

Set entries (default is empty) to configure a list of entries for usages to ban.

Each entry is either an object with 3 fields (ident, description and severity) or a type entry (type and entries).

Set type to configure the type for which the usages should be banned.

Set ident to configure the identifier name.

Set description to configure a user-facing message for each issue created from this config entry.

Set severity to override default severity for the given entry.

dart_code_metrics:
...
rules:
...
- banned-usage:
entries:
- type: List
entries:
- ident: sort
description: Use "sorted" instead
- ident: reversed
description: Do not reverse a list
- ident: showDialog
description: Please use myShowDialog in this package
- ident: strangeName
description: The name is too strange
- ident: AnotherStrangeName
description: Oops
- ident: StrangeClass.someMethod
description: Please use a NonStrangeClass.someMethod instead
- ident: DateTime.now
description: Please use a clock.now instead
severity: error
note

This rule requires configuration in order to highlight any issues.

Example

❌ Bad:

// suppose the configuration is the one shown above

showDialog('some_arguments', 'another_argument'); // LINT
material.showDialog('some_arguments', 'another_argument'); // LINT

var strangeName = 42; // LINT

void strangeName() {} // LINT

// LINT
class AnotherStrangeName {
late var strangeName; // LINT
}

StrangeClass.someMethod(); // LINT
NonStrangeClass.someMethod();

DateTime.now(); // LINT
DateTime.now().day; // LINT
class DateTimeTest {
final currentTimestamp = DateTime.now(); // LINT
}
DateTime currentTimestamp = DateTime('01.01.1959');

final list = [1, 2, 3];
list.sort(); // LINT

✅ Good:

myShowDialog();
NonStrangeClass.someMethod();

clock.now();
clock.now().day;

class DateTimeTest {
final currentTimestamp = clock.now();
}

final list = [1, 2, 3];
final newList = list.sorted();