Skip to content

Commit 647644b

Browse files
committed
Auto merge of #9772 - lukas-code:doc-spans, r=Manishearth
Shrink `missing_{safety,errors,panics}_doc` spans Shrink the spans of `clippy::missing_*_doc` to match `missing_docs` and don't cover the entire item anymore. This helps readability in IDEs by not coloring the entire screen yellow, similar to rust-lang/rust#103749 and rust-lang/rust#90761. before: ![image](https://user-images.githubusercontent.com/26522220/199483288-af0cf0c2-a9e4-47a8-81e3-50ccf380d939.png) after: ![image](https://user-images.githubusercontent.com/26522220/199483366-445e5ddd-9f71-4de1-85be-43461c9b7d5d.png) changelog: [`missing_safety_doc`], [`missing_error_doc`], [`missing_panics_doc`]: These lints no longer span the entire item.
2 parents 7600535 + d35b7de commit 647644b

File tree

4 files changed

+45
-103
lines changed

4 files changed

+45
-103
lines changed

clippy_lints/src/doc.rs

+7-22
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_ast::token::CommentKind;
1111
use rustc_data_structures::fx::FxHashSet;
1212
use rustc_data_structures::sync::Lrc;
1313
use rustc_errors::emitter::EmitterWriter;
14-
use rustc_errors::{Applicability, Handler, MultiSpan, SuggestionStyle};
14+
use rustc_errors::{Applicability, Handler, SuggestionStyle};
1515
use rustc_hir as hir;
1616
use rustc_hir::intravisit::{self, Visitor};
1717
use rustc_hir::{AnonConst, Expr};
@@ -265,15 +265,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
265265
panic_span: None,
266266
};
267267
fpu.visit_expr(body.value);
268-
lint_for_missing_headers(
269-
cx,
270-
item.def_id.def_id,
271-
item.span,
272-
sig,
273-
headers,
274-
Some(body_id),
275-
fpu.panic_span,
276-
);
268+
lint_for_missing_headers(cx, item.def_id.def_id, sig, headers, Some(body_id), fpu.panic_span);
277269
}
278270
},
279271
hir::ItemKind::Impl(impl_) => {
@@ -284,7 +276,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
284276
span_lint(
285277
cx,
286278
MISSING_SAFETY_DOC,
287-
item.span,
279+
cx.tcx.def_span(item.def_id),
288280
"docs for unsafe trait missing `# Safety` section",
289281
);
290282
}
@@ -304,7 +296,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
304296
let headers = check_attrs(cx, &self.valid_idents, attrs);
305297
if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind {
306298
if !in_external_macro(cx.tcx.sess, item.span) {
307-
lint_for_missing_headers(cx, item.def_id.def_id, item.span, sig, headers, None, None);
299+
lint_for_missing_headers(cx, item.def_id.def_id, sig, headers, None, None);
308300
}
309301
}
310302
}
@@ -323,23 +315,14 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
323315
panic_span: None,
324316
};
325317
fpu.visit_expr(body.value);
326-
lint_for_missing_headers(
327-
cx,
328-
item.def_id.def_id,
329-
item.span,
330-
sig,
331-
headers,
332-
Some(body_id),
333-
fpu.panic_span,
334-
);
318+
lint_for_missing_headers(cx, item.def_id.def_id, sig, headers, Some(body_id), fpu.panic_span);
335319
}
336320
}
337321
}
338322

