lunes, 22 de enero de 2024

Mensajes de error, lints con Clippy de rust.


El compilador de Rust produce fantásticos mensajes de error, así como útiles lints integradas. Clippy proporciona aún más ayuda, organizadas en grupos que se pueden habilitar por proyecto.


#[deny(clippy::cast_possible_truncation)]

fn main() {

    let x = 3;

    while (x < 70000) {

        x *= 2;

    }

    println!("X probably fits in a u16, right? {}", x as u16);

}

Si lo ejecutamos : 


   Compiling hello_cargo v0.1.0 

warning: unnecessary parentheses around `while` condition

 --> src/main.rs:7:11

  |

7 |     while (x < 70000) {

  |           ^         ^

  |

  = note: `#[warn(unused_parens)]` on by default

help: remove these parentheses

  |

7 -     while (x < 70000) {

7 +     while x < 70000 {

  |


error[E0384]: cannot assign twice to immutable variable `x`

 --> src/main.rs:9:9

  |

5 |     let x = 3;

  |         -

  |         |

  |         first assignment to `x`

  |         help: consider making this binding mutable: `mut x`

...

9 |         x *= 2;

  |         ^^^^^^ cannot assign twice to immutable variable


For more information about this error, try `rustc --explain E0384`.

warning: `hello_cargo` (bin "hello_cargo") generated 1 warning

error: could not compile `hello_cargo` (bin "hello_cargo") due to previous error; 1 warning emitted


 *  Error 


Clippy tiene una extensa documentación de sus lints: https://doc.rust-lang.org/nightly/clippy/

1 comentario: