Skip to content
On this page

Clippy3: exercises/clippy/clippy3.rs

题目

rust
// clippy3.rs
// 
// Here's a couple more easy Clippy fixes, so you can see its utility.
//
// Execute `rustlings hint clippy3` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

#[allow(unused_variables, unused_assignments)]
fn main() {
    let my_option: Option<()> = None;
    if my_option.is_none() {
        my_option.unwrap();
    }

    let my_arr = &[
        -1, -2, -3
        -4, -5, -6
    ];
    println!("My array! Here it is: {:?}", my_arr);

    let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5);
    println!("This Vec is empty, see? {:?}", my_empty_vec);

    let mut value_a = 45;
    let mut value_b = 66;
    // Let's swap these two!
    value_a = value_b;
    value_b = value_a;
    println!("value a: {}; value b: {}", value_a, value_b);
}

题目解析

txt
error: possibly missing a comma here
  --> clippy3.rs:17:19
   |
17 |         -1, -2, -3
   |                   ^
   |
   = note: to remove this lint, add a comma or write the expr in a single line
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_comma
   = note: `#[deny(clippy::possible_missing_comma)]` on by default

首先信息提示我们在第17行末尾缺少一个逗号:

rust
let my_arr = &[
        -1, -2, -3 
        -1, -2, -3, 
        -4, -5, -6
    ];
txt
error: this call to `unwrap()` will always panic
  --> clippy3.rs:13:9
   |
12 |     if my_option.is_none() {
   |        ------------------- because of this check
13 |         my_option.unwrap();
   |         ^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#panicking_unwrap
   = note: `#[deny(clippy::panicking_unwrap)]` on by default

然后提示我们,在第13行因为我们的my_optionsNone的时候进行unwrap()总是会panic。我们可以将其删除。

rust
if my_option.is_none() { 
        my_option.unwrap(); 
    } 
txt
error: this looks like you are trying to swap `value_a` and `value_b`
  --> clippy3.rs:25:5
   |
25 | /     value_a = value_b;
26 | |     value_b = value_a;
   | |_____________________^ help: try: `std::mem::swap(&mut value_a, &mut value_b)`
   |
   = note: or maybe you should use `std::mem::replace`?
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#almost_swapped
   = note: `#[deny(clippy::almost_swapped)]` on by default

接着提示我们在value_avalue_b进行交换的时候,而可以使用std::mem::swap(&mut value_a, &mut value_b)

rust
value_a = value_b; 
    value_b = value_a; 
    std::mem::swap(&mut value_a, &mut value_b); 
txt
error: this let-binding has unit value
  --> clippy3.rs:19:5
   |
19 |     let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `vec![1, 2, 3, 4, 5].resize(0, 5);`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
   = note: `-D clippy::let-unit-value` implied by `-D warnings`

最后一个,提示我们let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5);这个let绑定实际是一个()单元类型。我们可以省略这个let绑定。

rust
let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5); 
    println!("This Vec is empty, see? {:?}", my_empty_vec); 
    println!("This Vec is empty, see? {:?}", vec![1, 2, 3, 4, 5].resize(0, 5)); 

Powered by VitePress