Skip to content

Commit

Permalink
Auto merge of #6996 - Y-Nak:missing_panics_doc, r=Manishearth
Browse files Browse the repository at this point in the history
Allow missing panics doc if the panics occur only when debug-assertions is specified

fixes #6970

changelog: `missing_panics_doc`: Allow missing panics doc if the panics occur only when `debug-assertions` is specified.
  • Loading branch information
bors committed Mar 29, 2021
2 parents c07103b + 31afdfc commit 0e06b3c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
6 changes: 6 additions & 0 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
if let Some(path_def_id) = path.res.opt_def_id();
if match_panic_def_id(self.cx, path_def_id);
if is_expn_of(expr.span, "unreachable").is_none();
if !is_expn_of_debug_assertions(expr.span);
then {
self.panic_span = Some(expr.span);
}
Expand All @@ -738,3 +739,8 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
}
}

fn is_expn_of_debug_assertions(span: Span) -> bool {
const MACRO_NAMES: &[&str] = &["debug_assert", "debug_assert_eq", "debug_assert_ne"];
MACRO_NAMES.iter().any(|name| is_expn_of(span, name).is_some())
}
8 changes: 8 additions & 0 deletions tests/ui/doc_panics.rs → tests/ui/missing_panics_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,11 @@ fn inner_body_private(opt: Option<u32>) {
pub fn unreachable() {
unreachable!("This function panics")
}

/// #6970.
/// This is okay because it is expansion of `debug_assert` family.
pub fn debug_assertions() {
debug_assert!(false);
debug_assert_eq!(1, 2);
debug_assert_ne!(1, 2);
}
20 changes: 10 additions & 10 deletions tests/ui/doc_panics.stderr → tests/ui/missing_panics_doc.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: docs for function which may panic missing `# Panics` section
--> $DIR/doc_panics.rs:7:1
--> $DIR/missing_panics_doc.rs:7:1
|
LL | / pub fn unwrap() {
LL | | let result = Err("Hi");
Expand All @@ -9,43 +9,43 @@ LL | | }
|
= note: `-D clippy::missing-panics-doc` implied by `-D warnings`
note: first possible panic found here
--> $DIR/doc_panics.rs:9:5
--> $DIR/missing_panics_doc.rs:9:5
|
LL | result.unwrap()
| ^^^^^^^^^^^^^^^

error: docs for function which may panic missing `# Panics` section
--> $DIR/doc_panics.rs:13:1
--> $DIR/missing_panics_doc.rs:13:1
|
LL | / pub fn panic() {
LL | | panic!("This function panics")
LL | | }
| |_^
|
note: first possible panic found here
--> $DIR/doc_panics.rs:14:5
--> $DIR/missing_panics_doc.rs:14:5
|
LL | panic!("This function panics")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: docs for function which may panic missing `# Panics` section
--> $DIR/doc_panics.rs:18:1
--> $DIR/missing_panics_doc.rs:18:1
|
LL | / pub fn todo() {
LL | | todo!()
LL | | }
| |_^
|
note: first possible panic found here
--> $DIR/doc_panics.rs:19:5
--> $DIR/missing_panics_doc.rs:19:5
|
LL | todo!()
| ^^^^^^^
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: docs for function which may panic missing `# Panics` section
--> $DIR/doc_panics.rs:23:1
--> $DIR/missing_panics_doc.rs:23:1
|
LL | / pub fn inner_body(opt: Option<u32>) {
LL | | opt.map(|x| {
Expand All @@ -57,22 +57,22 @@ LL | | }
| |_^
|
note: first possible panic found here
--> $DIR/doc_panics.rs:26:13
--> $DIR/missing_panics_doc.rs:26:13
|
LL | panic!()
| ^^^^^^^^
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: docs for function which may panic missing `# Panics` section
--> $DIR/doc_panics.rs:32:1
--> $DIR/missing_panics_doc.rs:32:1
|
LL | / pub fn unreachable_and_panic() {
LL | | if true { unreachable!() } else { panic!() }
LL | | }
| |_^
|
note: first possible panic found here
--> $DIR/doc_panics.rs:33:39
--> $DIR/missing_panics_doc.rs:33:39
|
LL | if true { unreachable!() } else { panic!() }
| ^^^^^^^^
Expand Down

0 comments on commit 0e06b3c

Please sign in to comment.