Skip to content

Commit 085d9d1

Browse files
committed
feat: Add documentation for lint groups
1 parent 87b1b44 commit 085d9d1

3 files changed

Lines changed: 78 additions & 8 deletions

File tree

crates/xtask-lint-docs/src/main.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ fn main() -> anyhow::Result<()> {
4141
)?;
4242
writeln!(buf)?;
4343

44+
lint_groups(&mut buf)?;
45+
4446
if !allow.is_empty() {
4547
add_level_section(LintLevel::Allow, &allow, &mut buf)?;
4648
}
@@ -69,13 +71,51 @@ fn main() -> anyhow::Result<()> {
6971
Ok(())
7072
}
7173

72-
fn add_lint(lint: &Lint, buf: &mut String) -> std::fmt::Result {
73-
writeln!(buf, "## `{}`", lint.name)?;
74+
fn lint_groups(buf: &mut String) -> anyhow::Result<()> {
75+
let (max_name_len, max_desc_len) = cargo::lints::LINT_GROUPS.iter().filter(|g| !g.hidden).fold(
76+
(0, 0),
77+
|(max_name_len, max_desc_len), group| {
78+
// We add 9 to account for the "cargo::" prefix and backticks
79+
let name_len = group.name.chars().count() + 9;
80+
let desc_len = group.desc.chars().count();
81+
(max_name_len.max(name_len), max_desc_len.max(desc_len))
82+
},
83+
);
84+
let default_level_len = "Default level".chars().count();
85+
writeln!(buf, "\n")?;
7486
writeln!(
7587
buf,
76-
"Set to `{}` by default",
77-
lint.primary_group.default_level
88+
"| {:<max_name_len$} | {:<max_desc_len$} | Default level |",
89+
"Group", "Description",
7890
)?;
91+
writeln!(
92+
buf,
93+
"|-{}-|-{}-|-{}-|",
94+
"-".repeat(max_name_len),
95+
"-".repeat(max_desc_len),
96+
"-".repeat(default_level_len)
97+
)?;
98+
for group in cargo::lints::LINT_GROUPS.iter() {
99+
if group.hidden {
100+
continue;
101+
}
102+
let group_name = format!("`cargo::{}`", group.name);
103+
writeln!(
104+
buf,
105+
"| {:<max_name_len$} | {:<max_desc_len$} | {:<default_level_len$} |",
106+
group_name,
107+
group.desc,
108+
group.default_level.to_string(),
109+
)?;
110+
}
111+
writeln!(buf, "\n")?;
112+
Ok(())
113+
}
114+
115+
fn add_lint(lint: &Lint, buf: &mut String) -> std::fmt::Result {
116+
writeln!(buf, "## `{}`", lint.name)?;
117+
writeln!(buf, "Group: `{}`\n", lint.primary_group.name)?;
118+
writeln!(buf, "Level: `{}`", lint.primary_group.default_level)?;
79119
writeln!(buf, "{}\n", lint.docs.as_ref().unwrap())
80120
}
81121

src/cargo/lints/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::path::Path;
1717
pub mod rules;
1818
pub use rules::LINTS;
1919

20-
const LINT_GROUPS: &[LintGroup] = &[
20+
pub const LINT_GROUPS: &[LintGroup] = &[
2121
COMPLEXITY,
2222
CORRECTNESS,
2323
NURSERY,
@@ -269,62 +269,71 @@ pub struct LintGroup {
269269
pub default_level: LintLevel,
270270
pub desc: &'static str,
271271
pub feature_gate: Option<&'static Feature>,
272+
pub hidden: bool,
272273
}
273274

274275
const COMPLEXITY: LintGroup = LintGroup {
275276
name: "complexity",
276277
desc: "code that does something simple but in a complex way",
277278
default_level: LintLevel::Warn,
278279
feature_gate: None,
280+
hidden: false,
279281
};
280282

281283
const CORRECTNESS: LintGroup = LintGroup {
282284
name: "correctness",
283285
desc: "code that is outright wrong or useless",
284286
default_level: LintLevel::Deny,
285287
feature_gate: None,
288+
hidden: false,
286289
};
287290

288291
const NURSERY: LintGroup = LintGroup {
289292
name: "nursery",
290293
desc: "new lints that are still under development",
291294
default_level: LintLevel::Allow,
292295
feature_gate: None,
296+
hidden: false,
293297
};
294298

295299
const PEDANTIC: LintGroup = LintGroup {
296300
name: "pedantic",
297301
desc: "lints which are rather strict or have occasional false positives",
298302
default_level: LintLevel::Allow,
299303
feature_gate: None,
304+
hidden: false,
300305
};
301306

302307
const PERF: LintGroup = LintGroup {
303308
name: "perf",
304309
desc: "code that can be written to run faster",
305310
default_level: LintLevel::Warn,
306311
feature_gate: None,
312+
hidden: false,
307313
};
308314

309315
const RESTRICTION: LintGroup = LintGroup {
310316
name: "restriction",
311317
desc: "lints which prevent the use of Cargo features",
312318
default_level: LintLevel::Allow,
313319
feature_gate: None,
320+
hidden: false,
314321
};
315322

316323
const STYLE: LintGroup = LintGroup {
317324
name: "style",
318325
desc: "code that should be written in a more idiomatic way",
319326
default_level: LintLevel::Warn,
320327
feature_gate: None,
328+
hidden: false,
321329
};
322330

323331
const SUSPICIOUS: LintGroup = LintGroup {
324332
name: "suspicious",
325333
desc: "code that is most likely wrong or useless",
326334
default_level: LintLevel::Warn,
327335
feature_gate: None,
336+
hidden: false,
328337
};
329338

330339
/// This lint group is only to be used for testing purposes
@@ -333,6 +342,7 @@ const TEST_DUMMY_UNSTABLE: LintGroup = LintGroup {
333342
desc: "test_dummy_unstable is meant to only be used in tests",
334343
default_level: LintLevel::Allow,
335344
feature_gate: Some(Feature::test_dummy_unstable()),
345+
hidden: true,
336346
};
337347

338348
#[derive(Copy, Clone, Debug)]

src/doc/src/reference/lints.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
Note: [Cargo's linting system is unstable](unstable.md#lintscargo) and can only be used on nightly toolchains
44

5+
6+
7+
| Group | Description | Default level |
8+
|----------------------|------------------------------------------------------------------|---------------|
9+
| `cargo::complexity` | code that does something simple but in a complex way | warn |
10+
| `cargo::correctness` | code that is outright wrong or useless | deny |
11+
| `cargo::nursery` | new lints that are still under development | allow |
12+
| `cargo::pedantic` | lints which are rather strict or have occasional false positives | allow |
13+
| `cargo::perf` | code that can be written to run faster | warn |
14+
| `cargo::restriction` | lints which prevent the use of Cargo features | allow |
15+
| `cargo::style` | code that should be written in a more idiomatic way | warn |
16+
| `cargo::suspicious` | code that is most likely wrong or useless | warn |
17+
18+
519
## Allowed-by-default
620

721
These lints are all set to the 'allow' level by default.
@@ -14,7 +28,9 @@ These lints are all set to the 'warn' level by default.
1428
- [`unknown_lints`](#unknown_lints)
1529

1630
## `blanket_hint_mostly_unused`
17-
Set to `warn` by default
31+
Group: `suspicious`
32+
33+
Level: `warn`
1834

1935
### What it does
2036
Checks if `hint-mostly-unused` being applied to all dependencies.
@@ -42,7 +58,9 @@ hint-mostly-unused = true
4258

4359

4460
## `implicit_minimum_version_req`
45-
Set to `allow` by default
61+
Group: `pedantic`
62+
63+
Level: `allow`
4664

4765
### What it does
4866

@@ -91,7 +109,9 @@ serde = "1.0.219"
91109

92110

93111
## `unknown_lints`
94-
Set to `warn` by default
112+
Group: `suspicious`
113+
114+
Level: `warn`
95115

96116
### What it does
97117
Checks for unknown lints in the `[lints.cargo]` table

0 commit comments

Comments
 (0)