From 78dc48eaa6915cb5ff24f80a3b6ef9575e3fe57d Mon Sep 17 00:00:00 2001
From: Catherine <114838443+Centri3@users.noreply.github.com>
Date: Sat, 24 Jun 2023 10:08:26 -0500
Subject: [PATCH] heavily refactor
---
clippy_lints/src/tuple_array_conversions.rs | 180 +++++++++-----------
tests/ui/tuple_array_conversions.stderr | 6 +-
2 files changed, 88 insertions(+), 98 deletions(-)
diff --git a/clippy_lints/src/tuple_array_conversions.rs b/clippy_lints/src/tuple_array_conversions.rs
index 6564666d186b..1e137ce16de1 100644
--- a/clippy_lints/src/tuple_array_conversions.rs
+++ b/clippy_lints/src/tuple_array_conversions.rs
@@ -4,9 +4,8 @@ use clippy_utils::{
msrvs::{self, Msrv},
path_to_local,
};
-use itertools::Itertools;
use rustc_ast::LitKind;
-use rustc_hir::{Expr, ExprKind, Node, Pat};
+use rustc_hir::{Expr, ExprKind, HirId, Node, Pat};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::{lint::in_external_macro, ty};
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -17,8 +16,8 @@ declare_clippy_lint! {
/// Checks for tuple<=>array conversions that are not done with `.into()`.
///
/// ### Why is this bad?
- /// It's overly complex. `.into()` works for tuples<=>arrays with less than 13 elements and
- /// conveys the intent a lot better, while also leaving less room for bugs!
+ /// It's unnecessary complexity. `.into()` works for tuples<=>arrays at or below 13 elements and
+ /// conveys the intent a lot better, while also leaving less room for confusing bugs!
///
/// ### Example
/// ```rust,ignore
@@ -45,26 +44,28 @@ pub struct TupleArrayConversions {
impl LateLintPass<'_> for TupleArrayConversions {
fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if !in_external_macro(cx.sess(), expr.span) && self.msrv.meets(msrvs::TUPLE_ARRAY_CONVERSIONS) {
- _ = check_array(cx, expr) || check_tuple(cx, expr);
+ match expr.kind {
+ ExprKind::Array(elements) if (1..=12).contains(&elements.len()) => check_array(cx, expr, elements),
+ ExprKind::Tup(elements) if (1..=12).contains(&elements.len()) => check_tuple(cx, expr, elements),
+ _ => {},
+ }
}
}
extract_msrv_attr!(LateContext);
}
-fn check_array<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
- let ExprKind::Array(elements) = expr.kind else {
- return false;
- };
- if !(1..=12).contains(&elements.len()) {
- return false;
- }
-
- if let Some(locals) = path_to_locals(cx, &elements.iter().collect_vec())
- && let [first, rest @ ..] = &*locals
- && let Node::Pat(first_pat) = first
- && let first_id = parent_pat(cx, first_pat).hir_id
- && rest.iter().chain(once(first)).all(|local| {
+#[expect(
+ clippy::blocks_in_if_conditions,
+ reason = "not a FP, but this is much easier to understand"
+)]
+fn check_array<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, elements: &'tcx [Expr<'tcx>]) {
+ if should_lint(
+ cx,
+ elements,
+ // This is cursed.
+ Some,
+ |(first_id, local)| {
if let Node::Pat(pat) = local
&& let parent = parent_pat(cx, pat)
&& parent.hir_id == first_id
@@ -76,27 +77,18 @@ fn check_array<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
}
false
- })
- {
- return emit_lint(cx, expr, ToType::Array);
- }
-
- if let Some(elements) = elements
- .iter()
- .enumerate()
- .map(|(i, expr)| {
- if let ExprKind::Field(path, field) = expr.kind && field.as_str() == i.to_string() {
- return Some(path);
- };
-
- None
- })
- .collect::