• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
85**ATTRIBUTES**
86
87
88| Name  | Description | Type | Mandatory | Default |
89| :------------- | :------------- | :------------- | :------------- | :------------- |
90| <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 |  |
91| <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 | <code>[]</code> |
92
93
94<a id="rust_clippy_aspect"></a>
95
96## rust_clippy_aspect
97
98<pre>
99rust_clippy_aspect(<a href="#rust_clippy_aspect-name">name</a>)
100</pre>
101
102Executes the clippy checker on specified targets.
103
104This aspect applies to existing rust_library, rust_test, and rust_binary rules.
105
106As an example, if the following is defined in `examples/hello_lib/BUILD.bazel`:
107
108```python
109load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
110
111rust_library(
112    name = "hello_lib",
113    srcs = ["src/lib.rs"],
114)
115
116rust_test(
117    name = "greeting_test",
118    srcs = ["tests/greeting.rs"],
119    deps = [":hello_lib"],
120)
121```
122
123Then the targets can be analyzed with clippy using the following command:
124
125```output
126$ bazel build --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect               --output_groups=clippy_checks //hello_lib:all
127```
128
129
130**ASPECT ATTRIBUTES**
131
132
133
134**ATTRIBUTES**
135
136
137| Name  | Description | Type | Mandatory | Default |
138| :------------- | :------------- | :------------- | :------------- | :------------- |
139| <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 |   |
140
141
142