summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/try_err.fixed
blob: 9e77dcd8731642567072e5ff8ac0a8a12db38cd5 (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
// run-rustfix
// aux-build:macro_rules.rs

#![deny(clippy::try_err)]

#[macro_use]
extern crate macro_rules;

use std::io;
use std::task::Poll;

// Tests that a simple case works
// Should flag `Err(err)?`
pub fn basic_test() -> Result<i32, i32> {
    let err: i32 = 1;
    // To avoid warnings during rustfix
    if true {
        return Err(err);
    }
    Ok(0)
}

// Tests that `.into()` is added when appropriate
pub fn into_test() -> Result<i32, i32> {
    let err: u8 = 1;
    // To avoid warnings during rustfix
    if true {
        return Err(err.into());
    }
    Ok(0)
}

// Tests that tries in general don't trigger the error
pub fn negative_test() -> Result<i32, i32> {
    Ok(nested_error()? + 1)
}

// Tests that `.into()` isn't added when the error type
// matches the surrounding closure's return type, even
// when it doesn't match the surrounding function's.
pub fn closure_matches_test() -> Result<i32, i32> {
    let res: Result<i32, i8> = Some(1)
        .into_iter()
        .map(|i| {
            let err: i8 = 1;
            // To avoid warnings during rustfix
            if true {
                return Err(err);
            }
            Ok(i)
        })
        .next()
        .unwrap();

    Ok(res?)
}

// Tests that `.into()` isn't added when the error type
// doesn't match the surrounding closure's return type.
pub fn closure_into_test() -> Result<i32, i32> {
    let res: Result<i32, i16> = Some(1)
        .into_iter()
        .map(|i| {
            let err: i8 = 1;
            // To avoid warnings during rustfix
            if true {
                return Err(err.into());
            }
            Ok(i)
        })
        .next()
        .unwrap();

    Ok(res?)
}

fn nested_error() -> Result<i32, i32> {
    Ok(1)
}

fn main() {
    basic_test().unwrap();
    into_test().unwrap();
    negative_test().unwrap();
    closure_matches_test().unwrap();
    closure_into_test().unwrap();

    // We don't want to lint in external macros
    try_err!();
}

macro_rules! bar {
    () => {
        String::from("aasdfasdfasdfa")
    };
}

macro_rules! foo {
    () => {
        bar!()
    };
}

pub fn macro_inside(fail: bool) -> Result<i32, String> {
    if fail {
        return Err(foo!());
    }
    Ok(0)
}

pub fn poll_write(n: usize) -> Poll<io::Result<usize>> {
    if n == 0 {
        return Poll::Ready(Err(io::ErrorKind::WriteZero.into()))
    } else if n == 1 {
        return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidInput, "error")))
    };

    Poll::Ready(Ok(n))
}

pub fn poll_next(ready: bool) -> Poll<Option<io::Result<()>>> {
    if !ready {
        return Poll::Ready(Some(Err(io::ErrorKind::NotFound.into())))
    }

    Poll::Ready(None)
}