Skip to content

Commit 3db7fc1

Browse files
Rollup merge of rust-lang#115478 - gurry:115462-exprfield-no-warn, r=compiler-errors
Emit unused doc comment warnings for pat and expr fields Fixes rust-lang#115462
2 parents 53a0397 + a0a7173 commit 3db7fc1

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

compiler/rustc_lint/src/builtin.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1001,8 +1001,22 @@ impl EarlyLintPass for UnusedDocComment {
10011001
warn_if_doc(cx, arm_span, "match arms", &arm.attrs);
10021002
}
10031003

1004+
fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &ast::Pat) {
1005+
if let ast::PatKind::Struct(_, _, fields, _) = &pat.kind {
1006+
for field in fields {
1007+
warn_if_doc(cx, field.span, "pattern fields", &field.attrs);
1008+
}
1009+
}
1010+
}
1011+
10041012
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
10051013
warn_if_doc(cx, expr.span, "expressions", &expr.attrs);
1014+
1015+
if let ExprKind::Struct(s) = &expr.kind {
1016+
for field in &s.fields {
1017+
warn_if_doc(cx, field.span, "expression fields", &field.attrs);
1018+
}
1019+
}
10061020
}
10071021

10081022
fn check_generic_param(&mut self, cx: &EarlyContext<'_>, param: &ast::GenericParam) {

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
555555
subpattern: pattern,
556556
ascription: Ascription {
557557
annotation,
558-
/// Note that use `Contravariant` here. See the
559-
/// `variance` field documentation for details.
558+
// Note that use `Contravariant` here. See the
559+
// `variance` field documentation for details.
560560
variance: ty::Variance::Contravariant,
561561
},
562562
},

tests/ui/lint/unused/unused-doc-comments-edge-cases.rs

+26
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,32 @@ fn doc_comment_on_expr(num: u8) -> bool {
2626
num == 3
2727
}
2828

29+
fn doc_comment_on_expr_field() -> bool {
30+
struct S { foo: i32 }
31+
32+
let x = S {
33+
/// useless doc comment
34+
//~^ ERROR: unused doc comment
35+
foo: 3
36+
};
37+
38+
true
39+
}
40+
41+
fn doc_comment_on_pat_field() -> bool {
42+
struct S { foo: i32 }
43+
44+
let S {
45+
/// useless doc comment
46+
//~^ ERROR: unused doc comment
47+
foo
48+
} = S {
49+
foo: 3
50+
};
51+
52+
true
53+
}
54+
2955
fn doc_comment_on_generic<#[doc = "x"] T>(val: T) {}
3056
//~^ ERROR: unused doc comment
3157

tests/ui/lint/unused/unused-doc-comments-edge-cases.stderr

+26-4
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,37 @@ LL | num == 3
4242
= help: use `//` for a plain comment
4343

4444
error: unused doc comment
45-
--> $DIR/unused-doc-comments-edge-cases.rs:29:27
45+
--> $DIR/unused-doc-comments-edge-cases.rs:33:9
46+
|
47+
LL | /// useless doc comment
48+
| ^^^^^^^^^^^^^^^^^^^^^^^
49+
LL |
50+
LL | foo: 3
51+
| ------ rustdoc does not generate documentation for expression fields
52+
|
53+
= help: use `//` for a plain comment
54+
55+
error: unused doc comment
56+
--> $DIR/unused-doc-comments-edge-cases.rs:45:9
57+
|
58+
LL | /// useless doc comment
59+
| ^^^^^^^^^^^^^^^^^^^^^^^
60+
LL |
61+
LL | foo
62+
| --- rustdoc does not generate documentation for pattern fields
63+
|
64+
= help: use `//` for a plain comment
65+
66+
error: unused doc comment
67+
--> $DIR/unused-doc-comments-edge-cases.rs:55:27
4668
|
4769
LL | fn doc_comment_on_generic<#[doc = "x"] T>(val: T) {}
4870
| ^^^^^^^^^^^^ - rustdoc does not generate documentation for generic parameters
4971
|
5072
= help: use `//` for a plain comment
5173

5274
error: unused doc comment
53-
--> $DIR/unused-doc-comments-edge-cases.rs:33:5
75+
--> $DIR/unused-doc-comments-edge-cases.rs:59:5
5476
|
5577
LL | /// unused doc comment
5678
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -63,7 +85,7 @@ LL | | }
6385
= help: use `//` for a plain comment
6486

6587
error: unused doc comment
66-
--> $DIR/unused-doc-comments-edge-cases.rs:40:1
88+
--> $DIR/unused-doc-comments-edge-cases.rs:66:1
6789
|
6890
LL | /// unused doc comment
6991
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -89,7 +111,7 @@ help: you might have meant to return this value
89111
LL | return true;
90112
| ++++++ +
91113

92-
error: aborting due to 8 previous errors
114+
error: aborting due to 10 previous errors
93115

94116
Some errors have detailed explanations: E0308, E0658.
95117
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)