Is it possible to have Cargo always show warnings?

筅森魡賤 提交于 2019-11-28 09:10:58
mdup

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.

This is another variant of the touch pattern described in mdups answer.

Use this command to check for type errors and display all warnings every run, even when no code change has occurred:

touch src/my_tra_la_la.rs && clear && clear && cargo check

The clear && clear && part is mostly irrelevant to this answer but it's a mandatory part of every build command for me, to avoid having the new output visually merge with output from the previous run.

It requires two things, first mod my_tra_la_la; in main.rs, or in lib.rs if it's a library.

The second part is optional but I think it's nice to add some documentation to this special source file, src/my_tra_la_la.rs:

//! The only purpose of this file is to act as a target for the `touch` command
//! in order to force recompilation of this otherwise meaningless file and in
//! turn to force rustc to display warnings, every time.
//!
//! touch src/my_tra_la_la.rs && clear && clear && cargo check
//!
//! Mmm...my ding ding dong

The whole point of a dedicated touch file like this is to avoid the need to ever have it open in your code editor and avoid popups and warnings like "file changed!" while working.

Shepmaster

A solution, albeit temporarily, was to touch a file I rarely edit. This way, I'm avoiding the this file has been changed problem, and also don't need to clean the whole project all the time.

In addition, I managed to get colors in as well (cargo doesn't have a --color option), by not using watch at all, but by simply running the following script:

#!/bin/sh
while :
do
    script -qc "cargo build" /dev/null > .tmp
    clear
    cat .tmp
    sleep 2 # or whatever
    touch src/somefile.rs
done

The reason I'm writing and reading to .tmp is to make all the output appear at one (rustc outputs as it runs)

I've run into this before. I use:

cargo check
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!