|
1 | 1 | # Linting
|
2 | 2 |
|
3 |
| -We use `cargo clippy` to ensure high-quality code and to enforce a set of best practices for Rust programming. However, not all lints provided by `cargo clippy` are relevant or applicable to our project. |
| 3 | +We use `cargo clippy` to ensure high-quality code and to enforce a set of best practices for Rust programming. |
| 4 | +However, not all lints provided by `cargo clippy` are relevant or applicable to our project. |
4 | 5 | We may choose to ignore some lints if they are unstable, experimental, or specific to our project.
|
5 | 6 | If you are unsure about a lint, please ask us in the [rustup Discord channel](https://discord.com/channels/442252698964721669/463480252723888159).
|
6 | 7 |
|
7 | 8 | ## Manual linting
|
8 | 9 |
|
9 |
| -When checking the codebase with [`clippy`](https://doc.rust-lang.org/stable/clippy/index.html), it is recommended to use: |
| 10 | +When checking the codebase with [`clippy`](https://doc.rust-lang.org/stable/clippy/index.html), |
| 11 | +it is recommended to use the following command: |
10 | 12 |
|
11 | 13 | ```console
|
12 | 14 | $ cargo clippy --all --all-targets --all-features -- -D warnings
|
13 | 15 | ```
|
14 | 16 |
|
| 17 | +Please note the `--all-features` flag: it is used because we need to enable the `test` feature |
| 18 | +to make lints fully work, for which `--all-features` happens to be a convenient shortcut. |
| 19 | + |
| 20 | +The `test` feature is required because `rustup` uses |
| 21 | +[cargo features](https://doc.rust-lang.org/cargo/reference/features.html) to |
| 22 | +[conditionally compile](https://doc.rust-lang.org/reference/conditional-compilation.html) |
| 23 | +support code for integration tests, as `#[cfg(test)]` is only available for unit tests. |
| 24 | + |
| 25 | +If you encounter an issue or wish to speed up the initial analysis, you could also try |
| 26 | +activating only the `test` feature by replacing `--all-features` with `--features=test`. |
| 27 | + |
15 | 28 | ## Rust-Analyzer
|
16 | 29 |
|
17 |
| -When using [`rust-analyzer`](https://rust-analyzer.github.io/) integration in the IDE of your choice, you might want to set the `rust-analyzer.cargo.features` configuration to `"all"` (check the [`rust-analyzer` manual](https://rust-analyzer.github.io/manual.html#configuration) for more details). |
| 30 | +When checking the codebase using `rust-analyzer`, the first thing to do remains unchanged: |
| 31 | +enabling the features. |
18 | 32 |
|
19 |
| -### VSCode/VSCodium setup |
| 33 | +This is done by setting the `rust-analyzer.cargo.features` property to `"all"`. |
20 | 34 |
|
21 |
| -Add |
| 35 | +For example, if you are using `rust-analyzer` within VSCode, you would want to |
| 36 | +add the following to your project's `.vscode/settings.json`[^vscode-global-cfg]: |
22 | 37 |
|
23 |
| -```json |
24 |
| -"rust-analyzer.cargo.features": "all":, |
| 38 | +```jsonc |
| 39 | +"rust-analyzer.cargo.features": "all", |
25 | 40 | ```
|
26 | 41 |
|
27 |
| -in your project at `.vscode/settings.json` |
| 42 | +[^vscode-global-cfg]: |
| 43 | + Alternatively, if you want to apply the configuration to all your Rust projects, |
| 44 | + you can add it to your global configuration at `~/.config/Code/User/settings.json` instead. |
28 | 45 |
|
29 |
| -or |
30 |
| - |
31 |
| -to your global configuration `~/.config/Code/User/settings.json` (although you need to be aware that this will apply to all your Rust projects). |
| 46 | +Alternatively, if you want to enable the `test` feature only, you should set the |
| 47 | +following instead: |
32 | 48 |
|
| 49 | +```jsonc |
| 50 | +"rust-analyzer.cargo.features": ["test"] |
| 51 | +``` |
33 | 52 |
|
34 |
| -## Rationale |
| 53 | +Next, as `rust-analyzer` depends on `cargo check` by default, it is also recommended to |
| 54 | +enable the `cargo clippy` integration by adding the following: |
35 | 55 |
|
36 |
| -`rustup` uses cargo [features](https://doc.rust-lang.org/cargo/reference/features.html) in order to setup [conditional compilation](https://doc.rust-lang.org/reference/conditional-compilation.html) for integration tests as the `#[cfg(test)]` is only available for unit tests. To this end, the `test` feature has been created, however it then needs to be activated in order for tests and linting to fully work. As a shortcut we then propose to activate all features. However, if you encounter an issue, you could try activating only the `test` feature by setting the `rust-analyzer.cargo.features` configuration to `["test"]`. |
| 56 | +```jsonc |
| 57 | +"rust-analyzer.check.command": "clippy", |
| 58 | +``` |
37 | 59 |
|
| 60 | +You might also want to refer to the |
| 61 | +[`rust-analyzer` manual](https://rust-analyzer.github.io/manual.html#configuration) |
| 62 | +for more details on properly setting up `rust-analyzer` in your IDE of choice. |
0 commit comments