2 // aux-build:macro_rules.rs
4 #![deny(clippy::try_err)]
5 #![allow(clippy::unnecessary_wraps, clippy::needless_question_mark)]
8 extern crate macro_rules;
13 // Tests that a simple case works
14 // Should flag `Err(err)?`
15 pub fn basic_test() -> Result<i32, i32> {
17 // To avoid warnings during rustfix
24 // Tests that `.into()` is added when appropriate
25 pub fn into_test() -> Result<i32, i32> {
27 // To avoid warnings during rustfix
34 // Tests that tries in general don't trigger the error
35 pub fn negative_test() -> Result<i32, i32> {
36 Ok(nested_error()? + 1)
39 // Tests that `.into()` isn't added when the error type
40 // matches the surrounding closure's return type, even
41 // when it doesn't match the surrounding function's.
42 pub fn closure_matches_test() -> Result<i32, i32> {
43 let res: Result<i32, i8> = Some(1)
47 // To avoid warnings during rustfix
59 // Tests that `.into()` isn't added when the error type
60 // doesn't match the surrounding closure's return type.
61 pub fn closure_into_test() -> Result<i32, i32> {
62 let res: Result<i32, i16> = Some(1)
66 // To avoid warnings during rustfix
78 fn nested_error() -> Result<i32, i32> {
82 // Bad suggestion when in macro (see #6242)
83 macro_rules! try_validation {
92 macro_rules! ret_one {
98 macro_rules! try_validation_in_macro {
102 Err(_) => Err(ret_one!())?,
107 fn calling_macro() -> Result<i32, i32> {
109 try_validation!(Ok::<_, i32>(5));
110 // `Err` arg is another macro
111 try_validation_in_macro!(Ok::<_, i32>(5));
116 basic_test().unwrap();
117 into_test().unwrap();
118 negative_test().unwrap();
119 closure_matches_test().unwrap();
120 closure_into_test().unwrap();
121 calling_macro().unwrap();
123 // We don't want to lint in external macros
129 String::from("aasdfasdfasdfa")
139 pub fn macro_inside(fail: bool) -> Result<i32, String> {
146 pub fn poll_write(n: usize) -> Poll<io::Result<usize>> {
148 Err(io::ErrorKind::WriteZero)?
150 Err(io::Error::new(io::ErrorKind::InvalidInput, "error"))?
156 pub fn poll_next(ready: bool) -> Poll<Option<io::Result<()>>> {
158 Err(io::ErrorKind::NotFound)?
164 // Tests that `return` is not duplicated
165 pub fn try_return(x: bool) -> Result<i32, i32> {