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