dart-run-static-analysis
Execute `dart analyze` to identify warnings and errors, and use `dart fix --apply` to automatically resolve mechanical lint issues. Use during development to ensure code quality and before committing
SKILL.mdskills/dart/dart-run-static-analysis/SKILL.md
GitHubContents
- Analysis Configuration
- Diagnostic Suppression
- Workflow: Executing Static Analysis
- Workflow: Applying Automated Fixes
- Examples
Analysis Configuration
Configure the Dart analyzer using analysis_options.yaml at the package root.
- Base Configuration: Include a standard rule set (e.g.,
package:lints/recommended.yamlorpackage:flutter_lints/flutter.yaml). - Strict Type Checks: Enable
strict-casts: true,strict-inference: true, andstrict-raw-types: true. - Linter Rules: Enable or disable specific rules under
linter: rules:. - Formatter Configuration: Set
page_widthandtrailing_commasunderformatter:. - Analyzer Plugins: Add plugins under
analyzer: plugins:.
Diagnostic Suppression
- File-level Exclusion: Use
analyzer: exclude:with glob patterns (e.g.,**/*.g.dart). - File-level Suppression:
// ignore_for_file: <diagnostic_code>. - Line-level Suppression:
// ignore: <diagnostic_code>. - Pubspec Suppression:
# ignore: <diagnostic_code>.
Workflow: Executing Static Analysis
-
- Verify
analysis_options.yamlexists at the project root.
- Verify
-
- Run
dart analyze <target_directory>.
- Run
-
- Review the diagnostic output.
-
- If info-level issues must be failures, use
--fatal-infosflag.
- If info-level issues must be failures, use
-
- Resolve reported errors or proceed to Automated Fixes workflow.
Workflow: Applying Automated Fixes
-
- Preview changes:
dart fix --dry-run.
- Preview changes:
-
- Review the proposed fixes.
-
- Verify corresponding linter rules are enabled.
-
- Apply fixes:
dart fix --apply.
- Apply fixes:
-
- Format code:
dart format ..
- Format code:
-
- Re-run static analysis to verify all diagnostics are resolved.
Example: Comprehensive analysis_options.yaml
include: package:flutter_lints/recommended.yaml
analyzer:
exclude:
- "**/*.g.dart"
- "lib/generated/**"
language:
strict-casts: true
strict-inference: true
strict-raw-types: true
errors:
todo: ignore
invalid_assignment: warning
missing_return: error
linter:
rules:
avoid_shadowing_type_parameters: false
await_only_futures: true
use_super_parameters: true
formatter:
page_width: 100
trailing_commas: preserve
Example: Inline Diagnostic Suppression
// ignore_for_file: unused_local_variable, dead_code
void processData() {
// ignore: invalid_assignment
int x = '';
const y = 10; // ignore: constant_identifier_names
}