How to fail a build with warnings?

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 Likes

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

#![deny(warnings)]

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

7 Likes

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 Likes

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.

7 Likes

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.

1 Like

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

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.

3 Likes

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
4 Likes

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

1 Like
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.

While this does not directly answer the question, I figured that #![cfg_attr(not(debug_assertions), deny(warnings))] is a nice way to reject builds with warnings only on my CI. @ogeon's suggestion of feature = "strict" is good, but it does not work well with multi-package workspaces.

Yes, debug_assertions was not designed for this, but it is coincidentally true that we usually do not want to fail warned builds during development but want to prevent them from passing CI or deployments, which usually have the --release flag.