Skip to content

Commit 139a0cb

Browse files
committed
fix(lint): dont embed lint docs in cargo binary
Instead we read and concat all lint docs via xtask-lint-docs
1 parent 074062a commit 139a0cb

File tree

5 files changed

+81
-55
lines changed

5 files changed

+81
-55
lines changed

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use anyhow::Context as _;
12
use cargo::util::command_prelude::{ArgMatchesExt, flag};
23
use cargo::util::lints::{Lint, LintLevel};
34
use itertools::Itertools;
5+
46
use std::fmt::Write;
57
use std::path::PathBuf;
68

@@ -22,7 +24,7 @@ fn main() -> anyhow::Result<()> {
2224
.iter()
2325
.sorted_by_key(|lint| lint.name)
2426
{
25-
if lint.docs.is_some() {
27+
if !lint.hidden {
2628
let sectipn = match lint.default_level {
2729
LintLevel::Allow => &mut allow,
2830
LintLevel::Warn => &mut warn,
@@ -71,10 +73,16 @@ fn main() -> anyhow::Result<()> {
7173
Ok(())
7274
}
7375

74-
fn add_lint(lint: &Lint, buf: &mut String) -> std::fmt::Result {
76+
fn add_lint(lint: &Lint, buf: &mut String) -> anyhow::Result<()> {
7577
writeln!(buf, "## `{}`", lint.name)?;
7678
writeln!(buf, "Set to `{}` by default", lint.default_level)?;
77-
writeln!(buf, "{}\n", lint.docs.as_ref().unwrap())
79+
80+
let src_path = lint_docs_src_path(lint);
81+
let docs = std::fs::read_to_string(&src_path)
82+
.with_context(|| format!("failed to read {}", src_path.display()))?;
83+
writeln!(buf, "{docs}\n",)?;
84+
85+
Ok(())
7886
}
7987

8088
fn add_level_section(level: LintLevel, lint_names: &[&str], buf: &mut String) -> std::fmt::Result {
@@ -107,3 +115,17 @@ fn lint_docs_output_path() -> PathBuf {
107115
};
108116
path
109117
}
118+
119+
/// Gets the markdown source of the lint documentation.
120+
fn lint_docs_src_path(lint: &Lint) -> PathBuf {
121+
let pkg_root = env!("CARGO_MANIFEST_DIR");
122+
let ws_root = PathBuf::from(format!("{pkg_root}/../.."));
123+
let path = {
124+
let path = ws_root
125+
.join("src/cargo/util/lints")
126+
.join(lint.name)
127+
.with_extension("md");
128+
path.canonicalize().unwrap_or(path)
129+
};
130+
path
131+
}

src/cargo/util/lints.rs

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,8 @@ pub struct Lint {
259259
pub default_level: LintLevel,
260260
pub edition_lint_opts: Option<(Edition, LintLevel)>,
261261
pub feature_gate: Option<&'static Feature>,
262-
/// This is a markdown formatted string that will be used when generating
263-
/// the lint documentation. If docs is `None`, the lint will not be
264-
/// documented.
265-
pub docs: Option<&'static str>,
262+
/// Whether the lint should be hidden or documented.
263+
pub hidden: bool,
266264
}
267265

268266
impl Lint {
@@ -436,7 +434,7 @@ const IM_A_TEAPOT: Lint = Lint {
436434
default_level: LintLevel::Allow,
437435
edition_lint_opts: None,
438436
feature_gate: Some(Feature::test_dummy_unstable()),
439-
docs: None,
437+
hidden: true,
440438
};
441439

442440
pub fn check_im_a_teapot(
@@ -488,33 +486,7 @@ const BLANKET_HINT_MOSTLY_UNUSED: Lint = Lint {
488486
default_level: LintLevel::Warn,
489487
edition_lint_opts: None,
490488
feature_gate: None,
491-
docs: Some(
492-
r#"
493-
### What it does
494-
Checks if `hint-mostly-unused` being applied to all dependencies.
495-
496-
### Why it is bad
497-
`hint-mostly-unused` indicates that most of a crate's API surface will go
498-
unused by anything depending on it; this hint can speed up the build by
499-
attempting to minimize compilation time for items that aren't used at all.
500-
Misapplication to crates that don't fit that criteria will slow down the build
501-
rather than speeding it up. It should be selectively applied to dependencies
502-
that meet these criteria. Applying it globally is always a misapplication and
503-
will likely slow down the build.
504-
505-
### Example
506-
```toml
507-
[profile.dev.package."*"]
508-
hint-mostly-unused = true
509-
```
510-
511-
Should instead be:
512-
```toml
513-
[profile.dev.package.huge-mostly-unused-dependency]
514-
hint-mostly-unused = true
515-
```
516-
"#,
517-
),
489+
hidden: false,
518490
};
519491

520492
pub fn blanket_hint_mostly_unused(
@@ -640,24 +612,7 @@ const UNKNOWN_LINTS: Lint = Lint {
640612
default_level: LintLevel::Warn,
641613
edition_lint_opts: None,
642614
feature_gate: None,
643-
docs: Some(
644-
r#"
645-
### What it does
646-
Checks for unknown lints in the `[lints.cargo]` table
647-
648-
### Why it is bad
649-
- The lint name could be misspelled, leading to confusion as to why it is
650-
not working as expected
651-
- The unknown lint could end up causing an error if `cargo` decides to make
652-
a lint with the same name in the future
653-
654-
### Example
655-
```toml
656-
[lints.cargo]
657-
this-lint-does-not-exist = "warn"
658-
```
659-
"#,
660-
),
615+
hidden: false,
661616
};
662617

663618
fn output_unknown_lints(
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
### What it does
2+
3+
Checks if `hint-mostly-unused` being applied to all dependencies.
4+
5+
### Why it is bad
6+
7+
`hint-mostly-unused` indicates that most of a crate's API surface will go
8+
unused by anything depending on it; this hint can speed up the build by
9+
attempting to minimize compilation time for items that aren't used at all.
10+
Misapplication to crates that don't fit that criteria will slow down the build
11+
rather than speeding it up. It should be selectively applied to dependencies
12+
that meet these criteria. Applying it globally is always a misapplication and
13+
will likely slow down the build.
14+
15+
### Example
16+
17+
```toml
18+
[profile.dev.package."*"]
19+
hint-mostly-unused = true
20+
```
21+
22+
Should instead be:
23+
24+
```toml
25+
[profile.dev.package.huge-mostly-unused-dependency]
26+
hint-mostly-unused = true
27+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### What it does
2+
3+
Checks for unknown lints in the `[lints.cargo]` table
4+
5+
### Why it is bad
6+
7+
- The lint name could be misspelled, leading to confusion as to why it is
8+
not working as expected
9+
- The unknown lint could end up causing an error if `cargo` decides to make
10+
a lint with the same name in the future
11+
12+
### Example
13+
14+
```toml
15+
[lints.cargo]
16+
this-lint-does-not-exist = "warn"
17+
```

src/doc/src/reference/lints.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ These lints are all set to the 'warn' level by default.
1010

1111
## `blanket_hint_mostly_unused`
1212
Set to `warn` by default
13-
1413
### What it does
14+
1515
Checks if `hint-mostly-unused` being applied to all dependencies.
1616

1717
### Why it is bad
18+
1819
`hint-mostly-unused` indicates that most of a crate's API surface will go
1920
unused by anything depending on it; this hint can speed up the build by
2021
attempting to minimize compilation time for items that aren't used at all.
@@ -24,12 +25,14 @@ that meet these criteria. Applying it globally is always a misapplication and
2425
will likely slow down the build.
2526

2627
### Example
28+
2729
```toml
2830
[profile.dev.package."*"]
2931
hint-mostly-unused = true
3032
```
3133

3234
Should instead be:
35+
3336
```toml
3437
[profile.dev.package.huge-mostly-unused-dependency]
3538
hint-mostly-unused = true
@@ -38,17 +41,19 @@ hint-mostly-unused = true
3841

3942
## `unknown_lints`
4043
Set to `warn` by default
41-
4244
### What it does
45+
4346
Checks for unknown lints in the `[lints.cargo]` table
4447

4548
### Why it is bad
49+
4650
- The lint name could be misspelled, leading to confusion as to why it is
4751
not working as expected
4852
- The unknown lint could end up causing an error if `cargo` decides to make
4953
a lint with the same name in the future
5054

5155
### Example
56+
5257
```toml
5358
[lints.cargo]
5459
this-lint-does-not-exist = "warn"

0 commit comments

Comments
 (0)