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

Commit b006522

Browse files
author
Vali Schneider
committed
added lint for todo and removed option
1 parent b2d8ca9 commit b006522

File tree

4 files changed

+32
-78
lines changed

4 files changed

+32
-78
lines changed

clippy_lints/src/panic_in_result.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
77
use rustc_span::Span;
88

99
declare_clippy_lint! {
10-
/// **What it does:** Checks for usage of `panic!`, `unimplemented!` or `unreachable!` in a function of type result/option.
10+
/// **What it does:** Checks for usage of `panic!`, `unimplemented!`, `todo!` or `unreachable!` in a function of type result.
1111
///
12-
/// **Why is this bad?** For some codebases, it is desirable for functions of type option/result to return an error instead of crashing. Hence unimplemented, panic and unreachable should be avoided.
12+
/// **Why is this bad?** For some codebases, it is desirable for functions of type result to return an error instead of crashing. Hence unimplemented, panic and unreachable should be avoided.
1313
///
1414
/// **Known problems:** None.
1515
///
1616
/// **Example:**
1717
///
1818
/// ```rust
19-
/// fn option_with_panic() -> Option<bool>
19+
/// fn result_with_panic() -> Result<bool, String>
2020
/// {
2121
/// panic!("error");
2222
/// }
2323
/// ```
2424
2525
pub PANIC_IN_RESULT,
2626
restriction,
27-
"functions of type `Result<..>` / `Option`<...> that contain `panic!()` or `unreachable()` or `unimplemented()` "
27+
"functions of type `Result<..>` that contain `panic!()`, `todo!()` or `unreachable()` or `unimplemented()` "
2828
}
2929

3030
declare_lint_pass!(PanicInResult => [PANIC_IN_RESULT]);
@@ -35,8 +35,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicInResult {
3535
// first check if it's a method or function
3636
if let hir::ImplItemKind::Fn(ref _signature, _) = impl_item.kind;
3737
// 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-
|| is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym!(option_type));
38+
if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym!(result_type));
4039
then {
4140
lint_impl_body(cx, impl_item.span, impl_item);
4241
}
@@ -55,14 +54,13 @@ impl<'tcx> Visitor<'tcx> for FindPanicUnimplementedUnreachable {
5554
type Map = Map<'tcx>;
5655

5756
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
58-
if is_expn_of(expr.span, "unimplemented").is_some() {
59-
self.result.push(expr.span);
60-
} else if is_expn_of(expr.span, "unreachable").is_some() {
61-
self.result.push(expr.span);
62-
} else if is_expn_of(expr.span, "panic").is_some() {
57+
if is_expn_of(expr.span, "unimplemented").is_some()
58+
|| is_expn_of(expr.span, "unreachable").is_some()
59+
|| is_expn_of(expr.span, "panic").is_some()
60+
|| is_expn_of(expr.span, "todo").is_some()
61+
{
6362
self.result.push(expr.span);
6463
}
65-
6664
// and check sub-expressions
6765
intravisit::walk_expr(self, expr);
6866
}
@@ -88,10 +86,10 @@ fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_item: &'tc
8886
cx,
8987
PANIC_IN_RESULT,
9088
impl_span,
91-
"used unimplemented, unreachable or panic in a function that returns result or option",
89+
"used unimplemented, unreachable, todo or panic in a function that returns result",
9290
move |diag| {
9391
diag.help(
94-
"unimplemented, unreachable or panic should not be used in a function that returns result or option" );
92+
"unimplemented, unreachable, todo or panic should not be used in a function that returns result" );
9593
diag.span_note(fpu.result, "will cause the application to crash.");
9694
});
9795
}

src/lintlist/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
17071707
Lint {
17081708
name: "panic_in_result",
17091709
group: "restriction",
1710-
desc: "functions of type `Result<..>` / `Option`<...> that contain `panic!()` or `unreachable()` or `unimplemented()` ",
1710+
desc: "functions of type `Result<..>` that contain `panic!()`, `todo!()` or `unreachable()` or `unimplemented()` ",
17111711
deprecation: None,
17121712
module: "panic_in_result",
17131713
},

tests/ui/panic_in_result.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,9 @@ impl A {
1818
unreachable!();
1919
}
2020

