2023-07-27 08:23:04 -05:00
|
|
|
//@aux-build:proc_macros.rs
|
2024-07-15 23:54:15 -05:00
|
|
|
#![feature(try_blocks)]
|
2019-06-24 20:28:46 -05:00
|
|
|
#![deny(clippy::try_err)]
|
2023-06-26 00:44:17 -05:00
|
|
|
#![allow(
|
|
|
|
clippy::unnecessary_wraps,
|
|
|
|
clippy::needless_question_mark,
|
2023-07-12 21:48:22 -05:00
|
|
|
clippy::needless_return_with_question_mark
|
2023-06-26 00:44:17 -05:00
|
|
|
)]
|
2019-06-24 20:28:46 -05:00
|
|
|
|
2023-03-07 08:40:55 -06:00
|
|
|
extern crate proc_macros;
|
|
|
|
use proc_macros::{external, inline_macros};
|
2019-10-24 00:52:01 -05:00
|
|
|
|
2020-08-01 19:00:00 -05:00
|
|
|
use std::io;
|
|
|
|
use std::task::Poll;
|
|
|
|
|
2019-06-24 20:28:46 -05:00
|
|
|
// Tests that a simple case works
|
|
|
|
// Should flag `Err(err)?`
|
|
|
|
pub fn basic_test() -> Result<i32, i32> {
|
|
|
|
let err: i32 = 1;
|
2019-06-30 17:28:12 -05:00
|
|
|
// To avoid warnings during rustfix
|
|
|
|
if true {
|
2019-06-24 20:28:46 -05:00
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that `.into()` is added when appropriate
|
|
|
|
pub fn into_test() -> Result<i32, i32> {
|
|
|
|
let err: u8 = 1;
|
2019-06-30 17:28:12 -05:00
|
|
|
// To avoid warnings during rustfix
|
|
|
|
if true {
|
2019-06-24 20:28:46 -05:00
|
|
|
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> {
|
2019-06-30 17:28:12 -05:00
|
|
|
let res: Result<i32, i8> = Some(1)
|
|
|
|
.into_iter()
|
2019-06-24 20:28:46 -05:00
|
|
|
.map(|i| {
|
|
|
|
let err: i8 = 1;
|
2019-06-30 17:28:12 -05:00
|
|
|
// To avoid warnings during rustfix
|
|
|
|
if true {
|
2019-06-24 20:28:46 -05:00
|
|
|
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> {
|
2019-06-30 17:28:12 -05:00
|
|
|
let res: Result<i32, i16> = Some(1)
|
|
|
|
.into_iter()
|
2019-06-24 20:28:46 -05:00
|
|
|
.map(|i| {
|
|
|
|
let err: i8 = 1;
|
2019-06-30 17:28:12 -05:00
|
|
|
// To avoid warnings during rustfix
|
|
|
|
if true {
|
2019-06-24 20:28:46 -05:00
|
|
|
return Err(err.into());
|
|
|
|
}
|
|
|
|
Ok(i)
|
|
|
|
})
|
|
|
|
.next()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Ok(res?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nested_error() -> Result<i32, i32> {
|
|
|
|
Ok(1)
|
|
|
|
}
|
|
|
|
|
2023-03-07 08:40:55 -06:00
|
|
|
#[inline_macros]
|
|
|
|
fn calling_macro() -> Result<i32, i32> {
|
|
|
|
// macro
|
|
|
|
inline!(
|
|
|
|
match $(Ok::<_, i32>(5)) {
|
2020-11-02 10:59:47 -06:00
|
|
|
Ok(_) => 0,
|
|
|
|
Err(_) => return Err(1),
|
|
|
|
}
|
2023-03-07 08:40:55 -06:00
|
|
|
);
|
|
|
|
// `Err` arg is another macro
|
|
|
|
inline!(
|
|
|
|
match $(Ok::<_, i32>(5)) {
|
2020-11-05 11:00:34 -06:00
|
|
|
Ok(_) => 0,
|
2023-03-07 08:40:55 -06:00
|
|
|
Err(_) => return Err(inline!(1)),
|
2020-11-05 11:00:34 -06:00
|
|
|
}
|
2023-03-07 08:40:55 -06:00
|
|
|
);
|
2020-11-02 10:59:47 -06:00
|
|
|
Ok(5)
|
|
|
|
}
|
|
|
|
|
2019-06-24 20:28:46 -05:00
|
|
|
fn main() {
|
|
|
|
basic_test().unwrap();
|
|
|
|
into_test().unwrap();
|
|
|
|
negative_test().unwrap();
|
|
|
|
closure_matches_test().unwrap();
|
|
|
|
closure_into_test().unwrap();
|
2020-11-02 10:59:47 -06:00
|
|
|
calling_macro().unwrap();
|
2019-10-24 00:52:01 -05:00
|
|
|
|
|
|
|
// We don't want to lint in external macros
|
2023-03-07 08:40:55 -06:00
|
|
|
external! {
|
|
|
|
pub fn try_err_fn() -> Result<i32, i32> {
|
|
|
|
let err: i32 = 1;
|
|
|
|
// To avoid warnings during rustfix
|
|
|
|
if true { Err(err)? } else { Ok(2) }
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 07:33:34 -05:00
|
|
|
}
|
|
|
|
|
2023-03-07 08:40:55 -06:00
|
|
|
#[inline_macros]
|
2019-08-08 07:33:34 -05:00
|
|
|
pub fn macro_inside(fail: bool) -> Result<i32, String> {
|
|
|
|
if fail {
|
2023-03-07 08:40:55 -06:00
|
|
|
return Err(inline!(inline!(String::from("aasdfasdfasdfa"))));
|
2019-08-08 07:33:34 -05:00
|
|
|
}
|
|
|
|
Ok(0)
|
|
|
|
}
|
2020-08-01 19:00:00 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2021-04-18 16:49:54 -05:00
|
|
|
|
|
|
|
// Tests that `return` is not duplicated
|
|
|
|
pub fn try_return(x: bool) -> Result<i32, i32> {
|
|
|
|
if x {
|
|
|
|
return Err(42);
|
|
|
|
}
|
|
|
|
Ok(0)
|
|
|
|
}
|
2024-07-15 23:54:15 -05:00
|
|
|
|
|
|
|
// Test that the lint is suppressed in try block.
|
|
|
|
pub fn try_block() -> Result<(), i32> {
|
|
|
|
let _: Result<_, i32> = try {
|
|
|
|
Err(1)?;
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|