Skip to content

Commit ed64716

Browse files
authored
Added support for noclippy tag (#824)
* Added support for `noclippy` tag * Regenerate documentation * Added regression tests for `noclippy` tag. * Fixed typo * Fixed bad cherry-pick
1 parent 79177f1 commit ed64716

File tree

9 files changed

+82
-15
lines changed

9 files changed

+82
-15
lines changed

.bazelci/presubmit.yml

+16-11
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ tasks:
1717
- "//test/versioned_dylib:versioned_dylib_test"
1818
build_flags:
1919
- "--config=rustfmt"
20+
- "--config=clippy"
2021
ubuntu2004:
2122
name: "Minimum Supported Version"
2223
bazel: "3.5.0"
2324
build_targets: *default_linux_targets
2425
test_targets: *default_linux_targets
2526
build_flags:
2627
- "--config=rustfmt"
28+
- "--config=clippy"
2729
macos:
2830
osx_targets: &osx_targets
2931
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
@@ -35,6 +37,7 @@ tasks:
3537
test_targets: *osx_targets
3638
build_flags:
3739
- "--config=rustfmt"
40+
- "--config=clippy"
3841
rbe_ubuntu1604:
3942
test_targets:
4043
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
@@ -51,10 +54,12 @@ tasks:
5154
- "-@examples//ffi/rust_calling_c:matrix_dylib_test"
5255
build_flags:
5356
- "--config=rustfmt"
57+
- "--config=clippy"
5458
windows:
5559
build_flags:
5660
- "--enable_runfiles" # this is not enabled by default on windows and is necessary for the cargo build scripts
5761
- "--config=rustfmt"
62+
- "--config=clippy"
5863
windows_targets: &windows_targets
5964
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
6065
- "..."
@@ -81,6 +86,7 @@ tasks:
8186
- //...
8287
build_flags:
8388
- "--config=rustfmt"
89+
- "--config=clippy"
8490
docs_linux:
8591
name: Docs
8692
platform: ubuntu1804
@@ -89,20 +95,11 @@ tasks:
8995
- //...
9096
run_targets:
9197
- "//:test_docs"
92-
clippy_examples:
93-
name: Clippy on Examples
94-
platform: ubuntu1804
95-
working_directory: examples
96-
build_flags:
97-
- "--aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect"
98-
- "--output_groups=clippy_checks"
99-
build_targets:
100-
- //...
10198
clippy_failure:
10299
name: Negative Clippy Tests
103100
platform: ubuntu1804
104-
shell_commands:
105-
- ./test/clippy/clippy_failure_test.sh
101+
run_targets:
102+
- "//test/clippy:clippy_failure_test"
106103
rustfmt_failure:
107104
name: Negative Rustfmt Tests
108105
platform: ubuntu2004
@@ -112,6 +109,8 @@ tasks:
112109
name: Ubuntu 20.04 with Clang
113110
platform: ubuntu2004
114111
build_flags:
112+
- "--config=rustfmt"
113+
- "--config=clippy"
115114
- "--repo_env=CC=clang"
116115
# TODO(hlopko): Make this work (some tests were failing)
117116
# - "--linkopt=-fuse-ld=lld"
@@ -127,6 +126,9 @@ tasks:
127126
- "//..."
128127
test_targets:
129128
- "//..."
129+
build_flags:
130+
- "--config=rustfmt"
131+
- "--config=clippy"
130132
crate_universe_rbe_ubuntu1604:
131133
name: Crate Universe Examples
132134
platform: rbe_ubuntu1604
@@ -139,6 +141,7 @@ tasks:
139141
- "//..."
140142
build_flags:
141143
- "--config=rustfmt"
144+
- "--config=clippy"
142145
crate_universe_examples_macos:
143146
name: Crate Universe Examples
144147
platform: macos
@@ -151,6 +154,7 @@ tasks:
151154
- "//..."
152155
build_flags:
153156
- "--config=rustfmt"
157+
- "--config=clippy"
154158
crate_universe_examples_windows:
155159
name: Crate Universe Examples
156160
platform: windows
@@ -160,6 +164,7 @@ tasks:
160164
build_flags:
161165
- "--enable_runfiles" # this is not enabled by default on windows and is necessary for the cargo build scripts
162166
- "--config=rustfmt"
167+
- "--config=clippy"
163168
crate_universe_windows_targets: &crate_universe_windows_targets
164169
- "//..."
165170
# TODO: There are windows specific build issues in the generated

.bazelrc

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
# Enable rustfmt for all targets in the workspace
55
build:rustfmt --aspects=//rust:defs.bzl%rustfmt_aspect
66
build:rustfmt --output_groups=+rustfmt_checks
7+
8+
# Enable clippy for all targets in the workspace
9+
build:clippy --aspects=//rust:defs.bzl%rust_clippy_aspect
10+
build:clippy --output_groups=+clippy_checks

docs/rust_clippy.md

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ build --output_groups=+clippy_checks
2727

2828
This will enable clippy on all [Rust targets](./defs.md).
2929

30+
Note that targets tagged with `noclippy` will not perform clippy checks
31+
3032
<a id="#rust_clippy"></a>
3133

3234
## rust_clippy

docs/rust_clippy.vm

+2
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ build --output_groups=+clippy_checks
2020
```
2121

2222
This will enable clippy on all [Rust targets](./defs.md).
23+
24+
Note that targets tagged with `noclippy` will not perform clippy checks

examples/.bazelrc

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
# Enable rustfmt for all targets in the workspace
55
build:rustfmt --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
66
build:rustfmt --output_groups=+rustfmt_checks
7+
8+
# Enable clippy for all targets in the workspace
9+
build:clippy --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
10+
build:clippy --output_groups=+clippy_checks

examples/crate_universe/.bazelrc

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
# Enable rustfmt for all targets in the workspace
55
build:rustfmt --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
66
build:rustfmt --output_groups=+rustfmt_checks
7+
8+
# Enable clippy for all targets in the workspace
9+
build:clippy --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
10+
build:clippy --output_groups=+clippy_checks

rust/private/clippy.bzl

+9-3
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ load(
2323
)
2424
load("//rust/private:utils.bzl", "determine_output_hash", "find_cc_toolchain", "find_toolchain")
2525

26-
def _get_clippy_ready_crate_info(target):
26+
def _get_clippy_ready_crate_info(target, aspect_ctx):
2727
"""Check that a target is suitable for clippy and extract the `CrateInfo` provider from it.
2828
2929
Args:
3030
target (Target): The target the aspect is running on.
31+
aspect_ctx (ctx, optional): The aspect's context object.
3132
3233
Returns:
3334
CrateInfo, optional: A `CrateInfo` provider if clippy should be run or `None`.
@@ -37,14 +38,18 @@ def _get_clippy_ready_crate_info(target):
3738
if target.label.workspace_root.startswith("external"):
3839
return None
3940

41+
# Targets annotated with `noclippy` will not be formatted
42+
if aspect_ctx and "noclippy" in aspect_ctx.rule.attr.tags:
43+
return None
44+
4045
# Obviously ignore any targets that don't contain `CrateInfo`
4146
if rust_common.crate_info not in target:
4247
return None
4348

4449
return target[rust_common.crate_info]
4550

4651
def _clippy_aspect_impl(target, ctx):
47-
crate_info = _get_clippy_ready_crate_info(target)
52+
crate_info = _get_clippy_ready_crate_info(target, ctx)
4853
if not crate_info:
4954
return []
5055

@@ -189,7 +194,8 @@ $ bazel build --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect \
189194
)
190195

191196
def _rust_clippy_rule_impl(ctx):
192-
files = depset([], transitive = [dep[OutputGroupInfo].clippy_checks for dep in ctx.attr.deps])
197+
clippy_ready_targets = [dep for dep in ctx.attr.deps if "clippy_checks" in dir(dep[OutputGroupInfo])]
198+
files = depset([], transitive = [dep[OutputGroupInfo].clippy_checks for dep in clippy_ready_targets])
193199
return [DefaultInfo(files = files)]
194200

195201
rust_clippy = rule(

test/clippy/BUILD.bazel

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load(
2-
"//rust:rust.bzl",
2+
"@rules_rust//rust:rust.bzl",
33
"rust_binary",
44
"rust_clippy",
55
"rust_library",
@@ -50,18 +50,21 @@ rust_binary(
5050
name = "bad_binary",
5151
srcs = ["bad_src/main.rs"],
5252
edition = "2018",
53+
tags = ["noclippy"],
5354
)
5455

5556
rust_library(
5657
name = "bad_library",
5758
srcs = ["bad_src/lib.rs"],
5859
edition = "2018",
60+
tags = ["noclippy"],
5961
)
6062

6163
rust_test(
6264
name = "bad_test",
6365
srcs = ["bad_src/lib.rs"],
6466
edition = "2018",
67+
tags = ["noclippy"],
6568
)
6669

6770
# Clippy analysis of failing targets.
@@ -84,3 +87,8 @@ rust_clippy(
8487
tags = ["manual"],
8588
deps = [":bad_test"],
8689
)
90+
91+
sh_binary(
92+
name = "clippy_failure_test",
93+
srcs = ["clippy_failure_test.sh"],
94+
)

test/clippy/clippy_failure_test.sh

+32
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
set -euo pipefail
99

10+
if [[ -z "${BUILD_WORKSPACE_DIRECTORY:-}" ]]; then
11+
echo "This script should be run under Bazel"
12+
exit 1
13+
fi
14+
15+
cd "${BUILD_WORKSPACE_DIRECTORY}"
16+
1017
# Executes a bazel build command and handles the return value, exiting
1118
# upon seeing an error.
1219
#
@@ -31,6 +38,31 @@ function test_all() {
3138
local -r BUILD_FAILED=1
3239
local -r TEST_FAIL=3
3340

41+
temp_dir="$(mktemp -d -t ci-XXXXXXXXXX)"
42+
new_workspace="${temp_dir}/rules_rust_test_clippy"
43+
44+
mkdir -p "${new_workspace}/test/clippy" && \
45+
cp -r test/clippy/* "${new_workspace}/test/clippy/" && \
46+
cat << EOF > "${new_workspace}/WORKSPACE.bazel"
47+
workspace(name = "rules_rust_test_clippy")
48+
local_repository(
49+
name = "rules_rust",
50+
path = "${BUILD_WORKSPACE_DIRECTORY}",
51+
)
52+
load("@rules_rust//rust:repositories.bzl", "rust_repositories")
53+
rust_repositories()
54+
EOF
55+
56+
# Drop the 'noclippy' tags
57+
if [ "$(uname)" == "Darwin" ]; then
58+
SEDOPTS=(-i '' -e)
59+
else
60+
SEDOPTS=(-i)
61+
fi
62+
sed ${SEDOPTS[@]} 's/"noclippy"//' "${new_workspace}/test/clippy/BUILD.bazel"
63+
64+
pushd "${new_workspace}"
65+
3466
check_build_result $BUILD_OK ok_binary_clippy
3567
check_build_result $BUILD_OK ok_library_clippy
3668
check_build_result $BUILD_OK ok_test_clippy

0 commit comments

Comments
 (0)