type-literal-in-constant-pattern

Same as type_literal_in_constant_pattern in Dart lints

Path
lint/correctness/type-literal-in-constant-pattern
Default
On (recommended)
Origin
Dart lints
Rule
445 of 512
recommended

Don't use constant patterns whose expressions have the core Type type.

This includes constant variables and static fields typed as Type, not just direct type names. Patterns explicitly wrapped in const (...) and patterns matched against Type values are excluded. Qualified constants report the complete constant expression, including their import or class prefix but excluding an enclosing null-check pattern's ?.

Invalid

example.dartdart
class LocalType {}

void check(Object? value) {
  if (value case int) {
    print('int');
  }
  if (value case LocalType) {
    print('local');
  }
  switch (value) {
    case String:
      break;
    default:
      break;
  }
}

void dynamicTypePattern(Object? value) {
  switch (value) {
    case dynamic:
      break;
    default:
      break;
  }
}

void dynamicNestedPatterns(Object? value) {
  if (value case dynamic) {}
  if (value case [dynamic]) {}
  final result = switch (value) {
    dynamic => true,
    _ => false,
  };
  print(result);
}
Use a type pattern or wrap the type literal in `const (...)`.
3void check(Object? value) {
4 if (value case int) {
Use a type pattern or wrap the type literal in `const (...)`.
6 }
7 if (value case LocalType) {
Use a type pattern or wrap the type literal in `const (...)`.
10 switch (value) {
11 case String:
Use a type pattern or wrap the type literal in `const (...)`.
19 switch (value) {
20 case dynamic:
Use a type pattern or wrap the type literal in `const (...)`.
27void dynamicNestedPatterns(Object? value) {
28 if (value case dynamic) {}
Use a type pattern or wrap the type literal in `const (...)`.
28 if (value case dynamic) {}
29 if (value case [dynamic]) {}
Use a type pattern or wrap the type literal in `const (...)`.
30 final result = switch (value) {
31 dynamic => true,

Valid

example.dartdart
import 'missing.dart' as prefix;

class LocalType {}
enum State { ready }

void typePatterns(Object? value) {
  if (value case int _) {}
  if (value case LocalType()) {}
  if (value case const (int)) {}
  if (value case State.ready) {}
  if (value case prefix.RemoteType) {}
}

void shadowed(Object? value, Object? LocalType) {
  if (value case LocalType) {}
  {
    const int = 1;
    if (value case int) {}
  }
}

class MemberShadow {
  final Object? LocalType = null;

  void check(Object? value) {
    if (value case LocalType) {}
  }
}

// Matching Type values against type literals is intentional.
void typeValues(Type value) {
  if (value case int) {}
  switch (value) {
    case String:
      break;
    default:
      break;
  }
}

void boundedTypeValues<T extends Type>(T value) {
  if (value case int) {}
}

void dynamicTypedPatterns(Object? value) {
  if (value case dynamic _) {}
  if (value case dynamic item) {
    print(item);
  }
  if (value case const (dynamic)) {}
}

void dynamicTypeValues(Type value) {
  if (value case dynamic) {}
  switch (value) {
    case dynamic:
      break;
    default:
      break;
  }
}

Configure type-literal-in-constant-pattern

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "type-literal-in-constant-pattern": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/type-literal-in-constant-pattern: <reason>
  • undeclared-dependenciescross-file

    Report package imports and exports missing from the enclosing pubspec.

  • unnecessary-flutter-importsrecommendedflutter

    Disallow unused Flutter framework and dart:async imports.

  • unnecessary-nullablecheck-unnecessary-nullablecross-file

    Flags private function or method parameters whose nullable type is unnecessary.

  • unnecessary-nullable-return-typeunnecessary_nullable_return_type

    Disallow nullable return types on functions that never return null.

  • unnecessary-statementsunnecessary_statementsrecommended

    Flags expression statements whose expression has no clear effect.

  • unrelated-type-equality-checksunrelated_type_equality_checksrecommended

    Reports ==/!= comparisons and relational ==/!= patterns whose operand types are unrelated.

  • unused-codecheck-unused-codecross-file

    Report public top-level declarations that nothing references.

  • unused-dependenciescross-file

    Report regular pubspec dependencies with no Dart import or export in the analyzed package.