339323
fn lint_for_missing_headers(
340324
cx: &LateContext<'_>,
341325
def_id: LocalDefId,
342-
span: impl Into<MultiSpan> + Copy,
343326
sig: &hir::FnSig<'_>,
344327
headers: DocHeaders,
345328
body_id: Option<hir::BodyId>,
@@ -359,6 +342,8 @@ fn lint_for_missing_headers(
359342
return;
360343
}
361344

345+
let span = cx.tcx.def_span(def_id);
346+
362347
if !headers.safety && sig.header.unsafety == hir::Unsafety::Unsafe {
363348
span_lint(
364349
cx,

tests/ui/doc_errors.stderr

+12-24
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,40 @@
11
error: docs for function returning `Result` missing `# Errors` section
22
--> $DIR/doc_errors.rs:7:1
33
|
4-
LL | / pub fn pub_fn_missing_errors_header() -> Result<(), ()> {
5-
LL | | unimplemented!();
6-
LL | | }
7-
| |_^
4+
LL | pub fn pub_fn_missing_errors_header() -> Result<(), ()> {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86
|
97
= note: `-D clippy::missing-errors-doc` implied by `-D warnings`
108

119
error: docs for function returning `Result` missing `# Errors` section
1210
--> $DIR/doc_errors.rs:11:1
1311
|
14-
LL | / pub async fn async_pub_fn_missing_errors_header() -> Result<(), ()> {
15-
LL | | unimplemented!();
16-
LL | | }
17-
| |_^
12+
LL | pub async fn async_pub_fn_missing_errors_header() -> Result<(), ()> {
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1814

1915
error: docs for function returning `Result` missing `# Errors` section
2016
--> $DIR/doc_errors.rs:16:1
2117
|
22-
LL | / pub fn pub_fn_returning_io_result() -> io::Result<()> {
23-
LL | | unimplemented!();
24-
LL | | }
25-
| |_^
18+
LL | pub fn pub_fn_returning_io_result() -> io::Result<()> {
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2620

2721
error: docs for function returning `Result` missing `# Errors` section
2822
--> $DIR/doc_errors.rs:21:1
2923
|
30-
LL | / pub async fn async_pub_fn_returning_io_result() -> io::Result<()> {
31-
LL | | unimplemented!();
32-
LL | | }
33-
| |_^
24+
LL | pub async fn async_pub_fn_returning_io_result() -> io::Result<()> {
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3426

3527
error: docs for function returning `Result` missing `# Errors` section
3628
--> $DIR/doc_errors.rs:51:5
3729
|
38-
LL | / pub fn pub_method_missing_errors_header() -> Result<(), ()> {
39-
LL | | unimplemented!();
40-
LL | | }
41-
| |_____^
30+
LL | pub fn pub_method_missing_errors_header() -> Result<(), ()> {
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4232

4333
error: docs for function returning `Result` missing `# Errors` section
4434
--> $DIR/doc_errors.rs:56:5
4535
|
46-
LL | / pub async fn async_pub_method_missing_errors_header() -> Result<(), ()> {
47-
LL | | unimplemented!();
48-
LL | | }
49-
| |_____^
36+
LL | pub async fn async_pub_method_missing_errors_header() -> Result<(), ()> {
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5038

5139
error: docs for function returning `Result` missing `# Errors` section
5240
--> $DIR/doc_errors.rs:85:5

tests/ui/doc_unsafe.stderr

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
error: unsafe function's docs miss `# Safety` section
22
--> $DIR/doc_unsafe.rs:9:1
33
|
4-
LL | / pub unsafe fn destroy_the_planet() {
5-
LL | | unimplemented!();
6-
LL | | }
7-
| |_^
4+
LL | pub unsafe fn destroy_the_planet() {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86
|
97
= note: `-D clippy::missing-safety-doc` implied by `-D warnings`
108

119
error: unsafe function's docs miss `# Safety` section
1210
--> $DIR/doc_unsafe.rs:32:5
1311
|
14-
LL | / pub unsafe fn republished() {
15-
LL | | unimplemented!();
16-
LL | | }
17-
| |_____^
12+
LL | pub unsafe fn republished() {
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1814

1915
error: unsafe function's docs miss `# Safety` section
2016
--> $DIR/doc_unsafe.rs:40:5
@@ -25,29 +21,23 @@ LL | unsafe fn woefully_underdocumented(self);
2521
error: docs for unsafe trait missing `# Safety` section
2622
--> $DIR/doc_unsafe.rs:46:1
2723
|
28-
LL | / pub unsafe trait UnsafeTrait {
29-
LL | | fn method();
30-
LL | | }
31-
| |_^
24+
LL | pub unsafe trait UnsafeTrait {
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3226

3327
error: unsafe function's docs miss `# Safety` section
3428
--> $DIR/doc_unsafe.rs:76:5
3529
|
36-
LL | / pub unsafe fn more_undocumented_unsafe() -> Self {
37-
LL | | unimplemented!();
38-
LL | | }
39-
| |_____^
30+
LL | pub unsafe fn more_undocumented_unsafe() -> Self {
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4032

4133
error: unsafe function's docs miss `# Safety` section
4234
--> $DIR/doc_unsafe.rs:92:9
4335
|
44-
LL | / pub unsafe fn whee() {
45-
LL | | unimplemented!()
46-
LL | | }
47-
| |_________^
36+
LL | pub unsafe fn whee() {
37+
| ^^^^^^^^^^^^^^^^^^^^
4838
...
49-
LL | very_unsafe!();
50-
| -------------- in this macro invocation
39+
LL | very_unsafe!();
40+
| -------------- in this macro invocation
5141
|
5242
= note: this error originates in the macro `very_unsafe` (in Nightly builds, run with -Z macro-backtrace for more info)
5343

tests/ui/missing_panics_doc.stderr

+14-35
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
error: docs for function which may panic missing `# Panics` section
22
--> $DIR/missing_panics_doc.rs:6:1
33
|
4-
LL | / pub fn unwrap() {
5-
LL | | let result = Err("Hi");
6-
LL | | result.unwrap()
7-
LL | | }
8-
| |_^
4+
LL | pub fn unwrap() {
5+
| ^^^^^^^^^^^^^^^
96
|
107
note: first possible panic found here
118
--> $DIR/missing_panics_doc.rs:8:5
@@ -17,10 +14,8 @@ LL | result.unwrap()
1714
error: docs for function which may panic missing `# Panics` section
1815
--> $DIR/missing_panics_doc.rs:12:1
1916
|
20-
LL | / pub fn panic() {
21-
LL | | panic!("This function panics")
22-
LL | | }
23-
| |_^
17+
LL | pub fn panic() {
18+
| ^^^^^^^^^^^^^^
2419
|
2520
note: first possible panic found here
2621
--> $DIR/missing_panics_doc.rs:13:5
@@ -31,10 +26,8 @@ LL | panic!("This function panics")
3126
error: docs for function which may panic missing `# Panics` section
3227
--> $DIR/missing_panics_doc.rs:17:1
3328
|
34-
LL | / pub fn todo() {
35-
LL | | todo!()
36-
LL | | }
37-
| |_^
29+
LL | pub fn todo() {
30+
| ^^^^^^^^^^^^^
3831
|
3932
note: first possible panic found here
4033
--> $DIR/missing_panics_doc.rs:18:5
@@ -45,14 +38,8 @@ LL | todo!()
4538
error: docs for function which may panic missing `# Panics` section
4639
--> $DIR/missing_panics_doc.rs:22:1
4740
|
48-
LL | / pub fn inner_body(opt: Option<u32>) {
49-
LL | | opt.map(|x| {
50-
LL | | if x == 10 {
51-
LL | | panic!()
52-
LL | | }
53-
LL | | });
54-
LL | | }
55-
| |_^
41+
LL | pub fn inner_body(opt: Option<u32>) {
42+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5643
|
5744
note: first possible panic found here
5845
--> $DIR/missing_panics_doc.rs:25:13
@@ -63,10 +50,8 @@ LL | panic!()
6350
error: docs for function which may panic missing `# Panics` section
6451
--> $DIR/missing_panics_doc.rs:31:1
6552
|
66-
LL | / pub fn unreachable_and_panic() {
67-
LL | | if true { unreachable!() } else { panic!() }
68-
LL | | }
69-
| |_^
53+
LL | pub fn unreachable_and_panic() {
54+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7055
|
7156
note: first possible panic found here
7257
--> $DIR/missing_panics_doc.rs:32:39
@@ -77,11 +62,8 @@ LL | if true { unreachable!() } else { panic!() }
7762
error: docs for function which may panic missing `# Panics` section
7863
--> $DIR/missing_panics_doc.rs:36:1
7964
|
80-
LL | / pub fn assert_eq() {
81-
LL | | let x = 0;
82-
LL | | assert_eq!(x, 0);
83-
LL | | }
84-
| |_^
65+
LL | pub fn assert_eq() {
66+
| ^^^^^^^^^^^^^^^^^^
8567
|
8668
note: first possible panic found here
8769
--> $DIR/missing_panics_doc.rs:38:5
@@ -92,11 +74,8 @@ LL | assert_eq!(x, 0);
9274
error: docs for function which may panic missing `# Panics` section
9375
--> $DIR/missing_panics_doc.rs:42:1
9476
|
95-
LL | / pub fn assert_ne() {
96-
LL | | let x = 0;
97-
LL | | assert_ne!(x, 0);
98-
LL | | }
99-
| |_^
77+
LL | pub fn assert_ne() {
78+
| ^^^^^^^^^^^^^^^^^^
10079
|
10180
note: first possible panic found here
10281
--> $DIR/missing_panics_doc.rs:44:5

0 commit comments

Comments
 (0)