Rust Recommended Warnings


#1

I am new to Rust and wanted to know if there is a recommended compiling setting (like gcc -Wall) to capture warnings. The --help shows -W, how is used. Is there a better approach?

D:\rust\bin>rustc -W block.rs
error: no input filename given

I did find the warnings quite clear when there is a potential issue. In this case the last ‘x’ variable is bound, I think Ruby is the same. A Rubyist can confirm.

D:\rust\bin>rustc block.rs
block.rs:2:9: 2:10 warning: unused variable: x, #[warn(unused_variables)] on b
y default
block.rs:2 let x: u32 = 17;
^

D:\rust\bin>block
The value of x is 12 and value of y is 3

fn main() {
    let x: u32 = 17;
 
    let x = 5;
    {
        let y: i32 = 3;
        println!("The value of x is {} and value of y is {}", x, y);
    }
}

D:\rust\bin>block
The value of x is 5 and value of y is 3


#2

Hi!

First of all, you could surround your code in tripple backquotes ```code``` to make it properly highlighted:

fn main() {
    println!("Hello, World");
}

And you can edit your existing post to fix this :slight_smile:

Regarding warnings, Rust has a good set of warnings enabled by default, and I think that the recommended setting is just to rely on those defaults.

If you want even more scrutiny from the compiler, you can add #![deny(warnings)] to the top of your file. This will turn all warnings into hard errors.

And there is an interesting project called rust-clippy which provides additional warnings as a compiler plugin.


#3

I’d like to add that if you call rustc -W help it will show you a set of lints along with the default levels. You can set the levels using -W (for warn) or -D (for deny) followed by the lint name. The latter also applies to clippy (apart from help, but we have a pretty comprehensive wiki).


#4

Thanks, I checked the help windows which were useful.

Using the -o option creates the file - what specifically is it used for? Can’t be opened - is it binary?

Also simple as it sounds is there an example for -W usage?

D:\rust\bin>rustc -o debug.txt test.rs
test.rs:6:3: 6:24 warning: constant item is never used: `PI`, #[warn(dead_code)]
 on by default
test.rs:6   const PI: f32 = 3.14;
            ^~~~~~~~~~~~~~~~~~~~~

D:\rust\bin>rustc -W test.rs
error: no input filename given

Thanks for the responses.