Skip to content

Commit

Permalink
Auto merge of rust-lang#7029 - ABouttefeux:master, r=Manishearth
Browse files Browse the repository at this point in the history
fix `missing_panics_doc` not detecting `assert_eq!` and `assert_ne!`

fixes rust-lang#6997
changelog: `missing_panics_doc` detects `assert_eq!` and `assert_ne!`

---
searching for `assert_eq!` and `assert_ne!` in `FindPanicUnwrap`
  • Loading branch information
bors committed Apr 5, 2021
2 parents d91da40 + 8e5dd4b commit 25c1ed3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
5 changes: 5 additions & 0 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,11 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
}
}

// check for `assert_eq` or `assert_ne`
if is_expn_of(expr.span, "assert_eq").is_some() || is_expn_of(expr.span, "assert_ne").is_some() {
self.panic_span = Some(expr.span);
}

// check for `unwrap`
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
Expand Down
32 changes: 32 additions & 0 deletions tests/ui/missing_panics_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ pub fn unreachable_and_panic() {
if true { unreachable!() } else { panic!() }
}

/// This needs to be documented
pub fn assert_eq() {
let x = 0;
assert_eq!(x, 0);
}

/// This needs to be documented
pub fn assert_ne() {
let x = 0;
assert_ne!(x, 0);
}

/// This is documented
///
/// # Panics
Expand Down Expand Up @@ -83,6 +95,26 @@ pub fn unreachable_amd_panic_documented() {
if true { unreachable!() } else { panic!() }
}

/// This is documented
///
/// # Panics
///
/// Panics if `x` is not 0.
pub fn assert_eq_documented() {
let x = 0;
assert_eq!(x, 0);
}

/// This is documented
///
/// # Panics
///
/// Panics if `x` is 0.
pub fn assert_ne_documented() {
let x = 0;
assert_ne!(x, 0);
}

/// This is okay because it is private
fn unwrap_private() {
let result = Err("Hi");
Expand Down
34 changes: 33 additions & 1 deletion tests/ui/missing_panics_doc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,37 @@ LL | if true { unreachable!() } else { panic!() }
| ^^^^^^^^
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 5 previous errors
error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:37:1
|
LL | / pub fn assert_eq() {
LL | | let x = 0;
LL | | assert_eq!(x, 0);
LL | | }
| |_^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:39:5
|
LL | assert_eq!(x, 0);
| ^^^^^^^^^^^^^^^^^
= 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/missing_panics_doc.rs:43:1
|
LL | / pub fn assert_ne() {
LL | | let x = 0;
LL | | assert_ne!(x, 0);
LL | | }
| |_^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:45:5
|
LL | assert_ne!(x, 0);
| ^^^^^^^^^^^^^^^^^
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 7 previous errors

0 comments on commit 25c1ed3

Please sign in to comment.