xref: /aosp_15_r20/external/bazelbuild-rules_rust/docs/rust_clippy.md (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2# Rust Clippy
3
4* [rust_clippy](#rust_clippy)
5* [rust_clippy_aspect](#rust_clippy_aspect)
6
7
8## Overview
9
10
11[Clippy][clippy] is a tool for catching common mistakes in Rust code and improving it. An
12expansive list of lints and the justification can be found in their [documentation][docs].
13
14[clippy]: https://github.com/rust-lang/rust-clippy#readme
15[docs]: https://rust-lang.github.io/rust-clippy/
16
17
18### Setup
19
20
21Simply add the following to the `.bazelrc` file in the root of your workspace:
22
23```text
24build --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
25build --output_groups=+clippy_checks
26```
27
28This will enable clippy on all [Rust targets](./defs.md).
29
30Note that targets tagged with `no-clippy` will not perform clippy checks
31
32To use a local clippy.toml, add the following flag to your `.bazelrc`. Note that due to
33the 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
37build --@rules_rust//:clippy.toml=//:clippy.toml
38```
39
40<a id="rust_clippy"></a>
41
42## rust_clippy
43
44<pre>
45rust_clippy(<a href="#rust_clippy-name">name</a>, <a href="#rust_clippy-deps">deps</a>)
46</pre>
47
48Executes the clippy checker on a specific target.
49
50Similar to `rust_clippy_aspect`, but allows specifying a list of dependencies within the build system.
51
52For example, given the following example targets:
53
54```python
55load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
56
57rust_library(
58    name = "hello_lib",
59    srcs = ["src/lib.rs"],
60)
61
62rust_test(
63    name = "greeting_test",
64    srcs = ["tests/greeting.rs"],
65    deps = [":hello_lib"],
66)
67```
68
69Rust clippy can be set as a build target with the following:
70
71```python
72load("@rules_rust//rust:defs.bzl", "rust_clippy")
73
74rust_clippy(
75    name = "hello_library_clippy",
76    testonly = True,
77    deps = [
78        ":hello_lib",
79        ":greeting_test",
80    ],
81)
82```
83
84**ATTRIBUTES**
85
86
87| Name  | Description | Type | Mandatory | Default |
88| :------------- | :------------- | :------------- | :------------- | :------------- |
89| <a id="rust_clippy-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
90| <a id="rust_clippy-deps"></a>deps |  Rust targets to run clippy on.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
91
92
93<a id="rust_clippy_aspect"></a>
94
95## rust_clippy_aspect
96
97<pre>
98rust_clippy_aspect(<a href="#rust_clippy_aspect-name">name</a>)
99</pre>
100
101Executes the clippy checker on specified targets.
102
103This aspect applies to existing rust_library, rust_test, and rust_binary rules.
104
105As an example, if the following is defined in `examples/hello_lib/BUILD.bazel`:
106
107```python
108load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
109
110rust_library(
111    name = "hello_lib",
112    srcs = ["src/lib.rs"],
113)
114
115rust_test(
116    name = "greeting_test",
117    srcs = ["tests/greeting.rs"],
118    deps = [":hello_lib"],
119)
120```
121
122Then the targets can be analyzed with clippy using the following command:
123
124```output
125$ bazel build --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect               --output_groups=clippy_checks //hello_lib:all
126```
127
128**ASPECT ATTRIBUTES**
129
130
131
132**ATTRIBUTES**
133
134
135| Name  | Description | Type | Mandatory | Default |
136| :------------- | :------------- | :------------- | :------------- | :------------- |
137| <a id="rust_clippy_aspect-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
138
139
140