Skip to content

Commit 479d4f8

Browse files
committed
Auto merge of #7060 - daxpedda:debug-assert-panic-in-result-fn, r=flip1995
Remove `debug_assert` from `panic_in_result_fn` I couldn't find any documentation on `debug_assert` that should be remove. In my humble opinion, I would also like to argue that `todo` and `unreachable` shouldn't trigger this lint? Related: #6082 r? `@flip1995` changelog: Change `panic_in_result_fn` to ignore `debug_assert` and co macros
2 parents f7c2c44 + 271c163 commit 479d4f8

File tree

3 files changed

+12
-76
lines changed

3 files changed

+12
-76
lines changed

clippy_lints/src/panic_in_result_fn.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::is_type_diagnostic_item;
3-
use clippy_utils::{find_macro_calls, return_ty};
3+
use clippy_utils::{find_macro_calls, is_expn_of, return_ty};
44
use rustc_hir as hir;
55
use rustc_hir::intravisit::FnKind;
66
use rustc_lint::{LateContext, LateLintPass};
@@ -52,7 +52,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicInResultFn {
5252
}
5353

5454
fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, body: &'tcx hir::Body<'tcx>) {
55-
let panics = find_macro_calls(
55+
let mut panics = find_macro_calls(
5656
&[
5757
"unimplemented",
5858
"unreachable",
@@ -61,12 +61,10 @@ fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, body: &'tcx hir
6161
"assert",
6262
"assert_eq",
6363
"assert_ne",
64-
"debug_assert",
65-
"debug_assert_eq",
66-
"debug_assert_ne",
6764
],
6865
body,
6966
);
67+
panics.retain(|span| is_expn_of(*span, "debug_assert").is_none());
7068
if !panics.is_empty() {
7169
span_lint_and_then(
7270
cx,

tests/ui/panic_in_result_fn_debug_assertions.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,39 @@
11
#![warn(clippy::panic_in_result_fn)]
22
#![allow(clippy::unnecessary_wraps)]
33

4+
// debug_assert should never trigger the `panic_in_result_fn` lint
5+
46
struct A;
57

68
impl A {
7-
fn result_with_debug_assert_with_message(x: i32) -> Result<bool, String> // should emit lint
8-
{
9+
fn result_with_debug_assert_with_message(x: i32) -> Result<bool, String> {
910
debug_assert!(x == 5, "wrong argument");
1011
Ok(true)
1112
}
1213

13-
fn result_with_debug_assert_eq(x: i32) -> Result<bool, String> // should emit lint
14-
{
14+
fn result_with_debug_assert_eq(x: i32) -> Result<bool, String> {
1515
debug_assert_eq!(x, 5);
1616
Ok(true)
1717
}
1818

19-
fn result_with_debug_assert_ne(x: i32) -> Result<bool, String> // should emit lint
20-
{
19+
fn result_with_debug_assert_ne(x: i32) -> Result<bool, String> {
2120
debug_assert_ne!(x, 1);
2221
Ok(true)
2322
}
2423

25-
fn other_with_debug_assert_with_message(x: i32) // should not emit lint
26-
{
24+
fn other_with_debug_assert_with_message(x: i32) {
2725
debug_assert!(x == 5, "wrong argument");
2826
}
2927

30-
fn other_with_debug_assert_eq(x: i32) // should not emit lint
31-
{
28+
fn other_with_debug_assert_eq(x: i32) {
3229
debug_assert_eq!(x, 5);
3330
}
3431

35-
fn other_with_debug_assert_ne(x: i32) // should not emit lint
36-
{
32+
fn other_with_debug_assert_ne(x: i32) {
3733
debug_assert_ne!(x, 1);
3834
}
3935

40-
fn result_without_banned_functions() -> Result<bool, String> // should not emit lint
41-
{
36+
fn result_without_banned_functions() -> Result<bool, String> {
4237
let debug_assert = "debug_assert!";
4338
println!("No {}", debug_assert);
4439
Ok(true)

tests/ui/panic_in_result_fn_debug_assertions.stderr

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)