about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCatherine <114838443+Centri3@users.noreply.github.com>2023-06-24 09:40:58 -0500
committerCatherine <114838443+Centri3@users.noreply.github.com>2023-06-29 06:46:28 -0500
commit95b24d44a68e3f84c10e392cb19e2db921cbedf8 (patch)
tree87a35e885c3cee7d38c2abbdb48c2949d8c79a96
parentb1acbde618084db21339030db44120d84efc93ed (diff)
downloadrust-95b24d44a68e3f84c10e392cb19e2db921cbedf8.tar.gz
rust-95b24d44a68e3f84c10e392cb19e2db921cbedf8.zip
Fix FP
-rw-r--r--clippy_lints/src/tuple_array_conversions.rs15
-rw-r--r--tests/ui/tuple_array_conversions.rs2
-rw-r--r--tests/ui/tuple_array_conversions.stderr18
3 files changed, 12 insertions, 23 deletions
diff --git a/clippy_lints/src/tuple_array_conversions.rs b/clippy_lints/src/tuple_array_conversions.rs
index 9d5e872bd78..6564666d186 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 64aaa53be5c..f96a7c97f1a 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 d9d59a611a0..7352b089d6b 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