19

Often during development, I have a bunch of unused imports and variables. I like to fix those after I have correctly working code. The warnings these generate cause me to scroll though the cargo build output to find errors among all the warnings.

Is that possible to only show the warnings if compilation succeeds?

I don't want to ignore the warnings entirely, since I do want to solve them before committing the code.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Mark
  • 18,730
  • 7
  • 107
  • 130

2 Answers2

12

You can suppress warnings in your compilation using the -Awarnings flags. If you use Cargo, you can add it with:

cargo rustc -- -Awarnings

That will compile your crate with warnings disabled, so only errors will show up. When you get a successful compilation, you can switch back to:

cargo build

And your crate will compile again (because the flags have changed, the target is no longer up to date) and you will get the detailed warnings.

You can try automating them by running:

cargo rustc -- -Awarnings && cargo build

This has the drawback of compiling the crate twice if there are no errors and that can take some extra time.

If you want to compile all the dependencies without the warnings, you can run instead:

RUSTFLAGS=-Awarnings cargo build

But then, the double compilation issue is quite more relevant.


As as side note, I think that some IDEs (VSCode?) are able to do that: sort the compiler messages and filter out the ones you are not interested in.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • You may use `cargo check -- -Awarnings`? I haven't tried, but it can save you some time. – hellow Nov 18 '18 at 13:55
  • 1
    @hellow: I tried, but unfortunately `cargo check` is not able to use `rustc` flags... at least I don't know how. You can use it with the `RUSTFLAGS` trick, though. – rodrigo Nov 18 '18 at 13:57
  • To those working on a workspace, I used `cargo rustc --package [package name] -- -Awarnings`. `--package` can be shortened with `-p` – MikeTheSapien Nov 19 '22 at 01:47
3

You're looking for:

cargo install cargo-limit; cargo lcheck

See https://github.com/alopatindev/cargo-limit/blob/master/README.md for more information.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Hezuikn
  • 69
  • 1
  • 3
  • Please be more descriptive of the question. – raj kavadia Nov 28 '21 at 14:33
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 28 '21 at 14:34
  • didnt ask + radio – Hezuikn Jun 02 '22 at 14:53