Skip to content

Commit 9e55424

Browse files
committed
Apply review comments
1 parent 9035264 commit 9e55424

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

clippy_lints/src/ok_if_let.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_errors::Applicability;
44
use rustc_hir::*;
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_span::BytePos;
78

89
declare_clippy_lint! {
910
/// **What it does:*** Checks for unnecessary `ok()` in if let.
@@ -40,22 +41,22 @@ declare_lint_pass!(OkIfLet => [IF_LET_SOME_RESULT]);
4041
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
4142
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
4243
if_chain! { //begin checking variables
43-
if let ExprKind::Match(ref op, ref body, ref source) = expr.kind; //test if expr is a match
44-
if let MatchSource::IfLetDesugar { contains_else_clause } = *source; //test if it is an If Let
44+
if let ExprKind::Match(ref op, ref body, source) = expr.kind; //test if expr is a match
45+
if let MatchSource::IfLetDesugar { contains_else_clause } = source; //test if it is an If Let
4546
if let ExprKind::MethodCall(_, ok_span, ref result_types) = op.kind; //check is expr.ok() has type Result<T,E>.ok()
4647
if let PatKind::TupleStruct(QPath::Resolved(_, ref x), ref y, _) = body[0].pat.kind; //get operation
4748
if method_chain_args(op, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
4849

4950
then {
5051
let is_result_type = match_type(cx, cx.tables.expr_ty(&result_types[0]), &paths::RESULT);
5152
let mut applicability = Applicability::MachineApplicable;
53+
let trimed_ok_span = op.span.until(op.span.with_lo(ok_span.lo() - BytePos(1)));
5254
let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
53-
let trimmed_ok = snippet_with_applicability(cx, op.span.until(ok_span), "", &mut applicability);
55+
let trimmed_ok = snippet_with_applicability(cx, trimed_ok_span, "", &mut applicability);
5456
let mut sugg = format!(
5557
"if let Ok({}) = {} {}",
5658
some_expr_string,
57-
// FIXME(JohnTitor): this trimming is hacky, probably can improve it
58-
trimmed_ok.trim_matches('.'),
59+
trimmed_ok,
5960
snippet(cx, body[0].span, ".."),
6061
);
6162
if contains_else_clause {

tests/ui/ok_if_let.fixed

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ fn nested_some_no_else(x: &str) -> i32 {
2828
}
2929

3030
fn main() {
31-
let x = str_to_int("1");
32-
let y = str_to_int_ok("2");
33-
let z = nested_some_no_else("3");
34-
println!("{}{}{}", x, y, z);
31+
let _ = str_to_int("1");
32+
let _ = str_to_int_ok("2");
33+
let _ = nested_some_no_else("3");
3534
}

tests/ui/ok_if_let.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ fn nested_some_no_else(x: &str) -> i32 {
2828
}
2929

3030
fn main() {
31-
let x = str_to_int("1");
32-
let y = str_to_int_ok("2");
33-
let z = nested_some_no_else("3");
34-
println!("{}{}{}", x, y, z);
31+
let _ = str_to_int("1");
32+
let _ = str_to_int_ok("2");
33+
let _ = nested_some_no_else("3");
3534
}

0 commit comments

Comments
 (0)