Skip to content

Commit 187dab9

Browse files
committed
improve unused doc comment diagnostic reporting
Report all unused attributes on a given doc comment instead of just the first one, and extend the span of sugared doc comments to encompass the whole comment.
1 parent ad30e9a commit 187dab9

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

src/librustc_lint/builtin.rs

+32-9
Original file line numberDiff line numberDiff line change
@@ -753,27 +753,50 @@ impl LintPass for UnusedDocComment {
753753
}
754754

755755
impl UnusedDocComment {
756-
fn warn_if_doc<'a, 'tcx,
757-
I: Iterator<Item=&'a ast::Attribute>,
758-
C: LintContext<'tcx>>(&self, mut attrs: I, cx: &C) {
759-
if let Some(attr) = attrs.find(|a| a.is_value_str() && a.check_name("doc")) {
760-
cx.struct_span_lint(UNUSED_DOC_COMMENTS, attr.span, "doc comment not used by rustdoc")
761-
.emit();
756+
fn warn_if_doc(&self, cx: &EarlyContext, attrs: &[ast::Attribute]) {
757+
let mut attrs = attrs.into_iter().peekable();
758+
759+
// Accumulate a single span for sugared doc comments.
760+
let mut sugared_span: Option<Span> = None;
761+
762+
while let Some(attr) = attrs.next() {
763+
if attr.is_sugared_doc {
764+
sugared_span = Some(
765+
sugared_span.map_or_else(
766+
|| attr.span,
767+
|span| span.with_hi(attr.span.hi()),
768+
),
769+
);
770+
}
771+
772+
if attrs.peek().map(|next_attr| next_attr.is_sugared_doc).unwrap_or_default() {
773+
continue;
774+
}
775+
776+
let span = sugared_span.take().unwrap_or_else(|| attr.span);
777+
778+
if attr.name() == "doc" {
779+
cx.struct_span_lint(
780+
UNUSED_DOC_COMMENTS,
781+
span,
782+
"doc comment not used by rustdoc",
783+
).emit();
784+
}
762785
}
763786
}
764787
}
765788

766789
impl EarlyLintPass for UnusedDocComment {
767790
fn check_local(&mut self, cx: &EarlyContext, decl: &ast::Local) {
768-
self.warn_if_doc(decl.attrs.iter(), cx);
791+
self.warn_if_doc(cx, &decl.attrs);
769792
}
770793

771794
fn check_arm(&mut self, cx: &EarlyContext, arm: &ast::Arm) {
772-
self.warn_if_doc(arm.attrs.iter(), cx);
795+
self.warn_if_doc(cx, &arm.attrs);
773796
}
774797

775798
fn check_expr(&mut self, cx: &EarlyContext, expr: &ast::Expr) {
776-
self.warn_if_doc(expr.attrs.iter(), cx);
799+
self.warn_if_doc(cx, &expr.attrs);
777800
}
778801
}
779802

src/test/ui/useless_comment.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ fn foo() {
44
/// a //~ ERROR doc comment not used by rustdoc
55
let x = 12;
66

7-
/// b //~ doc comment not used by rustdoc
7+
/// multi-line //~ doc comment not used by rustdoc
8+
/// doc comment
9+
/// that is unused
810
match x {
911
/// c //~ ERROR doc comment not used by rustdoc
1012
1 => {},
@@ -13,6 +15,10 @@ fn foo() {
1315

1416
/// foo //~ ERROR doc comment not used by rustdoc
1517
unsafe {}
18+
19+
#[doc = "foo"] //~ ERROR doc comment not used by rustdoc
20+
#[doc = "bar"] //~ ERROR doc comment not used by rustdoc
21+
3;
1622
}
1723

1824
fn main() {

src/test/ui/useless_comment.stderr

+19-5
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,34 @@ LL | #![deny(unused_doc_comments)]
1313
error: doc comment not used by rustdoc
1414
--> $DIR/useless_comment.rs:7:5
1515
|
16-
LL | /// b //~ doc comment not used by rustdoc
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
LL | / /// multi-line //~ doc comment not used by rustdoc
17+
LL | | /// doc comment
18+
LL | | /// that is unused
19+
| |______________________^
1820

1921
error: doc comment not used by rustdoc
20-
--> $DIR/useless_comment.rs:9:9
22+
--> $DIR/useless_comment.rs:11:9
2123
|
2224
LL | /// c //~ ERROR doc comment not used by rustdoc
2325
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2426

2527
error: doc comment not used by rustdoc
26-
--> $DIR/useless_comment.rs:14:5
28+
--> $DIR/useless_comment.rs:16:5
2729
|
2830
LL | /// foo //~ ERROR doc comment not used by rustdoc
2931
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3032

31-
error: aborting due to 4 previous errors
33+
error: doc comment not used by rustdoc
34+
--> $DIR/useless_comment.rs:19:5
35+
|
36+
LL | #[doc = "foo"] //~ ERROR doc comment not used by rustdoc
37+
| ^^^^^^^^^^^^^^
38+
39+
error: doc comment not used by rustdoc
40+
--> $DIR/useless_comment.rs:20:5
41+
|
42+
LL | #[doc = "bar"] //~ ERROR doc comment not used by rustdoc
43+
| ^^^^^^^^^^^^^^
44+
45+
error: aborting due to 6 previous errors
3246

0 commit comments

Comments
 (0)