Skip to content

Commit

Permalink
Auto merge of #9772 - lukas-code:doc-spans, r=Manishearth
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Nov 2, 2022
2 parents 7600535 + d35b7de commit 647644b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 103 deletions.
29 changes: 7 additions & 22 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_ast::token::CommentKind;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc;
use rustc_errors::emitter::EmitterWriter;
use rustc_errors::{Applicability, Handler, MultiSpan, SuggestionStyle};
use rustc_errors::{Applicability, Handler, SuggestionStyle};
use rustc_hir as hir;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{AnonConst, Expr};
Expand Down Expand Up @@ -265,15 +265,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
panic_span: None,
};
fpu.visit_expr(body.value);
lint_for_missing_headers(
cx,
item.def_id.def_id,
item.span,
sig,
headers,
Some(body_id),
fpu.panic_span,
);
lint_for_missing_headers(cx, item.def_id.def_id, sig, headers, Some(body_id), fpu.panic_span);
}
},
hir::ItemKind::Impl(impl_) => {
Expand All @@ -284,7 +276,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
span_lint(
cx,
MISSING_SAFETY_DOC,
item.span,
cx.tcx.def_span(item.def_id),
"docs for unsafe trait missing `# Safety` section",
);
}
Expand All @@ -304,7 +296,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
let headers = check_attrs(cx, &self.valid_idents, attrs);
if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind {
if !in_external_macro(cx.tcx.sess, item.span) {
lint_for_missing_headers(cx, item.def_id.def_id, item.span, sig, headers, None, None);
lint_for_missing_headers(cx, item.def_id.def_id, sig, headers, None, None);
}
}
}
Expand All @@ -323,23 +315,14 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
panic_span: None,
};
fpu.visit_expr(body.value);
lint_for_missing_headers(
cx,
item.def_id.def_id,
item.span,
sig,
headers,
Some(body_id),
fpu.panic_span,
);
lint_for_missing_headers(cx, item.def_id.def_id, sig, headers, Some(body_id), fpu.panic_span);
}
}
}

