use_existing_destructuring
v0.4.0 Warning Fix Pattern Matching
When an object already has a destructuring declaration in the same scope, accessing additional properties directly on that object is inconsistent. The property should be added to the existing destructuring pattern instead, keeping all extractions in one place.
Why use this rule
Section titled “Why use this rule”Mixing destructuring and direct property access on the same variable is confusing. If you already have final Config(:name) = config;, then accessing config.timeout separately misses the opportunity to keep all property extractions together. Adding :timeout to the existing pattern is cleaner and avoids repetition of the variable name.
See also: Dart patterns
class Config { final String name; final int timeout; final bool verbose;
const Config({ required this.name, required this.timeout, required this.verbose, });}
// Accessing property directly when destructuring already existsvoid badDirectAccess(Config config) { final Config(:name) = config; print(config.timeout);}
// Multiple undeclared property accessesvoid badMultipleAccesses(Config config) { final Config(:name) = config; print(config.timeout); print(config.verbose);}// All needed properties are destructuredvoid goodFullDestructuring(Config config) { final Config(:name, :timeout) = config; print(name); print(timeout);}
// No destructuring exists (no lint)void goodNoDestructuring(Config config) { print(config.name); print(config.timeout);}
// Access appears before the destructuring declaration (no lint)void goodBeforeDestructuring(Config config) { print(config.timeout); final Config(:name) = config; print(name);}
// Different variable than the one being destructuredvoid goodDifferentVariable(Config a, Config b) { final Config(:name) = a; print(b.timeout);}Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: use_existing_destructuring: trueTo turn it off again:
rules: use_existing_destructuring: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_single_field_destructuring— Avoid destructuring a single field when direct property access is simpler.use_existing_variable— Use an existing variable instead of repeating its initializer expression.avoid_wildcard_cases_with_enums— Keep exhaustiveness checking by listing enum cases explicitly.prefer_switch_with_enums— Use a switch instead of an if-else chain over enum constants.