Skip to content

Commit

Permalink
fail build on any warnings when running clippy (#456)
Browse files Browse the repository at this point in the history
* fail build on any warnings when running clippy

Currently warnings from rustc such as an unused import are printed
during first execution, but they do not fail the build. If cargo users
run clippy again, the same warnings get printed, but with Bazel nothing
is shown, as the previous success is cached.

This patch changes all warnings into errors, with the rationale being
that if we're failing for any clippy warning, it probably makes sense
to fail for rustc warnings as well.

* clippy aspect needs to include --test on command line for tests

Without it, items imported in integration tests generate unused import
warnings, which fail the build.

* tag bindgen libaries, and don't fail when they generate warnings
  • Loading branch information
dae committed Dec 8, 2020
1 parent bc05787 commit 75d72ae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions bindgen/bindgen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def rust_bindgen_library(
rust_library(
name = name,
srcs = [name + "__bindgen.rs"],
tags = ["__bindgen"],
deps = [cc_lib],
**kwargs
)
Expand Down
4 changes: 4 additions & 0 deletions examples/hello_lib/tests/greeting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

extern crate hello_lib;

// test gate is not usually required in an integration test, but the BUILD
// script is using this file to test cdylib compilation as well, and when
// compiled in that mode, greeter is an unused import.
#[cfg(test)]
use hello_lib::greeter;

#[test]
Expand Down
25 changes: 16 additions & 9 deletions rust/private/clippy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,22 @@ def _clippy_aspect_impl(target, ctx):
emit = ["dep-info", "metadata"],
)

# Deny the default-on clippy warning levels.
#
# If these are left as warnings, then Bazel will consider the execution
# result of the aspect to be "success", and Clippy won't be re-triggered
# unless the source file is modified.
args.add("-Dclippy::style")
args.add("-Dclippy::correctness")
args.add("-Dclippy::complexity")
args.add("-Dclippy::perf")
# Turn any warnings from clippy or rustc into an error, as otherwise
# Bazel will consider the execution result of the aspect to be "success",
# and Clippy won't be re-triggered unless the source file is modified.
if "__bindgen" in ctx.rule.attr.tags:
# bindgen-generated content is likely to trigger warnings, so
# only fail on clippy warnings
args.add("-Dclippy::style")
args.add("-Dclippy::correctness")
args.add("-Dclippy::complexity")
args.add("-Dclippy::perf")
else:
# fail on any warning
args.add("-Dwarnings")

if crate_info.is_test:
args.add("--test")

ctx.actions.run(
executable = ctx.executable._process_wrapper,
Expand Down

0 comments on commit 75d72ae

Please sign in to comment.