Skip to content

Commit 3550568

Browse files
author
Vali Schneider
committed
removing if chain and renaming lint
1 parent f9fcbbe commit 3550568

File tree

6 files changed

+32
-39
lines changed

6 files changed

+32
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1651,7 +1651,7 @@ Released 2018-09-13
16511651
[`out_of_bounds_indexing`]: https://rust-lang.github.io/rust-clippy/master/index.html#out_of_bounds_indexing
16521652
[`overflow_check_conditional`]: https://rust-lang.github.io/rust-clippy/master/index.html#overflow_check_conditional
16531653
[`panic`]: https://rust-lang.github.io/rust-clippy/master/index.html#panic
1654-
[`panic_in_result`]: https://rust-lang.github.io/rust-clippy/master/index.html#panic_in_result
1654+
[`panic_in_result_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#panic_in_result
16551655
[`panic_params`]: https://rust-lang.github.io/rust-clippy/master/index.html#panic_params
16561656
[`panicking_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#panicking_unwrap
16571657
[`partialeq_ne_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_ne_impl

clippy_lints/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ mod open_options;
267267
mod option_env_unwrap;
268268
mod option_if_let_else;
269269
mod overflow_check_conditional;
270-
mod panic_in_result;
270+
mod panic_in_result_fn;
271271
mod panic_unimplemented;
272272
mod partialeq_ne_impl;
273273
mod path_buf_push_overwrite;
@@ -748,7 +748,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
748748
&option_env_unwrap::OPTION_ENV_UNWRAP,
749749
&option_if_let_else::OPTION_IF_LET_ELSE,
750750
&overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL,
751-
&panic_in_result::PANIC_IN_RESULT,
751+
&panic_in_result_fn::PANIC_IN_RESULT_FN,
752752
&panic_unimplemented::PANIC,
753753
&panic_unimplemented::PANIC_PARAMS,
754754
&panic_unimplemented::TODO,
@@ -1088,7 +1088,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10881088
store.register_late_pass(|| box manual_async_fn::ManualAsyncFn);
10891089
store.register_early_pass(|| box redundant_field_names::RedundantFieldNames);
10901090
store.register_late_pass(|| box vec_resize_to_zero::VecResizeToZero);
1091-
store.register_late_pass(|| box panic_in_result::PanicInResult);
1091+
store.register_late_pass(|| box panic_in_result_fn::PanicInResultFn);
10921092

10931093
let single_char_binding_names_threshold = conf.single_char_binding_names_threshold;
10941094
store.register_early_pass(move || box non_expressive_names::NonExpressiveNames {
@@ -1132,7 +1132,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11321132
LintId::of(&missing_doc::MISSING_DOCS_IN_PRIVATE_ITEMS),
11331133
LintId::of(&missing_inline::MISSING_INLINE_IN_PUBLIC_ITEMS),
11341134
LintId::of(&modulo_arithmetic::MODULO_ARITHMETIC),
1135-
LintId::of(&panic_in_result::PANIC_IN_RESULT),
1135+
LintId::of(&panic_in_result_fn::PANIC_IN_RESULT_FN),
11361136
LintId::of(&panic_unimplemented::PANIC),
11371137
LintId::of(&panic_unimplemented::TODO),
11381138
LintId::of(&panic_unimplemented::UNIMPLEMENTED),

clippy_lints/src/panic_in_result.rs renamed to clippy_lints/src/panic_in_result_fn.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::utils::{is_expn_of, is_type_diagnostic_item, return_ty, span_lint_and_then};
2-
use if_chain::if_chain;
32
use rustc_hir as hir;
43
use rustc_hir::intravisit::{self, FnKind, NestedVisitorMap, Visitor};
54
use rustc_hir::Expr;
@@ -23,14 +22,14 @@ declare_clippy_lint! {
2322
/// panic!("error");
2423
/// }
2524
/// ```
26-
pub PANIC_IN_RESULT,
25+
pub PANIC_IN_RESULT_FN,
2726
restriction,
2827
"functions of type `Result<..>` that contain `panic!()`, `todo!()` or `unreachable()` or `unimplemented()` "
2928
}
3029

31-
declare_lint_pass!(PanicInResult => [PANIC_IN_RESULT]);
30+
declare_lint_pass!(PanicInResultFn => [PANIC_IN_RESULT_FN]);
3231

33-
impl<'tcx> LateLintPass<'tcx> for PanicInResult {
32+
impl<'tcx> LateLintPass<'tcx> for PanicInResultFn {
3433
fn check_fn(
3534
&mut self,
3635
cx: &LateContext<'tcx>,
@@ -40,15 +39,10 @@ impl<'tcx> LateLintPass<'tcx> for PanicInResult {
4039
span: Span,
4140
hir_id: hir::HirId,
4241
) {
43-
if let FnKind::Closure(_) = fn_kind {
44-
return;
45-
}
46-
if_chain! {
47-
if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(result_type));
48-
then
49-
{
50-
lint_impl_body(cx, span, body);
51-
}
42+
if !matches!(fn_kind, FnKind::Closure(_))
43+
&& is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(result_type))
44+
{
45+
lint_impl_body(cx, span, body);
5246
}
5347
}
5448
}
@@ -61,10 +55,9 @@ impl<'tcx> Visitor<'tcx> for FindPanicUnimplementedUnreachable {
6155
type Map = Map<'tcx>;
6256

6357
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
64-
if is_expn_of(expr.span, "unimplemented").is_some()
65-
|| is_expn_of(expr.span, "unreachable").is_some()
66-
|| is_expn_of(expr.span, "panic").is_some()
67-
|| is_expn_of(expr.span, "todo").is_some()
58+
if ["unimplemented", "unreachable", "panic", "todo"]
59+
.iter()
60+
.any(|fun| is_expn_of(expr.span, fun).is_some())
6861
{
6962
self.result.push(expr.span);
7063
}
@@ -83,7 +76,7 @@ fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, body: &'tcx hir
8376
if !panics.result.is_empty() {
8477
span_lint_and_then(
8578
cx,
86-
PANIC_IN_RESULT,
79+
PANIC_IN_RESULT_FN,
8780
impl_span,
8881
"used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`",
8982
move |diag| {

src/lintlist/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,11 +1705,11 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
17051705
module: "panic_unimplemented",
17061706
},
17071707
Lint {
1708-
name: "panic_in_result",
1708+
name: "panic_in_result_fn",
17091709
group: "restriction",
17101710
desc: "functions of type `Result<..>` that contain `panic!()`, `todo!()` or `unreachable()` or `unimplemented()` ",
17111711
deprecation: None,
1712-
module: "panic_in_result",
1712+
module: "panic_in_result_fn",
17131713
},
17141714
Lint {
17151715
name: "panic_params",

tests/ui/panic_in_result.rs renamed to tests/ui/panic_in_result_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![warn(clippy::panic_in_result)]
1+
#![warn(clippy::panic_in_result_fn)]
22

33
struct A;
44

tests/ui/panic_in_result.stderr renamed to tests/ui/panic_in_result_fn.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
error: used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`
2-
--> $DIR/panic_in_result.rs:6:5
2+
--> $DIR/panic_in_result_fn.rs:6:5
33
|
44
LL | / fn result_with_panic() -> Result<bool, String> // should emit lint
55
LL | | {
66
LL | | panic!("error");
77
LL | | }
88
| |_____^
99
|
10-
= note: `-D clippy::panic-in-result` implied by `-D warnings`
10+
= note: `-D clippy::panic-in-result-fn` implied by `-D warnings`
1111
= 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
1212
note: return Err() instead of panicking
13-
--> $DIR/panic_in_result.rs:8:9
13+
--> $DIR/panic_in_result_fn.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

1919
error: used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`
20-
--> $DIR/panic_in_result.rs:11:5
20+
--> $DIR/panic_in_result_fn.rs:11:5
2121
|
2222
LL | / fn result_with_unimplemented() -> Result<bool, String> // should emit lint
2323
LL | | {
@@ -27,14 +27,14 @@ LL | | }
2727
|
2828
= 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
2929
note: return Err() instead of panicking
30-
--> $DIR/panic_in_result.rs:13:9
30+
--> $DIR/panic_in_result_fn.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

3636
error: used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`
37-
--> $DIR/panic_in_result.rs:16:5
37+
--> $DIR/panic_in_result_fn.rs:16:5
3838
|
3939
LL | / fn result_with_unreachable() -> Result<bool, String> // should emit lint
4040
LL | | {
@@ -44,14 +44,14 @@ LL | | }
4444
|
4545
= 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
4646
note: return Err() instead of panicking
47-
--> $DIR/panic_in_result.rs:18:9
47+
--> $DIR/panic_in_result_fn.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

5353
error: used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`
54-
--> $DIR/panic_in_result.rs:21:5
54+
--> $DIR/panic_in_result_fn.rs:21:5
5555
|
5656
LL | / fn result_with_todo() -> Result<bool, String> // should emit lint
5757
LL | | {
@@ -61,14 +61,14 @@ LL | | }
6161
|
6262
= 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
6363
note: return Err() instead of panicking
64-
--> $DIR/panic_in_result.rs:23:9
64+
--> $DIR/panic_in_result_fn.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

7070
error: used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`
71-
--> $DIR/panic_in_result.rs:52:1
71+
--> $DIR/panic_in_result_fn.rs:52:1
7272
|
7373
LL | / fn function_result_with_panic() -> Result<bool, String> // should emit lint
7474
LL | | {
@@ -78,14 +78,14 @@ LL | | }
7878
|
7979
= 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
8080
note: return Err() instead of panicking
81-
--> $DIR/panic_in_result.rs:54:5
81+
--> $DIR/panic_in_result_fn.rs:54:5
8282
|
8383
LL | panic!("error");
8484
| ^^^^^^^^^^^^^^^^
8585
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
8686

8787
error: used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`
88-
--> $DIR/panic_in_result.rs:67:1
88+
--> $DIR/panic_in_result_fn.rs:67:1
8989
|
9090
LL | / fn main() -> Result<(), String> {
9191
LL | | todo!("finish main method");
@@ -95,7 +95,7 @@ LL | | }
9595
|
9696
= 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
9797
note: return Err() instead of panicking
98-
--> $DIR/panic_in_result.rs:68:5
98+
--> $DIR/panic_in_result_fn.rs:68:5
9999
|
100100
LL | todo!("finish main method");
101101
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)