Skip to content

Commit 61c76dd

Browse files
committed
remove code duplication
1 parent 3b759bc commit 61c76dd

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

Diff for: clippy_lints/src/methods/get_first.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,23 @@ pub(super) fn check<'tcx>(
2020
if_chain! {
2121
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
2222
if let Some(impl_id) = cx.tcx.impl_of_method(method_id);
23-
if cx.tcx.type_of(impl_id).instantiate_identity().is_slice();
23+
let identity = cx.tcx.type_of(impl_id).instantiate_identity();
2424
if let hir::ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = arg.kind;
2525
then {
26-
let mut app = Applicability::MachineApplicable;
27-
let slice_name = snippet_with_applicability(cx, recv.span, "..", &mut app);
28-
span_lint_and_sugg(
29-
cx,
30-
GET_FIRST,
31-
expr.span,
32-
&format!("accessing first element with `{slice_name}.get(0)`"),
33-
"try",
34-
format!("{slice_name}.first()"),
35-
app,
36-
);
37-
}
38-
}
39-
40-
if_chain! {
41-
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
42-
if let Some(impl_id) = cx.tcx.impl_of_method(method_id);
43-
if is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id).instantiate_identity(), sym::VecDeque);
44-
if let hir::ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = arg.kind;
45-
then {
46-
let mut app = Applicability::MachineApplicable;
26+
if identity.is_slice() {
27+
let mut app = Applicability::MachineApplicable;
28+
let slice_name = snippet_with_applicability(cx, recv.span, "..", &mut app);
29+
span_lint_and_sugg(
30+
cx,
31+
GET_FIRST,
32+
expr.span,
33+
&format!("accessing first element with `{slice_name}.get(0)`"),
34+
"try",
35+
format!("{slice_name}.first()"),
36+
app,
37+
);
38+
} else if is_type_diagnostic_item(cx, identity, sym::VecDeque){
39+
let mut app = Applicability::MachineApplicable;
4740
let slice_name = snippet_with_applicability(cx, recv.span, "..", &mut app);
4841
span_lint_and_sugg(
4942
cx,
@@ -54,6 +47,7 @@ pub(super) fn check<'tcx>(
5447
format!("{slice_name}.front()"),
5548
app,
5649
);
50+
}
5751
}
5852
}
5953
}

0 commit comments

Comments
 (0)