Skip to content

Commit d8ac4b0

Browse files
committed
Apply review comments
1 parent 595476c commit d8ac4b0

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
@@ -5,6 +5,7 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
55
use rustc_errors::Applicability;
66
use rustc_hir::*;
77
use rustc_session::declare_tool_lint;
8+
use rustc_span::BytePos;
89

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

5051
then {
5152
let is_result_type = match_type(cx, cx.tables.expr_ty(&result_types[0]), &paths::RESULT);
5253
let mut applicability = Applicability::MachineApplicable;
54+
let trimed_ok_span = op.span.until(op.span.with_lo(ok_span.lo() - BytePos(1)));
5355
let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
54-
let trimmed_ok = snippet_with_applicability(cx, op.span.until(ok_span), "", &mut applicability);
56+
let trimmed_ok = snippet_with_applicability(cx, trimed_ok_span, "", &mut applicability);
5557
let mut sugg = format!(
5658
"if let Ok({}) = {} {}",
5759
some_expr_string,
58-
// FIXME(JohnTitor): this trimming is hacky, probably can improve it
59-
trimmed_ok.trim_matches('.'),
60+
trimmed_ok,
6061
snippet(cx, body[0].span, ".."),
6162
);
6263
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)