Code Quality with Clippy and rustfmt
Beginner6 minTrending
Enforce code quality and consistent formatting using clippy for linting and rustfmt for automatic code formatting.
Prerequisites
- -Rust toolchain installed via rustup
Steps
1
Run clippy for lint warnings
Analyze your code with clippy to catch common mistakes and unidiomatic patterns.
$ cargo clippy -- -W clippy::pedantic
Use -W clippy::pedantic for stricter checks. Use -D warnings to treat warnings as errors in CI.
2
Auto-fix clippy suggestions
Apply automatic fixes for issues clippy can resolve.
$ cargo clippy --fix --allow-dirty
Review the changes after auto-fixing. Some fixes may change behavior in subtle ways.
3
Format code with rustfmt
Apply consistent formatting across the entire project.
$ cargo fmt
4
Check formatting without modifying files
Verify that all code is properly formatted, failing if changes are needed. Ideal for CI.
$ cargo fmt -- --check
5
Create a rustfmt configuration
Customize formatting rules with a rustfmt.toml file.
$ cat > rustfmt.toml << 'EOF'
max_width = 100
tab_spaces = 4
edition = "2021"
use_field_init_shorthand = true
EOF
Full Script
FAQ
Discussion
Loading comments...