fn lint_for_missing_headers(
cx: &LateContext<'_>,
def_id: LocalDefId,
span: impl Into<MultiSpan> + Copy,
sig: &hir::FnSig<'_>,
headers: DocHeaders,
body_id: Option<hir::BodyId>,
Expand All @@ -359,6 +342,8 @@ fn lint_for_missing_headers(
return;
}

let span = cx.tcx.def_span(def_id);

if !headers.safety && sig.header.unsafety == hir::Unsafety::Unsafe {
span_lint(
cx,
Expand Down
36 changes: 12 additions & 24 deletions tests/ui/doc_errors.stderr
Original file line number Diff line number Diff line change
@@ -1,52 +1,40 @@
error: docs for function returning `Result` missing `# Errors` section
--> $DIR/doc_errors.rs:7:1
|
LL | / pub fn pub_fn_missing_errors_header() -> Result<(), ()> {
LL | | unimplemented!();
LL | | }
| |_^
LL | pub fn pub_fn_missing_errors_header() -> Result<(), ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::missing-errors-doc` implied by `-D warnings`

error: docs for function returning `Result` missing `# Errors` section
--> $DIR/doc_errors.rs:11:1
|
LL | / pub async fn async_pub_fn_missing_errors_header() -> Result<(), ()> {
LL | | unimplemented!();
LL | | }
| |_^
LL | pub async fn async_pub_fn_missing_errors_header() -> Result<(), ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: docs for function returning `Result` missing `# Errors` section
--> $DIR/doc_errors.rs:16:1
|
LL | / pub fn pub_fn_returning_io_result() -> io::Result<()> {
LL | | unimplemented!();
LL | | }
| |_^
LL | pub fn pub_fn_returning_io_result() -> io::Result<()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: docs for function returning `Result` missing `# Errors` section
--> $DIR/doc_errors.rs:21:1
|
LL | / pub async fn async_pub_fn_returning_io_result() -> io::Result<()> {
LL | | unimplemented!();
LL | | }
| |_^
LL | pub async fn async_pub_fn_returning_io_result() -> io::Result<()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: docs for function returning `Result` missing `# Errors` section
--> $DIR/doc_errors.rs:51:5
|
LL | / pub fn pub_method_missing_errors_header() -> Result<(), ()> {
LL | | unimplemented!();
LL | | }
| |_____^
LL | pub fn pub_method_missing_errors_header() -> Result<(), ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: docs for function returning `Result` missing `# Errors` section
--> $DIR/doc_errors.rs:56:5
|
LL | / pub async fn async_pub_method_missing_errors_header() -> Result<(), ()> {
LL | | unimplemented!();
LL | | }
| |_____^
LL | pub async fn async_pub_method_missing_errors_header() -> Result<(), ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: docs for function returning `Result` missing `# Errors` section
--> $DIR/doc_errors.rs:85:5
Expand Down
34 changes: 12 additions & 22 deletions tests/ui/doc_unsafe.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
error: unsafe function's docs miss `# Safety` section
--> $DIR/doc_unsafe.rs:9:1
|
LL | / pub unsafe fn destroy_the_planet() {
LL | | unimplemented!();
LL | | }
| |_^
LL | pub unsafe fn destroy_the_planet() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::missing-safety-doc` implied by `-D warnings`

error: unsafe function's docs miss `# Safety` section
--> $DIR/doc_unsafe.rs:32:5
|
LL | / pub unsafe fn republished() {
LL | | unimplemented!();
LL | | }
| |_____^
LL | pub unsafe fn republished() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

error: unsafe function's docs miss `# Safety` section
--> $DIR/doc_unsafe.rs:76:5
|
LL | / pub unsafe fn more_undocumented_unsafe() -> Self {
LL | | unimplemented!();
LL | | }
| |_____^
LL | pub unsafe fn more_undocumented_unsafe() -> Self {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: unsafe function's docs miss `# Safety` section
--> $DIR/doc_unsafe.rs:92:9
|
LL | / pub unsafe fn whee() {
LL | | unimplemented!()
LL | | }
| |_________^
LL | pub unsafe fn whee() {
| ^^^^^^^^^^^^^^^^^^^^
...
LL | very_unsafe!();
| -------------- in this macro invocation
LL | very_unsafe!();
| -------------- in this macro invocation
|
= note: this error originates in the macro `very_unsafe` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
49 changes: 14 additions & 35 deletions tests/ui/missing_panics_doc.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:6:1
|
LL | / pub fn unwrap() {
LL | | let result = Err("Hi");
LL | | result.unwrap()
LL | | }
| |_^
LL | pub fn unwrap() {
| ^^^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:8:5
Expand All @@ -17,10 +14,8 @@ LL | result.unwrap()
error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:12:1
|
LL | / pub fn panic() {
LL | | panic!("This function panics")
LL | | }
| |_^
LL | pub fn panic() {
| ^^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:13:5
Expand All @@ -31,10 +26,8 @@ LL | panic!("This function panics")
error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:17:1
|
LL | / pub fn todo() {
LL | | todo!()
LL | | }
| |_^
LL | pub fn todo() {
| ^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:18:5
Expand All @@ -45,14 +38,8 @@ LL | todo!()
error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:22:1
|
LL | / pub fn inner_body(opt: Option<u32>) {
LL | | opt.map(|x| {
LL | | if x == 10 {
LL | | panic!()
LL | | }
LL | | });
LL | | }
| |_^
LL | pub fn inner_body(opt: Option<u32>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:25:13
Expand All @@ -63,10 +50,8 @@ LL | panic!()
error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:31:1
|
LL | / pub fn unreachable_and_panic() {
LL | | if true { unreachable!() } else { panic!() }
LL | | }
| |_^
LL | pub fn unreachable_and_panic() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:32:39
Expand All @@ -77,11 +62,8 @@ LL | if true { unreachable!() } else { panic!() }
error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:36:1
|
LL | / pub fn assert_eq() {
LL | | let x = 0;
LL | | assert_eq!(x, 0);
LL | | }
| |_^
LL | pub fn assert_eq() {
| ^^^^^^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:38:5
Expand All @@ -92,11 +74,8 @@ LL | assert_eq!(x, 0);
error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:42:1
|
LL | / pub fn assert_ne() {
LL | | let x = 0;
LL | | assert_ne!(x, 0);
LL | | }
| |_^
LL | pub fn assert_ne() {
| ^^^^^^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:44:5
Expand Down

0 comments on commit 647644b

Please sign in to comment.