Is it possible to have Cargo always show warnings?

Warnings only happen when Rust recompiles your files; however it caches as much as possible and if something hasn't changed it will happily skip an useless compile. There's currently no option in Cargo to force rebuild.

A quick and dirty solution, but easy to setup, is to touch your source files so that Cargo believes they changed:

$ cd /path/to/project/root
$ ls
Cargo.lock Cargo.toml src        target
$ cargo build
     Compiling abc v0.1.0 (file:///private/tmp/b/abc)
  src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variables)] on by default
  src/main.rs:2     let x: u8 = 123;
                        ^
$ cargo build
$ touch $(find src)
$ cargo build
     Compiling abc v0.1.0 (file:///private/tmp/b/abc)
  src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variables)] on by default
  src/main.rs:2     let x: u8 = 123;
                        ^

Another solution, maybe better, would be to clean off the target directory containing binary artifacts, with cargo clean:

$ cargo build
   Compiling abc v0.1.0 (file:///private/tmp/b/abc)
src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variables)] on by default
src/main.rs:2     let x: u8 = 123;
                      ^
$ cargo build
$ cargo clean
$ cargo build
   Compiling abc v0.1.0 (file:///private/tmp/b/abc)
src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variables)] on by default
src/main.rs:2     let x: u8 = 123;
                      ^

It has the advantage of not triggering Vim "file changed!" warnings and is also runnable anywhere within the project dir, not just the root.


As of Rust 1.40, Cargo will cache the compiler messages. This means that even if the code doesn't need to be compiled again, the previous warnings will be printed out.

Rust 1.40

% cargo build
   Compiling warnings v0.1.0 (/private/tmp/warnings)
warning: unused variable: `a`
 --> src/main.rs:2:9
  |
2 |     let a = 42;
  |         ^ help: consider prefixing with an underscore: `_a`
  |
  = note: `#[warn(unused_variables)]` on by default

    Finished dev [unoptimized + debuginfo] target(s) in 1.58s

% cargo build
warning: unused variable: `a`
 --> src/main.rs:2:9
  |
2 |     let a = 42;
  |         ^ help: consider prefixing with an underscore: `_a`
  |
  = note: `#[warn(unused_variables)]` on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.00s

Rust 1.39

% cargo build
   Compiling warnings v0.1.0 (/private/tmp/warnings)
warning: unused variable: `a`
 --> src/main.rs:2:9
  |
2 |     let a = 42;
  |         ^ help: consider prefixing with an underscore: `_a`
  |
  = note: `#[warn(unused_variables)]` on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.42s

% cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s