Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a424a2c

Browse files
author
Vali Schneider
committed
changed check_impl_item to check_fn and added a few more test cases
1 parent b006522 commit a424a2c

File tree

3 files changed

+99
-50
lines changed

3 files changed

+99
-50
lines changed

clippy_lints/src/panic_in_result.rs

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::utils::{is_expn_of, is_type_diagnostic_item, return_ty, span_lint_and_then};
22
use if_chain::if_chain;
33
use rustc_hir as hir;
4+
use rustc_hir::intravisit::{self, FnKind, NestedVisitorMap, Visitor};
5+
use rustc_hir::Expr;
46
use rustc_lint::{LateContext, LateLintPass};
57
use rustc_middle::hir::map::Map;
68
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -21,7 +23,6 @@ declare_clippy_lint! {
2123
/// panic!("error");
2224
/// }
2325
/// ```
24-
2526
pub PANIC_IN_RESULT,
2627
restriction,
2728
"functions of type `Result<..>` that contain `panic!()`, `todo!()` or `unreachable()` or `unimplemented()` "
@@ -30,22 +31,26 @@ declare_clippy_lint! {
3031
declare_lint_pass!(PanicInResult => [PANIC_IN_RESULT]);
3132

3233
impl<'tcx> LateLintPass<'tcx> for PanicInResult {
33-
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
34+
/*
35+
fn check_fn(
36+
&mut self,
37+
cx: &LateContext<'tcx>,
38+
_: FnKind<'tcx>,
39+
_: &'tcx hir::FnDecl<'tcx>,
40+
body: &'tcx hir::Body<'tcx>,
41+
span: Span,
42+
hir_id: hir::HirId,
43+
) {
3444
if_chain! {
35-
// first check if it's a method or function
36-
if let hir::ImplItemKind::Fn(ref _signature, _) = impl_item.kind;
37-
// checking if its return type is `result` or `option`
38-
if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym!(result_type));
39-
then {
40-
lint_impl_body(cx, impl_item.span, impl_item);
45+
if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(result_type));
46+
then
47+
{
48+
lint_impl_body(cx, span, body);
4149
}
4250
}
43-
}
51+
}*/
4452
}
4553

46-
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
47-
use rustc_hir::{Expr, ImplItemKind};
48-
4954
struct FindPanicUnimplementedUnreachable {
5055
result: Vec<Span>,
5156
}
@@ -70,29 +75,21 @@ impl<'tcx> Visitor<'tcx> for FindPanicUnimplementedUnreachable {
7075
}
7176
}
7277

73-
fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_item: &'tcx hir::ImplItem<'_>) {
74-
if_chain! {
75-
if let ImplItemKind::Fn(_, body_id) = impl_item.kind;
76-
then {
77-
let body = cx.tcx.hir().body(body_id);
78-
let mut fpu = FindPanicUnimplementedUnreachable {
79-
result: Vec::new(),
80-
};
81-
fpu.visit_expr(&body.value);
82-
83-
// if we've found one, lint
84-
if !fpu.result.is_empty() {
85-
span_lint_and_then(
86-
cx,
87-
PANIC_IN_RESULT,
88-
impl_span,
89-
"used unimplemented, unreachable, todo or panic in a function that returns result",
90-
move |diag| {
91-
diag.help(
92-
"unimplemented, unreachable, todo or panic should not be used in a function that returns result" );
93-
diag.span_note(fpu.result, "will cause the application to crash.");
94-
});
95-
}
96-
}
78+
fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, body: &'tcx hir::Body<'tcx>) {
79+
let mut panics = FindPanicUnimplementedUnreachable { result: Vec::new() };
80+
panics.visit_expr(&body.value);
81+
if !panics.result.is_empty() {
82+
span_lint_and_then(
83+
cx,
84+
PANIC_IN_RESULT,
85+
impl_span,
86+
"used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`",
87+
move |diag| {
88+
diag.help(
89+
"`unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing",
90+
);
91+
diag.span_note(panics.result, "return Err() instead of panicking");
92+
},
93+
);
9794
}
9895
}

tests/ui/panic_in_result.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,22 @@ impl A {
4949
}
5050
}
5151

52-
fn main() {}
52+
fn function_result_with_panic() -> Result<bool, String> // should emit lint
53+
{
54+
panic!("error");
55+
}
56+
57+
fn todo() {
58+
println!("something");
59+
}
60+
61+
fn function_result_with_custom_todo() -> Result<bool, String> // should not emit lint
62+
{
63+
todo();
64+
Ok(true)
65+
}
66+
67+
fn main() -> Result<(), String> {
68+
todo!("finish main method");
69+
Ok(())
70+
}

