Skip to content

Commit 419c87f

Browse files
committed
Refactor 'check_unwrap_or_default'
1 parent 1436fea commit 419c87f

File tree

1 file changed

+26
-33
lines changed
  • clippy_lints/src/methods

1 file changed

+26
-33
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,43 +1229,36 @@ fn lint_or_fun_call<'a, 'tcx>(
12291229
or_has_args: bool,
12301230
span: Span,
12311231
) -> bool {
1232-
if or_has_args {
1233-
return false;
1234-
}
1235-
1236-
if name == "unwrap_or" {
1237-
if let hir::ExprKind::Path(ref qpath) = fun.node {
1238-
let path = &*last_path_segment(qpath).ident.as_str();
1232+
if_chain! {
1233+
if !or_has_args;
1234+
if name == "unwrap_or";
1235+
if let hir::ExprKind::Path(ref qpath) = fun.node;
1236+
let path = &*last_path_segment(qpath).ident.as_str();
1237+
if ["default", "new"].contains(&path);
1238+
let arg_ty = cx.tables.expr_ty(arg);
1239+
if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT);
1240+
if implements_trait(cx, arg_ty, default_trait_id, &[]);
12391241

1240-
if ["default", "new"].contains(&path) {
1241-
let arg_ty = cx.tables.expr_ty(arg);
1242-
let default_trait_id = if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT) {
1243-
default_trait_id
1244-
} else {
1245-
return false;
1246-
};
1242+
then {
1243+
let mut applicability = Applicability::MachineApplicable;
1244+
span_lint_and_sugg(
1245+
cx,
1246+
OR_FUN_CALL,
1247+
span,
1248+
&format!("use of `{}` followed by a call to `{}`", name, path),
1249+
"try this",
1250+
format!(
1251+
"{}.unwrap_or_default()",
1252+
snippet_with_applicability(cx, self_expr.span, "_", &mut applicability)
1253+
),
1254+
applicability,
1255+
);
12471256

1248-
if implements_trait(cx, arg_ty, default_trait_id, &[]) {
1249-
let mut applicability = Applicability::MachineApplicable;
1250-
span_lint_and_sugg(
1251-
cx,
1252-
OR_FUN_CALL,
1253-
span,
1254-
&format!("use of `{}` followed by a call to `{}`", name, path),
1255-
"try this",
1256-
format!(
1257-
"{}.unwrap_or_default()",
1258-
snippet_with_applicability(cx, self_expr.span, "_", &mut applicability)
1259-
),
1260-
applicability,
1261-
);
1262-
return true;
1263-
}
1264-
}
1257+
true
1258+
} else {
1259+
false
12651260
}
12661261
}
1267-
1268-
false
12691262
}
12701263

12711264
/// Checks for `*or(foo())`.

0 commit comments

Comments
 (0)