Skip to content

Commit 018d4d2

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 2a65cbe commit 018d4d2

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
@@ -802,27 +802,50 @@ impl LintPass for UnusedDocComment {
802802
}
803803

804804
impl UnusedDocComment {
805-
fn warn_if_doc<'a, 'tcx,
806-
I: Iterator<Item=&'a ast::Attribute>,
807-
C: LintContext<'tcx>>(&self, mut attrs: I, cx: &C) {
808-
if let Some(attr) = attrs.find(|a| a.is_value_str() && a.check_name("doc")) {
809-
cx.struct_span_lint(UNUSED_DOC_COMMENTS, attr.span, "doc comment not used by rustdoc")
810-
.emit();
805+
fn warn_if_doc(&self, cx: &EarlyContext, attrs: &[ast::Attribute]) {
806+
let mut attrs = attrs.into_iter().peekable();
807+
808+
// Accumulate a single span for sugared doc comments.
809+
let mut sugared_span: Option<Span> = None;
810+
811+
while let Some(attr) = attrs.next() {
812+
if attr.is_sugared_doc {
813+
sugared_span = Some(
814+
sugared_span.map_or_else(
815+
|| attr.span,
816+
|span| span.with_hi(attr.span.hi()),
817+
),
818+
);
819+
}
820+
821+
if attrs.peek().map(|next_attr| next_attr.is_sugared_doc).unwrap_or_default() {
822+
continue;
823+
}
824+
825+
let span = sugared_span.take().unwrap_or_else(|| attr.span);
826+
827+
if attr.name() == "doc" {
828+
cx.struct_span_lint(
829+
UNUSED_DOC_COMMENTS,
830+
span,
831+
"doc comment not used by rustdoc",
832+
).emit();
833+
}
811834
}
812835
}
813836
}
814837

815838
impl EarlyLintPass for UnusedDocComment {
816839
fn check_local(&mut self, cx: &EarlyContext<'_>, decl: &ast::Local) {
817-
self.warn_if_doc(decl.attrs.iter(), cx);
840+
self.warn_if_doc(cx, &decl.attrs);
818841
}
819842

820843
fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &ast::Arm) {
821-
self.warn_if_doc(arm.attrs.iter(), cx);
844+
self.warn_if_doc(cx, &arm.attrs);
822845
}
823846

824847
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
825-
self.warn_if_doc(expr.attrs.iter(), cx);
848+
self.warn_if_doc(cx, &expr.attrs);
826849
}
827850
}
828851

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)