prefer_shorthands_with_enums
v0.3.0 Warning Fix Shorthand Patterns
This rule flags LogLevel.debug in a position where the expected type is already LogLevel, because .debug says the same thing. The quick fix removes the prefix.
It fires wherever the context supplies the type: switch cases and switch expression patterns, typed variable declarations, == comparisons, default parameter values, typed named arguments, returns from a function with a declared return type, and elements of a collection that has a real element type.
See also: Dart language — dot shorthands
enum LogLevel { debug, warning, error }
void configure({LogLevel threshold = LogLevel.warning}) {} // LINT
LogLevel defaultLevel() => LogLevel.debug; // LINT
String label(LogLevel level) { final LogLevel fallback = LogLevel.warning; // LINT
if (level == LogLevel.debug) { // LINT return 'verbose'; }
return switch (level) { LogLevel.warning => 'warn', // LINT _ => 'other', };}void configure({LogLevel threshold = .warning}) {}
LogLevel defaultLevel() => .debug;
String label(LogLevel level) { final LogLevel fallback = .warning;
if (level == .debug) { return 'verbose'; }
return switch (level) { .warning => 'warn', _ => 'other', };}Named arguments and collections
Section titled “Named arguments and collections”A named argument with a declared enum type, and a collection whose element type is known, both give the compiler a context type:
enum LogLevel { debug, warning, error }
void setLevels({required List<LogLevel> levels}) {}
// Don'tvoid configure() { final List<LogLevel> verbose = [LogLevel.debug, LogLevel.warning]; setLevels(levels: [LogLevel.error]);
final Map<String, LogLevel> routes = {'api': LogLevel.warning};}
// Dovoid configure() { final List<LogLevel> verbose = [.debug, .warning]; setLevels(levels: [.error]);
final Map<String, LogLevel> routes = {'api': .warning};}Known limitations
Section titled “Known limitations”A dot shorthand is only legal where the compiler has a downward context
type. Where there is none, writing .debug fails to compile with
dot_shorthand_missing_context — so the rule stays quiet:
An untyped destination. Object asObject() => LogLevel.debug; and a dynamic or Object? parameter give the expression nothing to resolve against.
A collection in an untyped position. In expect(rankings, equals([LogLevel.debug])), equals takes Object?, so the list’s element type is inferred upward from the elements themselves — there is no context. Give the literal an explicit type argument (equals(<LogLevel>[.debug])) and it is reported.
A type argument solved from the argument. In Box(items: const [LogLevel.debug]) the analyzer infers T from the element. The type displays as List<LogLevel>, but it is upward inference and the shorthand would not compile. Box<LogLevel>(items: const [.debug]) pins the type argument and is reported.
Only the half that matches. In Map<LogLevel, Object>, the key position has a context type and the value position does not: {LogLevel.first: LogLevel.second} reports the key only.
Plain assignment to an already-declared variable. value = LogLevel.debug; after LogLevel value; is not reported.
Access through an import prefix. logging.LogLevel.debug is skipped, since the leading name is the library prefix rather than the enum.
Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_shorthands_with_enums: trueTo turn it off again:
rules: prefer_shorthands_with_enums: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_nested_shorthands— Avoid nesting a dot shorthand inside another dot shorthand invocation.prefer_returning_shorthands— Use dot shorthand constructors in expression function return values.prefer_shorthands_with_constructors— Use dot shorthand constructors for common Flutter classes.prefer_shorthands_with_static_fields— Use dot shorthands instead of explicit class prefixes for static fields.