|
1 |
| -use crate::utils::{match_type, method_chain_args, paths, snippet_with_applicability, span_lint_and_then}; |
| 1 | +use crate::utils::{match_type, method_chain_args, paths, snippet_with_applicability, span_lint_and_sugg}; |
2 | 2 | use if_chain::if_chain;
|
3 | 3 | use rustc_errors::Applicability;
|
4 | 4 | use rustc_hir::*;
|
@@ -50,28 +50,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
|
50 | 50 | then {
|
51 | 51 | let is_result_type = match_type(cx, cx.tables.expr_ty(&result_types[0]), &paths::RESULT);
|
52 | 52 | let mut applicability = Applicability::MachineApplicable;
|
53 |
| - let trimed_ok_span = op.span.until(op.span.with_lo(ok_span.lo() - BytePos(1))); |
| 53 | + // ok_span = `ok` |
| 54 | + // op.span = `x.parse() . ok()` |
| 55 | + // op.span.until(op.span.with_lo(ok_span.lo() - BytePos(1))) = `x.parse() .` |
| 56 | + // op.span.with_lo(ok_span.lo() - BytePos(1)) = ` ok()` |
| 57 | + // op.span.with_hi(ok_span.hi() - BytePos(1)) = `x.parse() . o` |
54 | 58 | let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
|
55 |
| - let trimmed_ok = snippet_with_applicability(cx, trimed_ok_span, "", &mut applicability); |
| 59 | + let trimmed_ok = snippet_with_applicability(cx, op.span.until(ok_span), "", &mut applicability); |
56 | 60 | let sugg = format!(
|
57 | 61 | "if let Ok({}) = {}",
|
58 | 62 | some_expr_string,
|
59 |
| - trimmed_ok, |
| 63 | + trimmed_ok.trim().trim_end_matches('.'), |
60 | 64 | );
|
61 | 65 | if print::to_string(print::NO_ANN, |s| s.print_path(x, false)) == "Some" && is_result_type {
|
62 |
| - span_lint_and_then( |
| 66 | + span_lint_and_sugg( |
63 | 67 | cx,
|
64 | 68 | IF_LET_SOME_RESULT,
|
65 |
| - expr.span, |
| 69 | + expr.span.with_hi(ok_span.hi() + BytePos(2)), |
66 | 70 | "Matching on `Some` with `ok()` is redundant",
|
67 |
| - |db| { |
68 |
| - db.span_suggestion( |
69 |
| - expr.span.shrink_to_lo().to(ok_span.with_hi(ok_span.hi() + BytePos(2))), |
70 |
| - &format!("Consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string), |
71 |
| - sugg, |
72 |
| - applicability, |
73 |
| - ); |
74 |
| - }, |
| 71 | + &format!("Consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string), |
| 72 | + sugg, |
| 73 | + applicability, |
75 | 74 | );
|
76 | 75 | }
|
77 | 76 | }
|
|
0 commit comments