avoid-unrelated-type-casts

Path
lint/correctness/avoid-unrelated-type-casts
Default
On (recommended)
Origin
Falcon
Rule
179 of 512
recommended

Flags casts between types proven unrelated by canonical type resolution.

Checks as expressions, cast patterns, and SDK collection cast<T>() element/key/value conversions. Dynamic, unresolved, and potentially related types are not reported. Collection casts are lazy but fail when an element of the incompatible type is read.

Invalid

example.dartdart
void casts(String text, int number, List<int> numbers, Set<int> unique, Map<String, int> map) {
  print(text as int);
  print(number as String);
  print(numbers as List<String>);
  print(numbers.cast<String>());
  print(unique.cast<String>());
  print(map.cast<int, int>());
  print(map.cast<String, String>());
}

class Animal {}
class NotAnimal {}
void unrelatedClasses(Animal animal) {
  print(animal as NotAnimal);
}
void castPattern(String text) {
  if (text case final number as int) print(number);
}
Cast types are unrelated — use a compatible type instead.
1void casts(String text, int number, List<int> numbers, Set<int> unique, Map<String, int> map) {
2 print(text as int);
Cast types are unrelated — use a compatible type instead.
2 print(text as int);
3 print(number as String);
Cast types are unrelated — use a compatible type instead.
3 print(number as String);
4 print(numbers as List<String>);
Cast types are unrelated — use a compatible type instead.
4 print(numbers as List<String>);
5 print(numbers.cast<String>());
Cast types are unrelated — use a compatible type instead.
5 print(numbers.cast<String>());
6 print(unique.cast<String>());
Cast types are unrelated — use a compatible type instead.
6 print(unique.cast<String>());
7 print(map.cast<int, int>());
Cast types are unrelated — use a compatible type instead.
7 print(map.cast<int, int>());
8 print(map.cast<String, String>());
Cast types are unrelated — use a compatible type instead.
13void unrelatedClasses(Animal animal) {
14 print(animal as NotAnimal);
Cast types are unrelated — use a compatible type instead.
16void castPattern(String text) {
17 if (text case final number as int) print(number);

Valid

example.dartdart
import 'dart:async';

void casts(Object value, dynamic unknown, List<num> numbers, Map<Object, Object> map) {
  print(value as String);
  print(unknown as int);
  print(numbers as List<int>);
  print(numbers.cast<int>());
  print(numbers.cast<Object>());
  print(map.cast<String, int>());
}
class Custom {
  Custom cast<T>() => this;
}
void custom(Custom item) { item.cast<String>(); }

extension type Id(int value) {}
void erasedAndStructural<T>(int number, Object value, Object recordValue, Function callback, T generic, FutureOr<int> futureOr) {
  print(number as Id);
  print(value as void Function());
  print(callback as void Function(int));
  print(recordValue as (int, String));
  print((1, 2) as Object);
  print(generic as void Function());
  print(futureOr as int);
  if (value case final callback as void Function()) print(callback);
}

Configure avoid-unrelated-type-casts

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-unrelated-type-casts": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/avoid-unrelated-type-casts: <reason>
  • avoid-unsafe-collection-methods

    Flags potentially throwing element access on SDK collections.

  • avoid-unsafe-reduce

    Warn when SDK Iterable.reduce can receive an empty collection.

  • avoid-unused-after-null-check

    Reports a null-checked local ignored by a branch that passes another local of the same resolved type to a call.

  • avoid-unused-parametersavoid-unused-parametersrecommended

    Disallow declared parameters that are never used, excluding primary constructors.

  • avoid-watch-outside-buildrecommendedprovider

    Flags listening Provider accesses proven to run outside Flutter builds.

  • avoid-web-libraries-in-flutteravoid_web_libraries_in_flutterrecommendedflutter

    Disallow importing web-only dart: libraries from Flutter code.

  • await-only-futuresawait_only_futuresrecommended

    Await only values assignable to futures; FutureOr, dynamic, bottom types and extension types are accepted like the analyzer.

  • banned-importscross-file

    Report imports forbidden by package-relative architectural boundaries.