about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/needless_match.fixed
blob: ece18ad737fdad5264a22085a2dcfb94003a83b8 (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
// run-rustfix
#![warn(clippy::needless_match)]
#![allow(clippy::manual_map)]
#![allow(dead_code)]

#[derive(Clone, Copy)]
enum Choice {
    A,
    B,
    C,
    D,
}

#[allow(unused_mut)]
fn useless_match() {
    let mut i = 10;
    let _: i32 = i;
    let _: i32 = i;
    let mut _i_mut = i;

    let s = "test";
    let _: &str = s;
}

fn custom_type_match(se: Choice) {
    let _: Choice = se;
    // Don't trigger
    let _: Choice = match se {
        Choice::A => Choice::A,
        Choice::B => Choice::B,
        _ => Choice::C,
    };
    // Mingled, don't trigger
    let _: Choice = match se {
        Choice::A => Choice::B,
        Choice::B => Choice::C,
        Choice::C => Choice::D,
        Choice::D => Choice::A,
    };
}

fn option_match(x: Option<i32>) {
    let _: Option<i32> = x;
    // Don't trigger, this is the case for manual_map_option
    let _: Option<i32> = match x {
        Some(a) => Some(-a),
        None => None,
    };
}

fn func_ret_err<T>(err: T) -> Result<i32, T> {
    Err(err)
}

fn result_match() {
    let _: Result<i32, i32> = Ok(1);
    let _: Result<i32, i32> = func_ret_err(0_i32);
}

fn if_let_option() -> Option<i32> {
    Some(1)
}

fn if_let_result(x: Result<(), i32>) {
    let _: Result<(), i32> = x;
    let _: Result<(), i32> = x;
    // Input type mismatch, don't trigger
    let _: Result<(), i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
}

fn if_let_custom_enum(x: Choice) {
    let _: Choice = x;
    // Don't trigger
    let _: Choice = if let Choice::A = x {
        Choice::A
    } else if true {
        Choice::B
    } else {
        x
    };
}

fn main() {}