diff options
| author | Matthias Richter <matthias.ri97@gmail.com> | 2023-11-01 23:35:28 +0100 |
|---|---|---|
| committer | Matthias Richter <matthias.ri97@gmail.com> | 2023-11-01 23:35:28 +0100 |
| commit | 61c76dd4ffd8c96acb45bfcd290f95ae0938513e (patch) | |
| tree | fb23302ae2ba07b9800ebc277a4010e10098df6d | |
| parent | 3b759bce9d01c27d776af54bc69784c79af06048 (diff) | |
| download | rust-61c76dd4ffd8c96acb45bfcd290f95ae0938513e.tar.gz rust-61c76dd4ffd8c96acb45bfcd290f95ae0938513e.zip | |
remove code duplication
| -rw-r--r-- | clippy_lints/src/methods/get_first.rs | 38 |
1 files changed, 16 insertions, 22 deletions
diff --git a/clippy_lints/src/methods/get_first.rs b/clippy_lints/src/methods/get_first.rs index 855246d8c9f..2e1dd3ec649 100644 --- a/clippy_lints/src/methods/get_first.rs +++ b/clippy_lints/src/methods/get_first.rs @@ -20,30 +20,23 @@ pub(super) fn check<'tcx>( if_chain! { if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id); if let Some(impl_id) = cx.tcx.impl_of_method(method_id); - if cx.tcx.type_of(impl_id).instantiate_identity().is_slice(); + let identity = cx.tcx.type_of(impl_id).instantiate_identity(); if let hir::ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = arg.kind; then { - let mut app = Applicability::MachineApplicable; - let slice_name = snippet_with_applicability(cx, recv.span, "..", &mut app); - span_lint_and_sugg( - cx, - GET_FIRST, - expr.span, - &format!("accessing first element with `{slice_name}.get(0)`"), - "try", - format!("{slice_name}.first()"), - app, - ); - } - } - - if_chain! { - if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id); - if let Some(impl_id) = cx.tcx.impl_of_method(method_id); - if is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id).instantiate_identity(), sym::VecDeque); - if let hir::ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = arg.kind; - then { - let mut app = Applicability::MachineApplicable; + if identity.is_slice() { + let mut app = Applicability::MachineApplicable; + let slice_name = snippet_with_applicability(cx, recv.span, "..", &mut app); + span_lint_and_sugg( + cx, + GET_FIRST, + expr.span, + &format!("accessing first element with `{slice_name}.get(0)`"), + "try", + format!("{slice_name}.first()"), + app, + ); + } else if is_type_diagnostic_item(cx, identity, sym::VecDeque){ + let mut app = Applicability::MachineApplicable; let slice_name = snippet_with_applicability(cx, recv.span, "..", &mut app); span_lint_and_sugg( cx, @@ -54,6 +47,7 @@ pub(super) fn check<'tcx>( format!("{slice_name}.front()"), app, ); + } } } } |
