Skip to content

Commit d339468

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 74e35d2 commit d339468

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
@@ -799,27 +799,50 @@ impl LintPass for UnusedDocComment {
799799
}
800800

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

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

817840
fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &ast::Arm) {
818-
self.warn_if_doc(arm.attrs.iter(), cx);
841+
self.warn_if_doc(cx, &arm.attrs);
819842
}
820843

821844
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
822-
self.warn_if_doc(expr.attrs.iter(), cx);
845+
self.warn_if_doc(cx, &expr.attrs);
823846
}
824847
}
825848

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)