Skip to content

Commit 6c9b8d7

Browse files
committed
Generate rustdoc lints
1 parent 5051717 commit 6c9b8d7

File tree

2 files changed

+127
-54
lines changed

2 files changed

+127
-54
lines changed

crates/ide_db/src/helpers/generated_lints.rs

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,46 @@ pub const DEFAULT_LINTS: &[Lint] = &[
502502
},
503503
];
504504

505+
pub const RUSTDOC_LINTS: &[Lint] = &[
506+
Lint {
507+
label: "rustdoc::all",
508+
description: r##"lint group for: rustdoc::broken-intra-doc-links, rustdoc::private-intra-doc-links, rustdoc::missing-doc-code-examples, rustdoc::private-doc-tests, rustdoc::invalid-codeblock-attributes, rustdoc::invalid-rust-codeblocks, rustdoc::invalid-html-tags, rustdoc::bare-urls, rustdoc::missing-crate-level-docs"##,
509+
},
510+
Lint { label: "rustdoc::bare_urls", description: r##"detects URLs that are not hyperlinks"## },
511+
Lint {
512+
label: "rustdoc::broken_intra_doc_links",
513+
description: r##"failures in resolving intra-doc link targets"##,
514+
},
515+
Lint {
516+
label: "rustdoc::invalid_codeblock_attributes",
517+
description: r##"codeblock attribute looks a lot like a known one"##,
518+
},
519+
Lint {
520+
label: "rustdoc::invalid_html_tags",
521+
description: r##"detects invalid HTML tags in doc comments"##,
522+
},
523+
Lint {
524+
label: "rustdoc::invalid_rust_codeblocks",
525+
description: r##"codeblock could not be parsed as valid Rust or is empty"##,
526+
},
527+
Lint {
528+
label: "rustdoc::missing_crate_level_docs",
529+
description: r##"detects crates with no crate-level documentation"##,
530+
},
531+
Lint {
532+
label: "rustdoc::missing_doc_code_examples",
533+
description: r##"detects publicly-exported items without code samples in their documentation"##,
534+
},
535+
Lint {
536+
label: "rustdoc::private_doc_tests",
537+
description: r##"detects code samples in docs of private items not documented by rustdoc"##,
538+
},
539+
Lint {
540+
label: "rustdoc::private_intra_doc_links",
541+
description: r##"linking from a public item to a private one"##,
542+
},
543+
];
544+
505545
pub const FEATURES: &[Lint] = &[
506546
Lint {
507547
label: "abi_c_cmse_nonsecure_call",
@@ -5572,11 +5612,9 @@ outside ticks in documentation."##,
55725612
},
55735613
Lint {
55745614
label: "clippy::double_must_use",
5575-
description: r##"Checks for a [`#[must_use]`] attribute without
5615+
description: r##"Checks for a `#[must_use]` attribute without
55765616
further information on functions and methods that return a type already
5577-
marked as `#[must_use]`.
5578-
5579-
[`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"##,
5617+
marked as `#[must_use]`."##,
55805618
},
55815619
Lint {
55825620
label: "clippy::double_neg",
@@ -5821,6 +5859,12 @@ derives the Copy trait"##,
58215859
label: "clippy::forget_ref",
58225860
description: r##"Checks for calls to `std::mem::forget` with a reference
58235861
instead of an owned value."##,
5862+
},
5863+
Lint {
5864+
label: "clippy::format_in_format_args",
5865+
description: r##"Detects `format!` within the arguments of another macro that does
5866+
formatting such as `format!` itself, `write!` or `println!`. Suggests
5867+
inlining the `format!` call."##,
58245868
},
58255869
Lint {
58265870
label: "clippy::from_iter_instead_of_collect",
@@ -6120,8 +6164,7 @@ where expr has a type that implements `Drop`"##,
61206164
},
61216165
Lint {
61226166
label: "clippy::let_underscore_must_use",
6123-
description: r##"Checks for `let _ = <expr>`
6124-
where expr is #[must_use]"##,
6167+
description: r##"Checks for `let _ = <expr>` where expr is `#[must_use]`"##,
61256168
},
61266169
Lint { label: "clippy::let_unit_value", description: r##"Checks for binding a unit value."## },
61276170
Lint {
@@ -6194,23 +6237,7 @@ be more readably expressed as `(3..8).contains(x)`."##,
61946237
},
61956238
Lint {
61966239
label: "clippy::manual_split_once",
6197-
description: r##"**What it does:** Checks for usages of `str::splitn(2, _)`
6198-
6199-
**Why is this bad?** `split_once` is both clearer in intent and slightly more efficient.
6200-
6201-
**Known problems:** None.
6202-
6203-
**Example:**
6204-
6205-
```rust
6206-
// Bad
6207-
let (key, value) = _.splitn(2, '=').next_tuple()?;
6208-
let value = _.splitn(2, '=').nth(1)?;
6209-
6210-
// Good
6211-
let (key, value) = _.split_once('=')?;
6212-
let value = _.split_once('=')?.1;
6213-
```"##,
6240+
description: r##"Checks for usages of `str::splitn(2, _)`"##,
62146241
},
62156242
Lint {
62166243
label: "clippy::manual_str_repeat",
@@ -6304,6 +6331,10 @@ instead. It also checks for `if let &foo = bar` blocks."##,
63046331
label: "clippy::match_single_binding",
63056332
description: r##"Checks for useless match that binds to only one value."##,
63066333
},
6334+
Lint {
6335+
label: "clippy::match_str_case_mismatch",
6336+
description: r##"Checks for `match` expressions modifying the case of a string with non-compliant arms"##,
6337+
},
63076338
Lint {
63086339
label: "clippy::match_wild_err_arm",
63096340
description: r##"Checks for arm which matches all errors with `Err(_)`
@@ -6433,17 +6464,13 @@ used."##,
64336464
Lint {
64346465
label: "clippy::must_use_candidate",
64356466
description: r##"Checks for public functions that have no
6436-
[`#[must_use]`] attribute, but return something not already marked
6437-
must-use, have no mutable arg and mutate no statics.
6438-
6439-
[`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"##,
6467+
`#[must_use]` attribute, but return something not already marked
6468+
must-use, have no mutable arg and mutate no statics."##,
64406469
},
64416470
Lint {
64426471
label: "clippy::must_use_unit",
6443-
description: r##"Checks for a [`#[must_use]`] attribute on
6444-
unit-returning functions and methods.
6445-
6446-
[`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"##,
6472+
description: r##"Checks for a `#[must_use]` attribute on
6473+
unit-returning functions and methods."##,
64476474
},
64486475
Lint {
64496476
label: "clippy::mut_from_ref",
@@ -6591,6 +6618,10 @@ implementation of
65916618
label: "clippy::no_effect",
65926619
description: r##"Checks for statements which have no effect."##,
65936620
},
6621+
Lint {
6622+
label: "clippy::no_effect_underscore_binding",
6623+
description: r##"Checks for binding to underscore prefixed variable without side-effects."##,
6624+
},
65946625
Lint {
65956626
label: "clippy::non_ascii_literal",
65966627
description: r##"Checks for non-ASCII characters in string literals."##,
@@ -7155,6 +7186,12 @@ assign a value in it."##,
71557186
label: "clippy::to_string_in_display",
71567187
description: r##"Checks for uses of `to_string()` in `Display` traits."##,
71577188
},
7189+
Lint {
7190+
label: "clippy::to_string_in_format_args",
7191+
description: r##"Checks for [`ToString::to_string`](https://doc.rust-lang.org/std/string/trait.ToString.html#tymethod.to_string)
7192+
applied to a type that implements [`Display`](https://doc.rust-lang.org/std/fmt/trait.Display.html)
7193+
in a macro that does formatting."##,
7194+
},
71587195
Lint { label: "clippy::todo", description: r##"Checks for usage of `todo!`."## },
71597196
Lint {
71607197
label: "clippy::too_many_arguments",
@@ -7194,6 +7231,7 @@ syntax specifications for trait bounds are used simultaneously."##,
71947231
label: "clippy::transmute_int_to_float",
71957232
description: r##"Checks for transmutes from an integer to a float."##,
71967233
},
7234+
Lint { label: "clippy::transmute_num_to_bytes", description: r##""## },
71977235
Lint {
71987236
label: "clippy::transmute_ptr_to_ptr",
71997237
description: r##"Checks for transmutes from a pointer to a pointer, or
@@ -7256,6 +7294,12 @@ that is not equal to its
72567294
label: "clippy::uninit_assumed_init",
72577295
description: r##"Checks for `MaybeUninit::uninit().assume_init()`."##,
72587296
},
7297+
Lint {
7298+
label: "clippy::uninit_vec",
7299+
description: r##"Checks for `set_len()` call that creates `Vec` with uninitialized elements.
7300+
This is commonly caused by calling `set_len()` right after allocating or
7301+
reserving a buffer with `new()`, `default()`, `with_capacity()`, or `reserve()`."##,
7302+
},
72597303
Lint {
72607304
label: "clippy::unit_arg",
72617305
description: r##"Checks for passing a unit value as an argument to a function without using a

crates/ide_db/src/tests/sourcegen_lints.rs

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Generates descriptors structure for unstable feature from Unstable Book
22
use std::{borrow::Cow, fs, path::Path};
33

4+
use itertools::Itertools;
45
use stdx::format_to;
56
use test_utils::project_root;
67
use xshell::cmd;
@@ -43,39 +44,62 @@ pub struct Lint {
4344
}
4445

4546
fn generate_lint_descriptor(buf: &mut String) {
46-
let stdout = cmd!("rustc -W help").read().unwrap();
47+
// FIXME: rustdoc currently requires an input file for -Whelp cc https://github.com/rust-lang/rust/pull/88831
48+
let file = project_root().join(file!());
49+
let stdout = cmd!("rustdoc -W help {file}").read().unwrap();
4750
let start_lints = stdout.find("---- ------- -------").unwrap();
4851
let start_lint_groups = stdout.find("---- ---------").unwrap();
49-
let end_lints = stdout.find("Lint groups provided by rustc:").unwrap();
50-
let end_lint_groups = stdout
51-
.find("Lint tools like Clippy can provide additional lints and lint groups.")
52-
.unwrap();
52+
let start_lints_rustdoc =
53+
stdout.find("Lint checks provided by plugins loaded by this crate:").unwrap();
54+
let start_lint_groups_rustdoc =
55+
stdout.find("Lint groups provided by plugins loaded by this crate:").unwrap();
56+
5357
buf.push_str(r#"pub const DEFAULT_LINTS: &[Lint] = &["#);
5458
buf.push('\n');
55-
let mut lints = stdout[start_lints..end_lints]
56-
.lines()
57-
.skip(1)
58-
.filter(|l| !l.is_empty())
59-
.map(|line| {
59+
60+
let lints = stdout[start_lints..].lines().skip(1).take_while(|l| !l.is_empty()).map(|line| {
61+
let (name, rest) = line.trim().split_once(char::is_whitespace).unwrap();
62+
let (_default_level, description) = rest.trim().split_once(char::is_whitespace).unwrap();
63+
(name.trim(), Cow::Borrowed(description.trim()))
64+
});
65+
let lint_groups =
66+
stdout[start_lint_groups..].lines().skip(1).take_while(|l| !l.is_empty()).map(|line| {
67+
let (name, lints) = line.trim().split_once(char::is_whitespace).unwrap();
68+
(name.trim(), format!("lint group for: {}", lints.trim()).into())
69+
});
70+
71+
lints.chain(lint_groups).sorted_by(|(ident, _), (ident2, _)| ident.cmp(ident2)).for_each(
72+
|(name, description)| push_lint_completion(buf, &name.replace("-", "_"), &description),
73+
);
74+
buf.push_str("];\n");
75+
76+
// rustdoc
77+
78+
buf.push('\n');
79+
buf.push_str(r#"pub const RUSTDOC_LINTS: &[Lint] = &["#);
80+
buf.push('\n');
81+
82+
let lints_rustdoc =
83+
stdout[start_lints_rustdoc..].lines().skip(2).take_while(|l| !l.is_empty()).map(|line| {
6084
let (name, rest) = line.trim().split_once(char::is_whitespace).unwrap();
6185
let (_default_level, description) =
6286
rest.trim().split_once(char::is_whitespace).unwrap();
6387
(name.trim(), Cow::Borrowed(description.trim()))
64-
})
65-
.collect::<Vec<_>>();
66-
lints.extend(
67-
stdout[start_lint_groups..end_lint_groups].lines().skip(1).filter(|l| !l.is_empty()).map(
88+
});
89+
let lint_groups_rustdoc =
90+
stdout[start_lint_groups_rustdoc..].lines().skip(2).take_while(|l| !l.is_empty()).map(
6891
|line| {
6992
let (name, lints) = line.trim().split_once(char::is_whitespace).unwrap();
7093
(name.trim(), format!("lint group for: {}", lints.trim()).into())
7194
},
72-
),
73-
);
74-
75-
lints.sort_by(|(ident, _), (ident2, _)| ident.cmp(ident2));
76-
lints.into_iter().for_each(|(name, description)| {
77-
push_lint_completion(buf, &name.replace("-", "_"), &description)
78-
});
95+
);
96+
97+
lints_rustdoc
98+
.chain(lint_groups_rustdoc)
99+
.sorted_by(|(ident, _), (ident2, _)| ident.cmp(ident2))
100+
.for_each(|(name, description)| {
101+
push_lint_completion(buf, &name.replace("-", "_"), &description)
102+
});
79103
buf.push_str("];\n");
80104
}
81105

@@ -126,8 +150,13 @@ fn generate_descriptor_clippy(buf: &mut String, path: &Path) {
126150
clippy_lints.push(clippy_lint)
127151
} else if let Some(line) = line.strip_prefix(r#""docs": ""#) {
128152
let prefix_to_strip = r#" ### What it does"#;
129-
// FIXME: replace unwrap_or with expect again, currently there is one lint that uses a different format in the json...
130-
let line = line.strip_prefix(prefix_to_strip).unwrap_or(line);
153+
let line = match line.strip_prefix(prefix_to_strip) {
154+
Some(line) => line,
155+
None => {
156+
eprintln!("unexpected clippy prefix for {}", clippy_lints.last().unwrap().id);
157+
continue;
158+
}
159+
};
131160
// Only take the description, any more than this is a lot of additional data we would embed into the exe
132161
// which seems unnecessary
133162
let up_to = line.find(r#"###"#).expect("no second section found?");

0 commit comments

Comments
 (0)