$ cargo fix
Checking myprogram v0.1.0 (file:///projects/myprogram)
Fixing src/main.rs (1 fix)
Finished dev [unoptimized + debuginfo] target(s) in 0.59s
如果再次查看 src/main.rs,會發(fā)現 cargo fix 修改了代碼:
文件名: src/main.rs
fn do_something() {}
fn main() {
for _i in 0..100 {
do_something();
}
}
現在 for 循環(huán)變量變?yōu)?nbsp;_i,警告也不再出現。
cargo fix 命令可以用于在不同 Rust 版本間遷移代碼。版本在附錄 E 中介紹。
通過 clippy 提供更多 lint 功能
clippy 工具是一系列 lint 的集合,用于捕捉常見錯誤和改進 Rust 代碼。
安裝 clippy:
$ rustup component add clippy
對任何 Cargo 項目運行 clippy 的 lint:
$ cargo clippy
例如,如果程序使用了如 pi 這樣數學常數的近似值,如下:
文件名: src/main.rs
fn main() {
let x = 3.1415;
let r = 8.0;
println!("the area of the circle is {}", x * r * r);
}
在此項目上運行 cargo clippy 會導致這個錯誤:
error: approximate value of `f{32, 64}::consts::PI` found. Consider using it directly
--> src/main.rs:2:13
|
2 | let x = 3.1415;
| ^^^^^^
|
= note: #[deny(clippy::approx_constant)] on by default
= help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/master/index.html#approx_constant
更多建議: