type-literal-in-constant-pattern
Same as type_literal_in_constant_pattern in Dart lints
- Group
- Correctness
- Path
lint/correctness/type-literal-in-constant-pattern- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 445 of 512
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
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);
}void check(Object? value) { if (value case int) { } if (value case LocalType) { switch (value) { case String: switch (value) { case dynamic:void dynamicNestedPatterns(Object? value) { if (value case dynamic) {} if (value case dynamic) {} if (value case [dynamic]) {} final result = switch (value) { dynamic => true,Valid
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.
{
"linter": {
"rules": {
"correctness": {
"type-literal-in-constant-pattern": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/type-literal-in-constant-pattern: <reason>More correctness rules
- undeclared-dependenciescross-file
Report package imports and exports missing from the enclosing pubspec.
- unnecessary-flutter-importsrecommendedflutter
Disallow unused Flutter framework and
dart:asyncimports. - unnecessary-nullable
check-unnecessary-nullablecross-fileFlags private function or method parameters whose nullable type is unnecessary.
- unnecessary-nullable-return-type
unnecessary_nullable_return_typeDisallow nullable return types on functions that never return null.
- unnecessary-statements
unnecessary_statementsrecommendedFlags expression statements whose expression has no clear effect.
- unrelated-type-equality-checks
unrelated_type_equality_checksrecommendedReports
==/!=comparisons and relational==/!=patterns whose operand types are unrelated. - unused-code
check-unused-codecross-fileReport public top-level declarations that nothing references.
- unused-dependenciescross-file
Report regular pubspec dependencies with no Dart import or export in the analyzed package.