Skip to content

Commit 39eb5e2

Browse files
committed
Fix FP
1 parent 567a3ed commit 39eb5e2

File tree

3 files changed

+12
-23
lines changed

3 files changed

+12
-23
lines changed

clippy_lints/src/tuple_array_conversions.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use clippy_utils::{
55
path_to_local,
66
};
77
use itertools::Itertools;
8+
use rustc_ast::LitKind;
89
use rustc_hir::{Expr, ExprKind, Node, Pat};
910
use rustc_lint::{LateContext, LateLintPass, LintContext};
1011
use rustc_middle::{lint::in_external_macro, ty};
@@ -82,8 +83,9 @@ fn check_array<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
8283

8384
if let Some(elements) = elements
8485
.iter()
85-
.map(|expr| {
86-
if let ExprKind::Field(path, _) = expr.kind {
86+
.enumerate()
87+
.map(|(i, expr)| {
88+
if let ExprKind::Field(path, field) = expr.kind && field.as_str() == i.to_string() {
8789
return Some(path);
8890
};
8991

@@ -146,8 +148,13 @@ fn check_tuple<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
146148

147149
if let Some(elements) = elements
148150
.iter()
149-
.map(|expr| {
150-
if let ExprKind::Index(path, _) = expr.kind {
151+
.enumerate()
152+
.map(|(i, expr)| {
153+
if let ExprKind::Index(path, index) = expr.kind
154+
&& let ExprKind::Lit(lit) = index.kind
155+
&& let LitKind::Int(val, _) = lit.node
156+
&& val as usize == i
157+
{
151158
return Some(path);
152159
};
153160

tests/ui/tuple_array_conversions.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ fn main() {
3939
let y = (1, 2);
4040
[x.0, y.0];
4141
[x.0, y.1];
42-
// FP
4342
let x = [x.0, x.0];
4443
let x = (x[0], x[0]);
45-
// How can this be fixed?
4644
external! {
4745
let t1: &[(u32, u32)] = &[(1, 2), (3, 4)];
4846
let v1: Vec<[u32; 2]> = t1.iter().map(|&(a, b)| [a, b]).collect();

tests/ui/tuple_array_conversions.stderr

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,6 @@ LL | t1.iter().for_each(|&(a, b)| _ = [a, b]);
5555
|
5656
= help: use `.into()` instead, or `<[T; N]>::from` if type annotations are needed
5757

58-
error: it looks like you're trying to convert a tuple to an array
59-
--> $DIR/tuple_array_conversions.rs:43:13
60-
|
61-
LL | let x = [x.0, x.0];
62-
| ^^^^^^^^^^
63-
|
64-
= help: use `.into()` instead, or `<[T; N]>::from` if type annotations are needed
65-
66-
error: it looks like you're trying to convert an array to a tuple
67-
--> $DIR/tuple_array_conversions.rs:44:13
68-
|
69-
LL | let x = (x[0], x[0]);
70-
| ^^^^^^^^^^^^
71-
|
72-
= help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::from` if type annotations are needed
73-
7458
error: it looks like you're trying to convert an array to a tuple
7559
--> $DIR/tuple_array_conversions.rs:71:13
7660
|
@@ -95,5 +79,5 @@ LL | let x = (x[0], x[1]);
9579
|
9680
= help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::from` if type annotations are needed
9781

98-
error: aborting due to 12 previous errors
82+
error: aborting due to 10 previous errors
9983

0 commit comments

Comments
 (0)