21-
fn option_with_unreachable() -> Option<bool> // should emit lint
21+
fn result_with_todo() -> Result<bool, String> // should emit lint
2222
{
23-
unreachable!();
24-
}
25-
26-
fn option_with_unimplemented() -> Option<bool> // should emit lint
27-
{
28-
unimplemented!();
29-
}
30-
31-
fn option_with_panic() -> Option<bool> // should emit lint
32-
{
33-
panic!("error");
23+
todo!("Finish this");
3424
}
3525

3626
fn other_with_panic() // should not emit lint
@@ -48,14 +38,14 @@ impl A {
4838
unimplemented!();
4939
}
5040

51-
fn result_without_banned_functions() -> Result<bool, String> // should not emit lint
41+
fn other_with_todo() // should not emit lint
5242
{
53-
Ok(true)
43+
todo!("finish this")
5444
}
5545

56-
fn option_without_banned_functions() -> Option<bool> // should not emit lint
46+
fn result_without_banned_functions() -> Result<bool, String> // should not emit lint
5747
{
58-
Some(true)
48+
Ok(true)
5949
}
6050
}
6151

tests/ui/panic_in_result.stderr

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: used unimplemented, unreachable or panic in a function that returns result or option
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 or panic should not be used in a function that returns result or option
11+
= help: unimplemented, unreachable, todo or panic should not be used in a function that returns result
1212
note: will cause the application to crash.
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 or panic in a function that returns result or option
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 or panic should not be used in a function that returns result or option
28+
= help: unimplemented, unreachable, todo or panic should not be used in a function that returns result
2929
note: will cause the application to crash.
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 or panic in a function that returns result or option
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,64 +42,30 @@ LL | | unreachable!();
4242
LL | | }
4343
| |_____^
4444
|
45-
= help: unimplemented, unreachable or panic should not be used in a function that returns result or option
45+
= help: unimplemented, unreachable, todo or panic should not be used in a function that returns result
4646
note: will cause the application to crash.
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 or panic in a function that returns result or option
53+
error: used unimplemented, unreachable, todo or panic in a function that returns result
5454
--> $DIR/panic_in_result.rs:21:5
5555
|
56-
LL | / fn option_with_unreachable() -> Option<bool> // should emit lint
56+
LL | / fn result_with_todo() -> Result<bool, String> // should emit lint
5757
LL | | {
58-
LL | | unreachable!();
58+
LL | | todo!("Finish this");
5959
LL | | }
6060
| |_____^
6161
|
62-
= help: unimplemented, unreachable or panic should not be used in a function that returns result or option
62+
= help: unimplemented, unreachable, todo or panic should not be used in a function that returns result
6363
note: will cause the application to crash.
6464
--> $DIR/panic_in_result.rs:23:9
6565
|
66-
LL | unreachable!();
67-
| ^^^^^^^^^^^^^^^
68-
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
69-
70-
error: used unimplemented, unreachable or panic in a function that returns result or option
71-
--> $DIR/panic_in_result.rs:26:5
72-
|
73-
LL | / fn option_with_unimplemented() -> Option<bool> // should emit lint
74-
LL | | {
75-
LL | | unimplemented!();
76-
LL | | }
77-
| |_____^
78-
|
79-
= help: unimplemented, unreachable or panic should not be used in a function that returns result or option
80-
note: will cause the application to crash.
81-
--> $DIR/panic_in_result.rs:28:9
82-
|
83-
LL | unimplemented!();
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 or panic in a function that returns result or option
88-
--> $DIR/panic_in_result.rs:31:5
89-
|
90-
LL | / fn option_with_panic() -> Option<bool> // should emit lint
91-
LL | | {
92-
LL | | panic!("error");
93-
LL | | }
94-
| |_____^
95-
|
96-
= help: unimplemented, unreachable or panic should not be used in a function that returns result or option
97-
note: will cause the application to crash.
98-
--> $DIR/panic_in_result.rs:33:9
99-
|
100-
LL | panic!("error");
101-
| ^^^^^^^^^^^^^^^^
66+
LL | todo!("Finish this");
67+
| ^^^^^^^^^^^^^^^^^^^^^
10268
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
10369

104-
error: aborting due to 6 previous errors
70+
error: aborting due to 4 previous errors
10571

0 commit comments

Comments
 (0)