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:
#![deny(warnings)]
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.
Yeah, that's how I do it, but I'm also testing like that locally to avoid pushing bad commits.
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.
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
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.
export RUSTFLAGS='-D warnings'
Set this before cargo build
, then all targets will fail with warnings.