Auto merge of #11020 - Centri3:tuple_array_conversion, r=llogiq

New lint [`tuple_array_conversions`]

Closes #10748

PS, the implementation is a bit ugly 😅 ~~I will likely refactor soon enough :)~~ Done :D

changelog: New lint [`tuple_array_conversions`]
This commit is contained in:
bors 2023-07-01 15:27:35 +00:00
commit c7bf05c1a4
10 changed files with 399 additions and 2 deletions

View file

@ -0,0 +1,73 @@
//@aux-build:proc_macros.rs:proc-macro
#![allow(clippy::no_effect, clippy::useless_vec, unused)]
#![warn(clippy::tuple_array_conversions)]
#[macro_use]
extern crate proc_macros;
fn main() {
let x = [1, 2];
let x = (x[0], x[1]);
let x = [x.0, x.1];
let x = &[1, 2];
let x = (x[0], x[1]);
let t1: &[(u32, u32)] = &[(1, 2), (3, 4)];
let v1: Vec<[u32; 2]> = t1.iter().map(|&(a, b)| [a, b]).collect();
t1.iter().for_each(|&(a, b)| _ = [a, b]);
let t2: Vec<(u32, u32)> = v1.iter().map(|&[a, b]| (a, b)).collect();
t1.iter().for_each(|&(a, b)| _ = [a, b]);
// Do not lint
let v2: Vec<[u32; 2]> = t1.iter().map(|&t| t.into()).collect();
let t3: Vec<(u32, u32)> = v2.iter().map(|&v| v.into()).collect();
let x = [1; 13];
let x = (
x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10], x[11], x[12],
);
let x = [x.0, x.1, x.2, x.3, x.4, x.5, x.6, x.7, x.8, x.9, x.10, x.11, x.12];
let x = (1, 2);
let x = (x.0, x.1);
let x = [1, 2];
let x = [x[0], x[1]];
let x = vec![1, 2];
let x = (x[0], x[1]);
let x = [1; 3];
let x = (x[0],);
let x = (1, 2, 3);
let x = [x.0];
let x = (1, 2);
let y = (1, 2);
[x.0, y.0];
[x.0, y.1];
let x = [x.0, x.0];
let x = (x[0], x[0]);
external! {
let t1: &[(u32, u32)] = &[(1, 2), (3, 4)];
let v1: Vec<[u32; 2]> = t1.iter().map(|&(a, b)| [a, b]).collect();
let t2: Vec<(u32, u32)> = v1.iter().map(|&[a, b]| (a, b)).collect();
}
with_span! {
span
let t1: &[(u32, u32)] = &[(1, 2), (3, 4)];
let v1: Vec<[u32; 2]> = t1.iter().map(|&(a, b)| [a, b]).collect();
let t2: Vec<(u32, u32)> = v1.iter().map(|&[a, b]| (a, b)).collect();
}
}
#[clippy::msrv = "1.70.0"]
fn msrv_too_low() {
let x = [1, 2];
let x = (x[0], x[1]);
let x = [x.0, x.1];
let x = &[1, 2];
let x = (x[0], x[1]);
}
#[clippy::msrv = "1.71.0"]
fn msrv_juust_right() {
let x = [1, 2];
let x = (x[0], x[1]);
let x = [x.0, x.1];
let x = &[1, 2];
let x = (x[0], x[1]);
}

View file

@ -0,0 +1,83 @@
error: it looks like you're trying to convert an array to a tuple
--> $DIR/tuple_array_conversions.rs:10:13
|
LL | let x = (x[0], x[1]);
| ^^^^^^^^^^^^
|
= help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::from` if type annotations are needed
= note: `-D clippy::tuple-array-conversions` implied by `-D warnings`
error: it looks like you're trying to convert a tuple to an array
--> $DIR/tuple_array_conversions.rs:11:13
|
LL | let x = [x.0, x.1];
| ^^^^^^^^^^
|
= 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:13:13
|
LL | let x = (x[0], x[1]);
| ^^^^^^^^^^^^
|
= help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::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:16:53
|
LL | let v1: Vec<[u32; 2]> = t1.iter().map(|&(a, b)| [a, b]).collect();
| ^^^^^^
|
= 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:17:38
|
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 an array to a tuple
--> $DIR/tuple_array_conversions.rs:18:55
|
LL | let t2: Vec<(u32, u32)> = v1.iter().map(|&[a, b]| (a, b)).collect();
| ^^^^^^
|
= help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::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:19:38
|
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 an array to a tuple
--> $DIR/tuple_array_conversions.rs:69:13
|
LL | let x = (x[0], x[1]);
| ^^^^^^^^^^^^
|
= help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::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:70:13
|
LL | let x = [x.0, x.1];
| ^^^^^^^^^^
|
= 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:72:13
|
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 10 previous errors