Skip to content

Commit 51f2a6f

Browse files
committed
Add documentation for basic Clippy hacking
1 parent 8c83d5f commit 51f2a6f

File tree

3 files changed

+119
-16
lines changed

3 files changed

+119
-16
lines changed

CONTRIBUTING.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ High level approach:
3232

3333
1. Find something to fix/improve
3434
2. Change code (likely some file in `clippy_lints/src/`)
35-
3. Follow the instructions in the [docs for writing lints](doc/adding_lints.md) such as running the `setup-toolchain.sh` script
35+
3. Follow the instructions in the [Basics docs](doc/basics.md) such as running the `setup-toolchain.sh` script
3636
4. Run `cargo test` in the root directory and wiggle code until it passes
3737
5. Open a PR (also can be done after 2. if you run into problems)
3838

@@ -95,16 +95,16 @@ quick read.
9595

9696
## Getting code-completion for rustc internals to work
9797

98-
Unfortunately, [`rust-analyzer`][ra_homepage] does not (yet?) understand how Clippy uses compiler-internals
99-
using `extern crate` and it also needs to be able to read the source files of the rustc-compiler which are not
100-
available via a `rustup` component at the time of writing.
101-
To work around this, you need to have a copy of the [rustc-repo][rustc_repo] available which can be obtained via
102-
`git clone https://github.com/rust-lang/rust/`.
103-
Then you can run a `cargo dev` command to automatically make Clippy use the rustc-repo via path-dependencies
104-
which rust-analyzer will be able to understand.
105-
Run `cargo dev ra-setup --repo-path <repo-path>` where `<repo-path>` is an absolute path to the rustc repo
106-
you just cloned.
107-
The command will add path-dependencies pointing towards rustc-crates inside the rustc repo to
98+
Unfortunately, [`rust-analyzer`][ra_homepage] does not (yet?) understand how Clippy uses compiler-internals
99+
using `extern crate` and it also needs to be able to read the source files of the rustc-compiler which are not
100+
available via a `rustup` component at the time of writing.
101+
To work around this, you need to have a copy of the [rustc-repo][rustc_repo] available which can be obtained via
102+
`git clone https://github.com/rust-lang/rust/`.
103+
Then you can run a `cargo dev` command to automatically make Clippy use the rustc-repo via path-dependencies
104+
which rust-analyzer will be able to understand.
105+
Run `cargo dev ra-setup --repo-path <repo-path>` where `<repo-path>` is an absolute path to the rustc repo
106+
you just cloned.
107+
The command will add path-dependencies pointing towards rustc-crates inside the rustc repo to
108108
Clippys `Cargo.toml`s and should allow rust-analyzer to understand most of the types that Clippy uses.
109109
Just make sure to remove the dependencies again before finally making a pull request!
110110

doc/adding_lints.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ because that's clearly a non-descriptive name.
2727

2828
## Setup
2929

30-
When working on Clippy, you will need the current git master version of rustc,
31-
which can change rapidly. Make sure you're working near rust-clippy's master,
32-
and use the `setup-toolchain.sh` script to configure the appropriate toolchain
33-
for the Clippy directory.
30+
See the [Basics](basics.md#get-the-code) documentation.
3431

3532
## Getting Started
3633

@@ -113,7 +110,7 @@ For cargo lints, the process of testing differs in that we are interested in
113110
the `Cargo.toml` manifest file. We also need a minimal crate associated
114111
with that manifest.
115112

116-
If our new lint is named e.g. `foo_categories`, after running `cargo dev new_lint`
113+
If our new lint is named e.g. `foo_categories`, after running `cargo dev new_lint`
117114
we will find by default two new crates, each with its manifest file:
118115

119116
* `tests/ui-cargo/foo_categories/fail/Cargo.toml`: this file should cause the new lint to raise an error.

doc/basics.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Basics for hacking on Clippy
2+
3+
This document explains the basics for hacking on Clippy. Besides others, this
4+
includes how to set-up the development environment, how to build and how to test
5+
Clippy. For a more in depth description on the codebase take a look at [Adding
6+
Lints] or [Common Tools].
7+
8+
[Adding Lints]: https://github.com/rust-lang/rust-clippy/blob/master/doc/adding_lints.md
9+
[Common Tools]: https://github.com/rust-lang/rust-clippy/blob/master/doc/common_tools_writing_lints.md
10+
11+
- [Basics for hacking on Clippy](#basics-for-hacking-on-clippy)
12+
- [Get the code](#get-the-code)
13+
- [Setup](#setup)
14+
- [Building and Testing](#building-and-testing)
15+
- [`cargo dev`](#cargo-dev)
16+
17+
## Get the Code
18+
19+
First, make sure you have checked out the latest version of Clippy. If this is
20+
your first time working on Clippy, create a fork of the repository and clone it
21+
afterwards with the following command:
22+
23+
```bash
24+
git clone [email protected]:<your-username>/rust-clippy
25+
```
26+
27+
If you've already cloned Clippy in the past, update it to the latest version:
28+
29+
```bash
30+
# upstream has to be the remote of the rust-lang/rust-clippy repo
31+
git fetch upstream
32+
# make sure that you are on the master branch
33+
git checkout master
34+
# rebase your master branch on the upstream master
35+
git rebase upstream/master
36+
# push to the master branch of your fork
37+
git push
38+
```
39+
40+
## Setup
41+
42+
Next we need to setup the toolchain to compile Clippy. Since Clippy heavily
43+
relies on compiler internals it is build with the latest rustc master. To get
44+
this toolchain, you can just use the `setup-toolchain.sh` script or use
45+
`rustup-toolchain-install-master`:
46+
47+
```bash
48+
sh setup-toolchain.sh
49+
# OR
50+
cargo install rustup-toolchain-install-master
51+
rustup-toolchain-install-master -f -n master -c rustc-dev -c llvm-tools
52+
rustup override set master
53+
```
54+
55+
## Building and Testing
56+
57+
Once the `master` toolchain is installed, you can build and test Clippy like
58+
every other Rust project:
59+
60+
```bash
61+
cargo build # builds Clippy
62+
cargo test # tests Clippy
63+
```
64+
65+
Since Clippys test suite is pretty big, there are some commands that only run a
66+
subset of Clippys tests:
67+
68+
```bash
69+
# only run UI tests
70+
cargo uitest
71+
# only run UI tests starting with `test_`
72+
TESTNAME="test_" cargo uitest
73+
# only run dogfood tests
74+
cargo test --test dogfood
75+
```
76+
77+
If the output of a UI test differs from the expected output, you can update the
78+
reference file with:
79+
80+
```bash
81+
sh tests/ui/update-all-references.sh
82+
```
83+
84+
For example, this is necessary, if you fix a typo in an error message of a lint
85+
or if you modify a test file to add a test case.
86+
87+
_Note:_ This command may update more files than you intended. In that case only
88+
commit the files you wanted to update.
89+
90+
## `cargo dev`
91+
92+
Clippy has some dev tools to make working on Clippy more convenient. These tools
93+
can be accessed through the `cargo dev` command. Available tools are listed
94+
below. To get more information about these commands, just call them with
95+
`--help`.
96+
97+
```bash
98+
# formats the whole Clippy codebase and all tests
99+
cargo dev fmt
100+
# register or update lint names/groups/...
101+
cargo dev update_lints
102+
# create a new lint and register it
103+
cargo dev new_lint
104+
# (experimental) Setup Clippy to work with rust-analyzer
105+
cargo dev ra-setup
106+
```

0 commit comments

Comments
 (0)