Skip to content

Commit 4493fc7

Browse files
FoseFxxFrednet
andcommitted
refactor: use pattern matching for MethodCall arguments
Co-authored-by: xFrednet <xFrednet@gmail.com>
1 parent c22ff6c commit 4493fc7

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

clippy_lints/src/use_unwrap_or.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use if_chain::if_chain;
44
use rustc_hir::{Expr, ExprKind, QPath};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
7-
use rustc_span::{sym, Span};
7+
use rustc_span::sym;
88

99
declare_clippy_lint! {
1010
/// ### What it does
@@ -47,25 +47,22 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
4747
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
4848
// look for x.or().unwrap()
4949
if_chain! {
50-
if let ExprKind::MethodCall(path, args, unwrap_span) = expr.kind;
50+
if let ExprKind::MethodCall(path, [unwrap_self], unwrap_span) = expr.kind;
5151
if path.ident.name == sym::unwrap;
52-
if let Some(caller) = args.first();
53-
if let ExprKind::MethodCall(caller_path, caller_args, or_span) = caller.kind;
52+
if let ExprKind::MethodCall(caller_path, [or_self, or_arg], or_span) = unwrap_self.kind;
5453
if caller_path.ident.name == sym::or;
5554
then {
56-
let ty = cx.typeck_results().expr_ty(&caller_args[0]); // get type of x (we later check if it's Option or Result)
55+
let ty = cx.typeck_results().expr_ty(&or_self); // get type of x (we later check if it's Option or Result)
5756
let title;
58-
let arg = &caller_args[1]; // the argument or(xyz) is called with
5957

6058
if is_type_diagnostic_item(cx, ty, sym::Option) {
6159
title = ".or(Some(…)).unwrap() found";
62-
if !is(arg, "Some") {
60+
if !is(or_arg, "Some") {
6361
return;
6462
}
65-
6663
} else if is_type_diagnostic_item(cx, ty, sym::Result) {
6764
title = ".or(Ok(…)).unwrap() found";
68-
if !is(arg, "Ok") {
65+
if !is(or_arg, "Ok") {
6966
return;
7067
}
7168
} else {
@@ -74,13 +71,10 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
7471
return;
7572
}
7673

77-
// span = or_span + unwrap_span
78-
let span = Span::new(or_span.lo(), unwrap_span.hi(), or_span.ctxt(), or_span.parent());
79-
8074
span_lint_and_help(
8175
cx,
8276
USE_UNWRAP_OR,
83-
span,
77+
or_span.to(unwrap_span),
8478
title,
8579
None,
8680
"use `unwrap_or()` instead"

0 commit comments

Comments
 (0)