How to fail a build with warnings?


#1

I am trying to setup for my project so I can cause a build to fail when there are warnings. I couldn’t figure out how to do this with cargo. Anyone doing this?


#2

Just make the first line of your .rs file be:

#![deny(warnings)]

Took me a while to find this, a few weeks back!


#3

I’d recommend against. I used to do it and found out the hard way that it breaks forward compatibility. Future versions of Rust might choose to warn on things that weren’t a warning before (first and foremost deprecations).

This might lead to the case where code that is compatible with Rust 1.1 suddenly not compatible with 1.2 anymore just because of an introduced warning.

cargo won’t have this problem, as it caps lint level for release code: https://github.com/rust-lang/rfcs/pull/1193


#4

Yes, be careful. I have added these lines to Rustful:

#![cfg_attr(feature = "strict", deny(missing_docs))]
#![cfg_attr(feature = "strict", deny(warnings))]

and a cargo feature, called strict, that activates them. This feature is only used while testing, to keep the requirements strict, and not otherwise. It may still become annoying when something becomes deprecated, but it hasn’t been a problem so far. I’ll see what I do when it becomes a problem, but it will still only affect tests and be opt-in.


#5

So is there a way to this with a cargo flag?

Thanks! I think I’ll start with that. Maybe run it with that only in travis for now.


#6

Yeah, that’s how I do it, but I’m also testing like that locally to avoid pushing bad commits. :wink:


#7

This is an old thread, but it’s still useful today. Here’s a step-by-step guide:

If you want to fail the build when there are warnings, but only in CI, add this to your Cargo.toml:

[features]
# Treat warnings as a build error.
strict = []

Now add to the root of your crate (lib.rs or main.rs):

#![cfg_attr(feature = "strict", deny(warnings))]

You can then compile on Travis with the following command:

$ cargo build --features "strict"

and it will fail if there are any warnings.


#8

An alternative that lets you avoid doing any code changes is to build with

cargo rustc -- -D warnings

Or if you use clippy

cargo clippy -- -D warnings

#9

See also https://github.com/rust-unofficial/patterns/blob/master/anti_patterns/deny-warnings.md


#10
What's the difference between cargo rust "Compile a PACKAGE and all of its dependencies" and cargo build "Compile a A LOCAL PACKAGE and all of its dependencies"

I ask so as to substitute cargo rustc for cargo build and use -D warnings
Thanks.