tests/ui/panic_in_result.stderr

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: used unimplemented, unreachable, todo or panic in a function that returns result
1+
error: used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`
22
--> $DIR/panic_in_result.rs:6:5
33
|
44
LL | / fn result_with_panic() -> Result<bool, String> // should emit lint
@@ -8,15 +8,15 @@ LL | | }
88
| |_____^
99
|
1010
= note: `-D clippy::panic-in-result` implied by `-D warnings`
11-
= help: unimplemented, unreachable, todo or panic should not be used in a function that returns result
12-
note: will cause the application to crash.
11+
= help: `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing
12+
note: return Err() instead of panicking
1313
--> $DIR/panic_in_result.rs:8:9
1414
|
1515
LL | panic!("error");
1616
| ^^^^^^^^^^^^^^^^
1717
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1818

19-
error: used unimplemented, unreachable, todo or panic in a function that returns result
19+
error: used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`
2020
--> $DIR/panic_in_result.rs:11:5
2121
|
2222
LL | / fn result_with_unimplemented() -> Result<bool, String> // should emit lint
@@ -25,15 +25,15 @@ LL | | unimplemented!();
2525
LL | | }
2626
| |_____^
2727
|
28-
= help: unimplemented, unreachable, todo or panic should not be used in a function that returns result
29-
note: will cause the application to crash.
28+
= help: `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing
29+
note: return Err() instead of panicking
3030
--> $DIR/panic_in_result.rs:13:9
3131
|
3232
LL | unimplemented!();
3333
| ^^^^^^^^^^^^^^^^^
3434
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
3535

36-
error: used unimplemented, unreachable, todo or panic in a function that returns result
36+
error: used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`
3737
--> $DIR/panic_in_result.rs:16:5
3838
|
3939
LL | / fn result_with_unreachable() -> Result<bool, String> // should emit lint
@@ -42,15 +42,15 @@ LL | | unreachable!();
4242
LL | | }
4343
| |_____^
4444
|
45-
= help: unimplemented, unreachable, todo or panic should not be used in a function that returns result
46-
note: will cause the application to crash.
45+
= help: `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing
46+
note: return Err() instead of panicking
4747
--> $DIR/panic_in_result.rs:18:9
4848
|
4949
LL | unreachable!();
5050
| ^^^^^^^^^^^^^^^
5151
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
5252

53-
error: used unimplemented, unreachable, todo or panic in a function that returns result
53+
error: used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`
5454
--> $DIR/panic_in_result.rs:21:5
5555
|
5656
LL | / fn result_with_todo() -> Result<bool, String> // should emit lint
@@ -59,13 +59,47 @@ LL | | todo!("Finish this");
5959
LL | | }
6060
| |_____^
6161
|
62-
= help: unimplemented, unreachable, todo or panic should not be used in a function that returns result
63-
note: will cause the application to crash.
62+
= help: `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing
63+
note: return Err() instead of panicking
6464
--> $DIR/panic_in_result.rs:23:9
6565
|
6666
LL | todo!("Finish this");
6767
| ^^^^^^^^^^^^^^^^^^^^^
6868
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
6969

70-
error: aborting due to 4 previous errors
70+
error: used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`
71+
--> $DIR/panic_in_result.rs:52:1
72+
|
73+
LL | / fn function_result_with_panic() -> Result<bool, String> // should emit lint
74+
LL | | {
75+
LL | | panic!("error");
76+
LL | | }
77+
| |_^
78+
|
79+
= help: `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing
80+
note: return Err() instead of panicking
81+
--> $DIR/panic_in_result.rs:54:5
82+
|
83+
LL | panic!("error");
84+
| ^^^^^^^^^^^^^^^^
85+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
86+
87+
error: used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`
88+
--> $DIR/panic_in_result.rs:67:1
89+
|
90+
LL | / fn main() -> Result<(), String> {
91+
LL | | todo!("finish main method");
92+
LL | | Ok(())
93+
LL | | }
94+
| |_^
95+
|
96+
= help: `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing
97+
note: return Err() instead of panicking
98+
--> $DIR/panic_in_result.rs:68:5
99+
|
100+
LL | todo!("finish main method");
101+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
103+
104+
error: aborting due to 6 previous errors
71105

0 commit comments

Comments
 (0)