[linter] Improve do_not_use_environment lint error message and documentation #60970
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Improves the
do_not_use_environment
lint rule error message and documentation to be more helpful and actionable.Problem
The current error message is confusing and unhelpful:
Solution
After: "Avoid using environment values like 'bool.hasEnvironment' which create hidden global state."
Key Improvements:
Specific Method Names: Error message now includes the exact method being used (e.g., "bool.hasEnvironment", "String.fromEnvironment")
Clear Explanation: Explains that environment constructors create "hidden global state" which makes code hard to understand and maintain
Actionable Guidance: Suggests using
Platform.environment
for runtime access insteadComprehensive Documentation:
Better Test Coverage: Added tests for all variants and edge cases
Examples
Before
After
Documentation Improvements
Added clear examples in the lint documentation:
BAD:
GOOD:
Files Changed
pkg/linter/messages.yaml
- Updated message and documentationpkg/linter/lib/src/rules/do_not_use_environment.dart
- Pass method name to errorpkg/linter/lib/src/lint_codes.g.dart
- Generated code updatepkg/linter/test/rules/do_not_use_environment_test.dart
- Enhanced test coverageTest Plan
bool.hasEnvironment('APPENGINE_RUNTIME')
)Fixes #59203