summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/unnecessary_wraps.rs
blob: 63648ef5826f6e1f2c5009050393c4b884e9a735 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#![warn(clippy::unnecessary_wraps)]
#![allow(clippy::no_effect)]
#![allow(clippy::needless_return)]
#![allow(clippy::if_same_then_else)]
#![allow(dead_code)]

// should be linted
fn func1(a: bool, b: bool) -> Option<i32> {
    if a && b {
        return Some(42);
    }
    if a {
        Some(-1);
        Some(2)
    } else {
        return Some(1337);
    }
}

// should be linted
fn func2(a: bool, b: bool) -> Option<i32> {
    if a && b {
        return Some(10);
    }
    if a { Some(20) } else { Some(30) }
}

// public fns should not be linted
pub fn func3(a: bool) -> Option<i32> {
    if a { Some(1) } else { Some(1) }
}

// should not be linted
fn func4(a: bool) -> Option<i32> {
    if a { Some(1) } else { None }
}

// should be linted
fn func5() -> Option<i32> {
    Some(1)
}

// should not be linted
fn func6() -> Option<i32> {
    None
}

// should be linted
fn func7() -> Result<i32, ()> {
    Ok(1)
}

// should not be linted
fn func8(a: bool) -> Result<i32, ()> {
    if a { Ok(1) } else { Err(()) }
}

// should not be linted
fn func9(a: bool) -> Result<i32, ()> {
    Err(())
}

// should not be linted
fn func10() -> Option<()> {
    unimplemented!()
}

pub struct A;

impl A {
    // should not be linted
    pub fn func11() -> Option<i32> {
        Some(1)
    }

    // should be linted
    fn func12() -> Option<i32> {
        Some(1)
    }
}

trait B {
    // trait impls are not linted
    fn func13() -> Option<i32> {
        Some(1)
    }
}

impl B for A {
    // trait impls are not linted
    fn func13() -> Option<i32> {
        Some(0)
    }
}

fn issue_6384(s: &str) -> Option<&str> {
    Some(match s {
        "a" => "A",
        _ => return None,
    })
}

// should be linted
fn issue_6640_1(a: bool, b: bool) -> Option<()> {
    if a && b {
        return Some(());
    }
    if a {
        Some(());
        Some(())
    } else {
        return Some(());
    }
}

// should be linted
fn issue_6640_2(a: bool, b: bool) -> Result<(), i32> {
    if a && b {
        return Ok(());
    }
    if a {
        Ok(())
    } else {
        return Ok(());
    }
}

// should not be linted
fn issue_6640_3() -> Option<()> {
    if true { Some(()) } else { None }
}

// should not be linted
fn issue_6640_4() -> Result<(), ()> {
    if true { Ok(()) } else { Err(()) }
}

fn main() {
    // method calls are not linted
    func1(true, true);
    func2(true, true);
    issue_6640_1(true, true);
    issue_6640_2(true, true);
}