fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
3.4 KiB154 linesraw
1//@aux-build:proc_macros.rs
2
3#![deny(clippy::try_err)]
4#![allow(
5    clippy::unnecessary_wraps,
6    clippy::needless_question_mark,
7    clippy::needless_return_with_question_mark
8)]
9
10extern crate proc_macros;
11use proc_macros::{external, inline_macros};
12
13use std::io;
14use std::task::Poll;
15
16// Tests that a simple case works
17// Should flag `Err(err)?`
18pub fn basic_test() -> Result<i32, i32> {
19    let err: i32 = 1;
20    // To avoid warnings during rustfix
21    if true {
22        return Err(err);
23    }
24    Ok(0)
25}
26
27// Tests that `.into()` is added when appropriate
28pub fn into_test() -> Result<i32, i32> {
29    let err: u8 = 1;
30    // To avoid warnings during rustfix
31    if true {
32        return Err(err.into());
33    }
34    Ok(0)
35}
36
37// Tests that tries in general don't trigger the error
38pub fn negative_test() -> Result<i32, i32> {
39    Ok(nested_error()? + 1)
40}
41
42// Tests that `.into()` isn't added when the error type
43// matches the surrounding closure's return type, even
44// when it doesn't match the surrounding function's.
45pub fn closure_matches_test() -> Result<i32, i32> {
46    let res: Result<i32, i8> = Some(1)
47        .into_iter()
48        .map(|i| {
49            let err: i8 = 1;
50            // To avoid warnings during rustfix
51            if true {
52                return Err(err);
53            }
54            Ok(i)
55        })
56        .next()
57        .unwrap();
58
59    Ok(res?)
60}
61
62// Tests that `.into()` isn't added when the error type
63// doesn't match the surrounding closure's return type.
64pub fn closure_into_test() -> Result<i32, i32> {
65    let res: Result<i32, i16> = Some(1)
66        .into_iter()
67        .map(|i| {
68            let err: i8 = 1;
69            // To avoid warnings during rustfix
70            if true {
71                return Err(err.into());
72            }
73            Ok(i)
74        })
75        .next()
76        .unwrap();
77
78    Ok(res?)
79}
80
81fn nested_error() -> Result<i32, i32> {
82    Ok(1)
83}
84
85#[inline_macros]
86fn calling_macro() -> Result<i32, i32> {
87    // macro
88    inline!(
89        match $(Ok::<_, i32>(5)) {
90            Ok(_) => 0,
91            Err(_) => return Err(1),
92        }
93    );
94    // `Err` arg is another macro
95    inline!(
96        match $(Ok::<_, i32>(5)) {
97            Ok(_) => 0,
98            Err(_) => return Err(inline!(1)),
99        }
100    );
101    Ok(5)
102}
103
104fn main() {
105    basic_test().unwrap();
106    into_test().unwrap();
107    negative_test().unwrap();
108    closure_matches_test().unwrap();
109    closure_into_test().unwrap();
110    calling_macro().unwrap();
111
112    // We don't want to lint in external macros
113    external! {
114        pub fn try_err_fn() -> Result<i32, i32> {
115            let err: i32 = 1;
116            // To avoid warnings during rustfix
117            if true { Err(err)? } else { Ok(2) }
118        }
119    }
120}
121
122#[inline_macros]
123pub fn macro_inside(fail: bool) -> Result<i32, String> {
124    if fail {
125        return Err(inline!(inline!(String::from("aasdfasdfasdfa"))));
126    }
127    Ok(0)
128}
129
130pub fn poll_write(n: usize) -> Poll<io::Result<usize>> {
131    if n == 0 {
132        return Poll::Ready(Err(io::ErrorKind::WriteZero.into()))
133    } else if n == 1 {
134        return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidInput, "error")))
135    };
136
137    Poll::Ready(Ok(n))
138}
139
140pub fn poll_next(ready: bool) -> Poll<Option<io::Result<()>>> {
141    if !ready {
142        return Poll::Ready(Some(Err(io::ErrorKind::NotFound.into())))
143    }
144
145    Poll::Ready(None)
146}
147
148// Tests that `return` is not duplicated
149pub fn try_return(x: bool) -> Result<i32, i32> {
150    if x {
151        return Err(42);
152    }
153    Ok(0)
154}