]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/book/src/development/basics.md
New upstream version 1.64.0+dfsg1
[rustc.git] / src / tools / clippy / book / src / development / basics.md
CommitLineData
f20569fa
XL
1# Basics for hacking on Clippy
2
3This document explains the basics for hacking on Clippy. Besides others, this
923072b8
FG
4includes how to build and test Clippy. For a more in depth description on the
5codebase take a look at [Adding Lints] or [Common Tools].
f20569fa 6
064997fb
FG
7[Adding Lints]: https://github.com/rust-lang/rust-clippy/blob/master/book/src/development/adding_lints.md
8[Common Tools]: https://github.com/rust-lang/rust-clippy/blob/master/book/src/development/common_tools_writing_lints.md
f20569fa
XL
9
10- [Basics for hacking on Clippy](#basics-for-hacking-on-clippy)
11 - [Get the Code](#get-the-code)
12 - [Building and Testing](#building-and-testing)
13 - [`cargo dev`](#cargo-dev)
14 - [lintcheck](#lintcheck)
15 - [PR](#pr)
16 - [Common Abbreviations](#common-abbreviations)
136023e0 17 - [Install from source](#install-from-source)
f20569fa
XL
18
19## Get the Code
20
21First, make sure you have checked out the latest version of Clippy. If this is
22your first time working on Clippy, create a fork of the repository and clone it
23afterwards with the following command:
24
25```bash
26git clone git@github.com:<your-username>/rust-clippy
27```
28
29If you've already cloned Clippy in the past, update it to the latest version:
30
31```bash
17df50a5
XL
32# If the upstream remote has not been added yet
33git remote add upstream https://github.com/rust-lang/rust-clippy
f20569fa
XL
34# upstream has to be the remote of the rust-lang/rust-clippy repo
35git fetch upstream
36# make sure that you are on the master branch
37git checkout master
38# rebase your master branch on the upstream master
39git rebase upstream/master
40# push to the master branch of your fork
41git push
42```
43
44## Building and Testing
45
46You can build and test Clippy like every other Rust project:
47
48```bash
49cargo build # builds Clippy
50cargo test # tests Clippy
51```
52
53Since Clippy's test suite is pretty big, there are some commands that only run a
54subset of Clippy's tests:
55
56```bash
57# only run UI tests
58cargo uitest
59# only run UI tests starting with `test_`
60TESTNAME="test_" cargo uitest
61# only run dogfood tests
064997fb 62cargo dev dogfood
f20569fa
XL
63```
64
923072b8
FG
65If the output of a [UI test] differs from the expected output, you can update
66the reference file with:
f20569fa
XL
67
68```bash
69cargo dev bless
70```
71
72For example, this is necessary, if you fix a typo in an error message of a lint
73or if you modify a test file to add a test case.
74
923072b8
FG
75> _Note:_ This command may update more files than you intended. In that case
76> only commit the files you wanted to update.
f20569fa
XL
77
78[UI test]: https://rustc-dev-guide.rust-lang.org/tests/adding.html#guide-to-the-ui-tests
79
80## `cargo dev`
81
82Clippy has some dev tools to make working on Clippy more convenient. These tools
83can be accessed through the `cargo dev` command. Available tools are listed
84below. To get more information about these commands, just call them with
85`--help`.
86
87```bash
88# formats the whole Clippy codebase and all tests
89cargo dev fmt
90# register or update lint names/groups/...
91cargo dev update_lints
92# create a new lint and register it
93cargo dev new_lint
064997fb
FG
94# deprecate a lint and attempt to remove code relating to it
95cargo dev deprecate
136023e0
XL
96# automatically formatting all code before each commit
97cargo dev setup git-hook
cdc7bbd5 98# (experimental) Setup Clippy to work with IntelliJ-Rust
136023e0 99cargo dev setup intellij
064997fb
FG
100# runs the `dogfood` tests
101cargo dev dogfood
f20569fa 102```
923072b8
FG
103
104More about intellij command usage and reasons
064997fb 105[here](https://github.com/rust-lang/rust-clippy/blob/master/CONTRIBUTING.md#intellij-rust)
f20569fa
XL
106
107## lintcheck
923072b8
FG
108
109`cargo lintcheck` will build and run clippy on a fixed set of crates and
110generate a log of the results. You can `git diff` the updated log against its
111previous version and see what impact your lint made on a small set of crates.
112If you add a new lint, please audit the resulting warnings and make sure there
113are no false positives and that the suggestions are valid.
f20569fa
XL
114
115Refer to the tools [README] for more details.
116
117[README]: https://github.com/rust-lang/rust-clippy/blob/master/lintcheck/README.md
923072b8 118
f20569fa
XL
119## PR
120
923072b8
FG
121We follow a rustc no merge-commit policy. See
122<https://rustc-dev-guide.rust-lang.org/contributing.html#opening-a-pr>.
f20569fa
XL
123
124## Common Abbreviations
125
126| Abbreviation | Meaning |
127| ------------ | -------------------------------------- |
128| UB | Undefined Behavior |
129| FP | False Positive |
130| FN | False Negative |
131| ICE | Internal Compiler Error |
132| AST | Abstract Syntax Tree |
133| MIR | Mid-Level Intermediate Representation |
134| HIR | High-Level Intermediate Representation |
135| TCX | Type context |
136
923072b8
FG
137This is a concise list of abbreviations that can come up during Clippy
138development. An extensive general list can be found in the [rustc-dev-guide
139glossary][glossary]. Always feel free to ask if an abbreviation or meaning is
140unclear to you.
f20569fa 141
136023e0
XL
142## Install from source
143
923072b8
FG
144If you are hacking on Clippy and want to install it from source, do the
145following:
136023e0 146
923072b8
FG
147First, take note of the toolchain
148[override](https://rust-lang.github.io/rustup/overrides.html) in
149`/rust-toolchain`. We will use this override to install Clippy into the right
150toolchain.
136023e0 151
923072b8
FG
152> Tip: You can view the active toolchain for the current directory with `rustup
153> show active-toolchain`.
136023e0 154
923072b8
FG
155From the Clippy project root, run the following command to build the Clippy
156binaries and copy them into the toolchain directory. This will override the
157currently installed Clippy component.
136023e0
XL
158
159```terminal
160cargo build --release --bin cargo-clippy --bin clippy-driver -Zunstable-options --out-dir "$(rustc --print=sysroot)/bin"
161```
162
923072b8
FG
163Now you may run `cargo clippy` in any project, using the toolchain where you
164just installed Clippy.
136023e0
XL
165
166```terminal
167cd my-project
168cargo +nightly-2021-07-01 clippy
169```
170
171...or `clippy-driver`
172
173```terminal
174clippy-driver +nightly-2021-07-01 <filename>
175```
176
923072b8
FG
177If you need to restore the default Clippy installation, run the following (from
178the Clippy project root).
136023e0
XL
179
180```terminal
181rustup component remove clippy
182rustup component add clippy
183```
184
923072b8
FG
185> **DO NOT** install using `cargo install --path . --force` since this will
186> overwrite rustup
187> [proxies](https://rust-lang.github.io/rustup/concepts/proxies.html). That is,
188> `~/.cargo/bin/cargo-clippy` and `~/.cargo/bin/clippy-driver` should be hard or
189> soft links to `~/.cargo/bin/rustup`. You can repair these by running `rustup
190> update`.
136023e0 191
f20569fa 192[glossary]: https://rustc-dev-guide.rust-lang.org/appendix/glossary.html