summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/tuple_array_conversions.rs
blob: 569415acbceee5b971a46581cce49372056de910 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//@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();
    }
    // FP #11082; needs discussion
    let (a, b) = (1.0f64, 2.0f64);
    let _: &[f64] = &[a, b];
    // FP #11085; impossible to fix
    let [src, dest]: [_; 2] = [1, 2];
    (src, dest);
    // FP #11100
    fn issue_11100_array_to_tuple(this: [&mut i32; 2]) -> (&i32, &mut i32) {
        let [input, output] = this;
        (input, output)
    }

    fn issue_11100_tuple_to_array<'a>(this: (&'a mut i32, &'a mut i32)) -> [&'a i32; 2] {
        let (input, output) = this;
        [input, output]
    }
    // FP #11124
    // tuple=>array
    let (a, b) = (1, 2);
    [a, b];
    let x = a;
    // array=>tuple
    let [a, b] = [1, 2];
    (a, b);
    let x = a;
    // FP #11144
    let (a, (b, c)) = (1, (2, 3));
    [a, c];
    let [[a, b], [c, d]] = [[1, 2], [3, 4]];
    (a, c);
}

#[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]);
}