diff --git a/clippy_lints/src/tuple_array_conversions.rs b/clippy_lints/src/tuple_array_conversions.rs index 9d5e872bd78f..6564666d186b 100644 --- a/clippy_lints/src/tuple_array_conversions.rs +++ b/clippy_lints/src/tuple_array_conversions.rs @@ -5,6 +5,7 @@ use clippy_utils::{ path_to_local, }; use itertools::Itertools; +use rustc_ast::LitKind; use rustc_hir::{Expr, ExprKind, Node, Pat}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::{lint::in_external_macro, ty}; @@ -82,8 +83,9 @@ fn check_array<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool { if let Some(elements) = elements .iter() - .map(|expr| { - if let ExprKind::Field(path, _) = expr.kind { + .enumerate() + .map(|(i, expr)| { + if let ExprKind::Field(path, field) = expr.kind && field.as_str() == i.to_string() { return Some(path); }; @@ -146,8 +148,13 @@ fn check_tuple<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool { if let Some(elements) = elements .iter() - .map(|expr| { - if let ExprKind::Index(path, _) = expr.kind { + .enumerate() + .map(|(i, expr)| { + if let ExprKind::Index(path, index) = expr.kind + && let ExprKind::Lit(lit) = index.kind + && let LitKind::Int(val, _) = lit.node + && val as usize == i + { return Some(path); }; diff --git a/tests/ui/tuple_array_conversions.rs b/tests/ui/tuple_array_conversions.rs index 279c0c4f54a2..a17f661fe232 100644 --- a/tests/ui/tuple_array_conversions.rs +++ b/tests/ui/tuple_array_conversions.rs @@ -39,10 +39,8 @@ fn main() { let y = (1, 2); [x.0, y.0]; [x.0, y.1]; - // FP let x = [x.0, x.0]; let x = (x[0], x[0]); - // How can this be fixed? external! { let t1: &[(u32, u32)] = &[(1, 2), (3, 4)]; let v1: Vec<[u32; 2]> = t1.iter().map(|&(a, b)| [a, b]).collect(); diff --git a/tests/ui/tuple_array_conversions.stderr b/tests/ui/tuple_array_conversions.stderr index d9d59a611a01..7352b089d6b5 100644 --- a/tests/ui/tuple_array_conversions.stderr +++ b/tests/ui/tuple_array_conversions.stderr @@ -55,22 +55,6 @@ LL | t1.iter().for_each(|&(a, b)| _ = [a, b]); | = help: use `.into()` instead, or `<[T; N]>::from` if type annotations are needed -error: it looks like you're trying to convert a tuple to an array - --> $DIR/tuple_array_conversions.rs:43:13 - | -LL | let x = [x.0, x.0]; - | ^^^^^^^^^^ - | - = help: use `.into()` instead, or `<[T; N]>::from` if type annotations are needed - -error: it looks like you're trying to convert an array to a tuple - --> $DIR/tuple_array_conversions.rs:44:13 - | -LL | let x = (x[0], x[0]); - | ^^^^^^^^^^^^ - | - = help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::from` if type annotations are needed - error: it looks like you're trying to convert an array to a tuple --> $DIR/tuple_array_conversions.rs:71:13 | @@ -95,5 +79,5 @@ LL | let x = (x[0], x[1]); | = help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::from` if type annotations are needed -error: aborting due to 12 previous errors +error: aborting due to 10 previous errors