Skip to content

Commit a473467

Browse files
committed
Rebasing with master
1 parent 386784f commit a473467

File tree

5 files changed

+54
-59
lines changed

5 files changed

+54
-59
lines changed

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
19821982
store.register_late_pass(|| Box::new(missing_doc::MissingDoc::new()));
19831983
store.register_late_pass(|| Box::new(missing_inline::MissingInline));
19841984
store.register_late_pass(move || Box::new(exhaustive_items::ExhaustiveItems));
1985-
store.register_late_pass(|| box match_result_ok::MatchResultOk);
1985+
store.register_late_pass(|| Box::new(match_result_ok::MatchResultOk));
19861986
store.register_late_pass(|| Box::new(partialeq_ne_impl::PartialEqNeImpl));
19871987
store.register_late_pass(|| Box::new(unused_io_amount::UnusedIoAmount));
19881988
let enum_variant_size_threshold = conf.enum_variant_size_threshold;

clippy_lints/src/match_result_ok.rs

+44-33
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ declare_lint_pass!(MatchResultOk => [MATCH_RESULT_OK]);
4848
impl<'tcx> LateLintPass<'tcx> for MatchResultOk {
4949
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
5050
if_chain! {
51-
if let Some(higher::IfLet { let_pat, let_expr, .. }) = higher::IfLet::hir(cx, expr) |
52-
if let Some(higher::WhileLet { let_pat, let_expr, .. }) = higher::WhileLet::hir(cx, expr);
51+
if let Some(higher::IfLet { let_pat, let_expr, .. }) = higher::IfLet::hir(cx, expr);
5352
if let ExprKind::MethodCall(_, ok_span, [ref result_types_0, ..], _) = let_expr.kind; //check is expr.ok() has type Result<T,E>.ok(, _)
5453
if let PatKind::TupleStruct(QPath::Resolved(_, x), y, _) = let_pat.kind; //get operation
5554
if method_chain_args(let_expr, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
@@ -61,38 +60,50 @@ impl<'tcx> LateLintPass<'tcx> for MatchResultOk {
6160
let mut applicability = Applicability::MachineApplicable;
6261
let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
6362
let trimmed_ok = snippet_with_applicability(cx, let_expr.span.until(ok_span), "", &mut applicability);
63+
let sugg = format!(
64+
"if let Ok({}) = {}",
65+
some_expr_string,
66+
trimmed_ok.trim().trim_end_matches('.'),
67+
);
68+
span_lint_and_sugg(
69+
cx,
70+
MATCH_RESULT_OK,
71+
expr.span.with_hi(let_expr.span.hi()),
72+
"matching on `Some` with `ok()` is redundant",
73+
&format!("consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
74+
sugg,
75+
applicability,
76+
);
77+
}
78+
}
79+
80+
if_chain! {
81+
if let Some(higher::WhileLet { let_pat, let_expr, .. }) = higher::WhileLet::hir(expr);
82+
if let ExprKind::MethodCall(_, ok_span, [ref result_types_0, ..], _) = let_expr.kind; //check is expr.ok() has type Result<T,E>.ok(, _)
83+
if let PatKind::TupleStruct(QPath::Resolved(_, x), y, _) = let_pat.kind; //get operation
84+
if method_chain_args(let_expr, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
85+
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(result_types_0), sym::result_type);
86+
if rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(x, false)) == "Some";
87+
88+
then {
6489

65-
if let Some(higher::IfLet { let_pat, let_expr, .. }) = higher::IfLet::hir(cx, expr) {
66-
let sugg = format!(
67-
"if let Ok({}) = {}",
68-
some_expr_string,
69-
trimmed_ok.trim().trim_end_matches('.'),
70-
);
71-
span_lint_and_sugg(
72-
cx,
73-
MATCH_RESULT_OK,
74-
expr.span.with_hi(let_expr.span.hi()),
75-
"matching on `Some` with `ok()` is redundant",
76-
&format!("consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
77-
sugg,
78-
applicability,
79-
);
80-
} else if let Some(higher::WhileLet { let_pat, let_expr, .. }) = higher::WhileLet::hir(cx, expr) | {
81-
let sugg = format!(
82-
"while let Ok({}) = {}",
83-
some_expr_string,
84-
trimmed_ok.trim().trim_end_matches('.'),
85-
);
86-
span_lint_and_sugg(
87-
cx,
88-
MATCH_RESULT_OK,
89-
expr.span.with_hi(let_expr.span.hi()),
90-
"matching on `Some` with `ok()` is redundant",
91-
&format!("consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
92-
sugg,
93-
applicability,
94-
);
95-
}
90+
let mut applicability = Applicability::MachineApplicable;
91+
let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
92+
let trimmed_ok = snippet_with_applicability(cx, let_expr.span.until(ok_span), "", &mut applicability);
93+
let sugg = format!(
94+
"while let Ok({}) = {}",
95+
some_expr_string,
96+
trimmed_ok.trim().trim_end_matches('.'),
97+
);
98+
span_lint_and_sugg(
99+
cx,
100+
MATCH_RESULT_OK,
101+
expr.span.with_hi(let_expr.span.hi()),
102+
"matching on `Some` with `ok()` is redundant",
103+
&format!("consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
104+
sugg,
105+
applicability,
106+
);
96107
}
97108
}
98109
}

tests/ui/match_result_ok.fixed

+3-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#![warn(clippy::match_result_ok)]
44
#![allow(clippy::boxed_local)]
5+
#![allow(dead_code)]
56

67
// Checking `if` cases
78

@@ -54,18 +55,9 @@ fn base_2(x: i32) {
5455
}
5556
}
5657

57-
fn _base_3(test_func: Box<Result<i32, &str>>) {
58+
fn base_3(test_func: Box<Result<i32, &str>>) {
5859
// Expected to stay as is
5960
while let Some(_b) = test_func.ok() {}
6061
}
6162

62-
fn main() {
63-
// `if` cases
64-
let _ = str_to_int("1");
65-
let _ = str_to_int_ok("2");
66-
let _ = strange_some_no_else("3");
67-
68-
// `while` cases
69-
let _ = base_1(1);
70-
let _ = base_2(2);
71-
}
63+
fn main() {}

tests/ui/match_result_ok.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#![warn(clippy::match_result_ok)]
44
#![allow(clippy::boxed_local)]
5+
#![allow(dead_code)]
56

67
// Checking `if` cases
78

@@ -54,18 +55,9 @@ fn base_2(x: i32) {
5455
}
5556
}
5657

57-
fn _base_3(test_func: Box<Result<i32, &str>>) {
58+
fn base_3(test_func: Box<Result<i32, &str>>) {
5859
// Expected to stay as is
5960
while let Some(_b) = test_func.ok() {}
6061
}
6162

62-
fn main() {
63-
// `if` cases
64-
let _ = str_to_int("1");
65-
let _ = str_to_int_ok("2");
66-
let _ = strange_some_no_else("3");
67-
68-
// `while` cases
69-
let _ = base_1(1);
70-
let _ = base_2(2);
71-
}
63+
fn main() {}

tests/ui/match_result_ok.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: matching on `Some` with `ok()` is redundant
2-
--> $DIR/match_result_ok.rs:9:5
2+
--> $DIR/match_result_ok.rs:10:5
33
|
44
LL | if let Some(y) = x.parse().ok() { y } else { 0 }
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -11,7 +11,7 @@ LL | if let Ok(y) = x.parse() { y } else { 0 }
1111
| ~~~~~~~~~~~~~~~~~~~~~~~~
1212

1313
error: matching on `Some` with `ok()` is redundant
14-
--> $DIR/match_result_ok.rs:19:9
14+
--> $DIR/match_result_ok.rs:20:9
1515
|
1616
LL | if let Some(y) = x . parse() . ok () {
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -22,7 +22,7 @@ LL | if let Ok(y) = x . parse() {
2222
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2323

2424
error: matching on `Some` with `ok()` is redundant
25-
--> $DIR/match_result_ok.rs:45:5
25+
--> $DIR/match_result_ok.rs:46:5
2626
|
2727
LL | while let Some(a) = wat.next().ok() {
2828
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)