about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/unnecessary_map_on_constructor.rs
blob: 0cd41f2b363acb2edd830c57291b8772270f4d23 (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
#![allow(unused)]
#![warn(clippy::unnecessary_map_on_constructor)]

use std::ffi::OsStr;

fn fun(t: i32) -> i32 {
    t
}

fn notfun(e: SimpleError) -> SimpleError {
    e
}
macro_rules! expands_to_fun {
    () => {
        fun
    };
}

#[derive(Copy, Clone)]
struct SimpleError {}

type SimpleResult = std::result::Result<i32, SimpleError>;

fn main() {
    let x: i32 = 4;

    let err = SimpleError {};
    let a = Some(x);
    let b: SimpleResult = Ok(x);
    let c: SimpleResult = Err(err);

    let a = Some(x).map(fun);
    //~^ unnecessary_map_on_constructor
    let b: SimpleResult = Ok(x).map(fun);
    //~^ unnecessary_map_on_constructor
    let c: SimpleResult = Err(err).map_err(notfun);
    //~^ unnecessary_map_on_constructor

    let a = Option::Some(x).map(fun);
    //~^ unnecessary_map_on_constructor
    let b: SimpleResult = SimpleResult::Ok(x).map(fun);
    //~^ unnecessary_map_on_constructor
    let c: SimpleResult = SimpleResult::Err(err).map_err(notfun);
    //~^ unnecessary_map_on_constructor
    let b: std::result::Result<i32, SimpleError> = Ok(x).map(fun);
    //~^ unnecessary_map_on_constructor
    let c: std::result::Result<i32, SimpleError> = Err(err).map_err(notfun);
    //~^ unnecessary_map_on_constructor

    let a = Some(fun(x));
    let b: SimpleResult = Ok(fun(x));
    let c: SimpleResult = Err(notfun(err));

    // Should not trigger warning
    a.map(fun);
    b.map(fun);
    c.map_err(notfun);

    b.map_err(notfun); // Ok(_).map_err
    c.map(fun); // Err(_).map()

    option_env!("PATH").map(OsStr::new);
    Some(x).map(expands_to_fun!());
}