Skip to content

Commit 404b84b

Browse files
djmarcinUebelAndre
andauthored
Add support for clippy.toml (#835)
* Add support for clippy.toml * Regenerate documentation * Fix build file naming * Fix use of depset * Fix path for non-root config file locations * Document Rust 1.34.0 requirement * Regenerate documentation * Document location of clippy documentation * buildifier * Use modern python formatting Co-authored-by: UebelAndre <[email protected]> * Use modern python formatting Co-authored-by: UebelAndre <[email protected]> * Add a test * Address review comments Co-authored-by: UebelAndre <[email protected]>
1 parent 86a653f commit 404b84b

File tree

10 files changed

+55
-1
lines changed

10 files changed

+55
-1
lines changed

BUILD.bazel

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ error_format(
1818
visibility = ["//visibility:public"],
1919
)
2020

21+
# This setting is used by the clippy rules. See https://bazelbuild.github.io/rules_rust/rust_clippy.html
22+
label_flag(
23+
name = "clippy.toml",
24+
build_setting_default = "//tools/clippy:clippy.toml",
25+
visibility = ["//visibility:public"],
26+
)
27+
2128
# This setting is used by the rustfmt rules. See https://bazelbuild.github.io/rules_rust/rust_fmt.html
2229
label_flag(
2330
name = "rustfmt.toml",

docs/rust_clippy.md

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ This will enable clippy on all [Rust targets](./defs.md).
2929

3030
Note that targets tagged with `noclippy` will not perform clippy checks
3131

32+
To use a local clippy.toml, add the following flag to your `.bazelrc`. Note that due to
33+
the upstream implementation of clippy, this file must be named either `.clippy.toml` or
34+
`clippy.toml`. Using a custom config file requires Rust 1.34.0 or newer.
35+
36+
```text
37+
build --@rules_rust//:clippy.toml=//:clippy.toml
38+
```
39+
3240
<a id="#rust_clippy"></a>
3341

3442
## rust_clippy

docs/rust_clippy.vm

+8
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ build --output_groups=+clippy_checks
2222
This will enable clippy on all [Rust targets](./defs.md).
2323

2424
Note that targets tagged with `noclippy` will not perform clippy checks
25+
26+
To use a local clippy.toml, add the following flag to your `.bazelrc`. Note that due to
27+
the upstream implementation of clippy, this file must be named either `.clippy.toml` or
28+
`clippy.toml`. Using a custom config file requires Rust 1.34.0 or newer.
29+
30+
```text
31+
build --@rules_rust//:clippy.toml=//:clippy.toml
32+
```

rust/private/clippy.bzl

+13
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ def _clippy_aspect_impl(target, ctx):
116116
if crate_info.is_test:
117117
args.add("--test")
118118

119+
# Upstream clippy requires one of these two filenames or it silently uses
120+
# the default config. Enforce the naming so users are not confused.
121+
valid_config_file_names = [".clippy.toml", "clippy.toml"]
122+
if ctx.file._config.basename not in valid_config_file_names:
123+
fail("The clippy config file must be named one of: {}".format(valid_config_file_names))
124+
env["CLIPPY_CONF_DIR"] = "${{pwd}}/{}".format(ctx.file._config.dirname)
125+
compile_inputs = depset([ctx.file._config], transitive = [compile_inputs])
126+
119127
ctx.actions.run(
120128
executable = ctx.executable._process_wrapper,
121129
inputs = compile_inputs,
@@ -145,6 +153,11 @@ rust_clippy_aspect = aspect(
145153
),
146154
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
147155
),
156+
"_config": attr.label(
157+
doc = "The `clippy.toml` file used for configuration",
158+
allow_single_file = True,
159+
default = Label("//:clippy.toml"),
160+
),
148161
"_error_format": attr.label(
149162
doc = "The desired `--error-format` flags for clippy",
150163
default = "//:error_format",

test/clippy/clippy_failure_test.sh

+12-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ cd "${BUILD_WORKSPACE_DIRECTORY}"
2020
# Takes two arguments:
2121
# ${1}: The expected return code.
2222
# ${2}: The target within "//test/clippy" to be tested.
23+
#
24+
# Optional third argument:
25+
# ${3}: The clippy.toml file to pass as an override.
2326
function check_build_result() {
2427
local ret=0
2528
echo -n "Testing ${2}... "
26-
(bazel build //test/clippy:"${2}" &> /dev/null) || ret="$?" && true
29+
ADDITIONAL_ARGS=()
30+
if [[ -n "${3-}" ]]; then
31+
ADDITIONAL_ARGS+=("--@rules_rust//:clippy.toml=${3}")
32+
fi
33+
(bazel build "${ADDITIONAL_ARGS[@]}" //test/clippy:"${2}" &> /dev/null) || ret="$?" && true
2734
if [[ "${ret}" -ne "${1}" ]]; then
2835
echo "FAIL: Unexpected return code [saw: ${ret}, want: ${1}] building target //test/clippy:${2}"
2936
echo " Run \"bazel build //test/clippy:${2}\" to see the output"
@@ -69,6 +76,10 @@ EOF
6976
check_build_result $BUILD_FAILED bad_binary_clippy
7077
check_build_result $BUILD_FAILED bad_library_clippy
7178
check_build_result $BUILD_FAILED bad_test_clippy
79+
80+
# Test that we can make the ok_library_clippy fail when using an extra config file.
81+
# Proves that the config file is used and overrides default settings.
82+
check_build_result $BUILD_FAILED ok_library_clippy //too_many_args:clippy.toml
7283
}
7384

7485
test_all

test/clippy/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ pub fn greeting() -> String {
22
"Hello World".to_owned()
33
}
44

5+
// too_many_args/clippy.toml will require no more than 2 args.
6+
pub fn with_args(_: u32, _: u32, _: u32) {}
7+
58
#[cfg(test)]
69
mod tests {
710
use super::*;

test/clippy/too_many_args/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports_files(["clippy.toml"])

test/clippy/too_many_args/clippy.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
too-many-arguments-threshold = 2

tools/clippy/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports_files(["clippy.toml"])

tools/clippy/clippy.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# clippy options: https://rust-lang.github.io/rust-clippy/

0 commit comments

Comments
 (0)