New lint rule: warn about Color constants that match existing color constants #58525
Labels
analyzer-linter
Issues with the analyzer's support for the linter package
area-analyzer
Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
linter-lint-request
P3
A lower priority bug or feature request
type-enhancement
A request for a change that isn't a bug
Describe the rule you'd like to see implemented
Warn if a raw color value like
Color(0xFF000000)
is used instead of an equivalent color constant (e.g.Colors.black
)It is fairly easy to accidentally enter a color constant that matches a color constant that is in scope for your library and features like flutter/flutter-intellij#5780 may make it more common. Using the named color constant that is already in scope makes code a bit easier to read as the semantic meaning of the color constants is clearer.
Examples
Example from DevTools:
https://github.com/flutter/devtools/blob/a4852d2140ebe6ee81cbd32ec7e75aa8f506a0f6/packages/devtools_app/lib/src/theme.dart#L283
Here we accidentally duplicated the Material.blue color.
Additional context
Functionality added for flutter/flutter-intellij#5780 would make it easier for users to accidentally add color values that match Material colors. Having this lint and quickfix would make it easier cleanup cases where colors that should be named are used.
A quick search of G3 suggests that this lint will catch a few cases but not so many cases that fixing the issues caught would be time consuming.
Risk: need to ensure this lint would not have undesirable false positives.
The text was updated successfully, but these errors were encountered: