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?


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


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


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: RFC: Prevent lint changes being a breaking change by alexcrichton 路 Pull Request #1193 路 rust-lang/rfcs 路 GitHub


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.


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:

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

Now add to the root of your crate ( or

#![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.


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

See also

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

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.

export RUSTFLAGS='-D warnings'

Set this before cargo build, then all targets will fail with warnings.

This topic is over 7 years old, no need to keep it going any further :slight_smile: