Skip to content

Commit 0286d46

Browse files
Fix a help message of empty_line_after_doc_comments for cases where the following item is nameless (#14584)
Fixes #14515. changelog: [`empty_line_after_doc_comments`]: Fix a help message for cases where the following item is nameless.
2 parents 529bb5f + 15fd2ab commit 0286d46

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

Diff for: clippy_lints/src/empty_line_after.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::borrow::Cow;
2+
13
use clippy_utils::diagnostics::span_lint_and_then;
24
use clippy_utils::source::{SpanRangeExt, snippet_indent};
35
use clippy_utils::tokenize_with_text;
@@ -89,7 +91,7 @@ declare_clippy_lint! {
8991
#[derive(Debug)]
9092
struct ItemInfo {
9193
kind: &'static str,
92-
name: Symbol,
94+
name: Option<Symbol>,
9395
span: Span,
9496
mod_items: Option<NodeId>,
9597
}
@@ -315,8 +317,12 @@ impl EmptyLineAfter {
315317
for stop in gaps.iter().flat_map(|gap| gap.prev_chunk) {
316318
stop.comment_out(cx, &mut suggestions);
317319
}
320+
let name = match info.name {
321+
Some(name) => format!("{} `{name}`", info.kind).into(),
322+
None => Cow::from("the following item"),
323+
};
318324
diag.multipart_suggestion_verbose(
319-
format!("if the doc comment should not document `{}` comment it out", info.name),
325+
format!("if the doc comment should not document {name} then comment it out"),
320326
suggestions,
321327
Applicability::MaybeIncorrect,
322328
);
@@ -381,13 +387,10 @@ impl EmptyLineAfter {
381387
) {
382388
self.items.push(ItemInfo {
383389
kind: kind.descr(),
384-
// FIXME: this `sym::empty` can be leaked, see
385-
// https://github.com/rust-lang/rust/pull/138740#discussion_r2021979899
386-
name: if let Some(ident) = ident { ident.name } else { kw::Empty },
387-
span: if let Some(ident) = ident {
388-
span.with_hi(ident.span.hi())
389-
} else {
390-
span.with_hi(span.lo())
390+
name: ident.map(|ident| ident.name),
391+
span: match ident {
392+
Some(ident) => span.with_hi(ident.span.hi()),
393+
None => span.shrink_to_lo(),
391394
},
392395
mod_items: match kind {
393396
ItemKind::Mod(_, _, ModKind::Loaded(items, _, _, _)) => items
@@ -447,7 +450,7 @@ impl EarlyLintPass for EmptyLineAfter {
447450
fn check_crate(&mut self, _: &EarlyContext<'_>, krate: &Crate) {
448451
self.items.push(ItemInfo {
449452
kind: "crate",
450-
name: kw::Crate,
453+
name: Some(kw::Crate),
451454
span: krate.spans.inner_span.with_hi(krate.spans.inner_span.lo()),
452455
mod_items: krate
453456
.items

Diff for: tests/ui/empty_line_after/doc_comments.1.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,9 @@ impl Foo for LineComment {
142142
fn bar() {}
143143
}
144144

145+
//~v empty_line_after_doc_comments
146+
/// Docs for this item.
147+
// fn some_item() {}
148+
impl LineComment {} // or any other nameless item kind
149+
145150
fn main() {}

Diff for: tests/ui/empty_line_after/doc_comments.2.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,10 @@ impl Foo for LineComment {
152152
fn bar() {}
153153
}
154154

155+
//~v empty_line_after_doc_comments
156+
// /// Docs for this item.
157+
// fn some_item() {}
158+
159+
impl LineComment {} // or any other nameless item kind
160+
155161
fn main() {}

Diff for: tests/ui/empty_line_after/doc_comments.rs

+6
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,10 @@ impl Foo for LineComment {
155155
fn bar() {}
156156
}
157157

158+
//~v empty_line_after_doc_comments
159+
/// Docs for this item.
160+
// fn some_item() {}
161+
162+
impl LineComment {} // or any other nameless item kind
163+
158164
fn main() {}

Diff for: tests/ui/empty_line_after/doc_comments.stderr

+22-6
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ LL | fn new_code() {}
8787
| ----------- the comment documents this function
8888
|
8989
= help: if the empty line is unintentional, remove it
90-
help: if the doc comment should not document `new_code` comment it out
90+
help: if the doc comment should not document function `new_code` then comment it out
9191
|
9292
LL | // /// docs for `old_code`
9393
| ++
@@ -107,7 +107,7 @@ LL | struct Multiple;
107107
| --------------- the comment documents this struct
108108
|
109109
= help: if the empty lines are unintentional, remove them
110-
help: if the doc comment should not document `Multiple` comment it out
110+
help: if the doc comment should not document struct `Multiple` then comment it out
111111
|
112112
LL ~ // /// Docs
113113
LL ~ // /// for OldA
@@ -149,7 +149,7 @@ LL | fn new_code() {}
149149
| ----------- the comment documents this function
150150
|
151151
= help: if the empty line is unintentional, remove it
152-
help: if the doc comment should not document `new_code` comment it out
152+
help: if the doc comment should not document function `new_code` then comment it out
153153
|
154154
LL - /**
155155
LL + /*
@@ -167,7 +167,7 @@ LL | fn new_code2() {}
167167
| ------------ the comment documents this function
168168
|
169169
= help: if the empty line is unintentional, remove it
170-
help: if the doc comment should not document `new_code2` comment it out
170+
help: if the doc comment should not document function `new_code2` then comment it out
171171
|
172172
LL | // /// Docs for `old_code2`
173173
| ++
@@ -183,10 +183,26 @@ LL | fn bar() {}
183183
| ------ the comment documents this function
184184
|
185185
= help: if the empty line is unintentional, remove it
186-
help: if the doc comment should not document `bar` comment it out
186+
help: if the doc comment should not document function `bar` then comment it out
187187
|
188188
LL | // /// comment on assoc item
189189
| ++
190190

191-
error: aborting due to 11 previous errors
191+
error: empty line after doc comment
192+
--> tests/ui/empty_line_after/doc_comments.rs:159:1
193+
|
194+
LL | / /// Docs for this item.
195+
LL | | // fn some_item() {}
196+
LL | |
197+
| |_^
198+
LL | impl LineComment {} // or any other nameless item kind
199+
| - the comment documents this implementation
200+
|
201+
= help: if the empty line is unintentional, remove it
202+
help: if the doc comment should not document the following item then comment it out
203+
|
204+
LL | // /// Docs for this item.
205+
| ++
206+
207+
error: aborting due to 12 previous errors
192208

0 commit comments

Comments
 (0)