Skip to main content

pass-correct-accepted-type

effort: 3m
teams+

Warns when an invocation receives an argument that does not match the type listed in the @AcceptedTypes() annotation.

@AcceptedTypes() helps handle cases when you have an Object field or parameter and pass various types (e.g. String and int). Since the closest parent class is Object, there is no better built-in type to use, but it becomes easy to pass a bool value or any other value that does not match the two expect types. This rule helps ensure that only the types listed in the annotation (or their subclasses) are passed to the invocation.

info

To use the @AcceptedTypes() annotation, install the dart-code-metrics-annotations package.

Example

❌ Bad:

void fn() {
// LINT: This type does not match any of the @AcceptedTypes types.
// Try passing another expression or updating the annotation.
fn(1);

// LINT: This type does not match any of the @AcceptedTypes types.
// Try passing another expression or updating the annotation.
SomeClass(false);
}

void fn(({String, bool}) Object value) {}

class SomeClass {
({String, int})
final Object field;

const SomeClass(this.field);
}

✅ Good:

void fn() {
fn('str');
SomeClass(1);
}

void fn(({String, bool}) Object value) {}

class SomeClass {
({String, int})
final Object field;

const SomeClass(this.field);
}