Skip to content

Commit

Permalink
Auto merge of rust-lang#13107 - yaxum62:i5757, r=xFrednet
Browse files Browse the repository at this point in the history
Add test for `try_err` lint within try blocks.

Fixes rust-lang#5757

Turns out the current `try_err` implementation already skips expressions inside of a try block.

When inside of a try block, `Err(_)?` is desugared to a `break` instead of normal `return` . This makes `find_return_type()` function at [this line](https://github.com/rust-lang/rust-clippy/blob/eb4d88e690c431691bc0fd8eaa9f7096ecc2a723/clippy_lints/src/matches/try_err.rs#L29) always returns `None` and skips the check.

I just added a test case for try block.

changelog: none
  • Loading branch information
bors committed Aug 3, 2024
2 parents 1ea827f + 1821def commit 0347280
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion tests/ui/try_err.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@aux-build:proc_macros.rs

#![feature(try_blocks)]
#![deny(clippy::try_err)]
#![allow(
clippy::unnecessary_wraps,
Expand Down Expand Up @@ -152,3 +152,11 @@ pub fn try_return(x: bool) -> Result<i32, i32> {
}
Ok(0)
}

// Test that the lint is suppressed in try block.
pub fn try_block() -> Result<(), i32> {
let _: Result<_, i32> = try {
Err(1)?;
};
Ok(())
}
10 changes: 9 additions & 1 deletion tests/ui/try_err.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@aux-build:proc_macros.rs

#![feature(try_blocks)]
#![deny(clippy::try_err)]
#![allow(
clippy::unnecessary_wraps,
Expand Down Expand Up @@ -152,3 +152,11 @@ pub fn try_return(x: bool) -> Result<i32, i32> {
}
Ok(0)
}

// Test that the lint is suppressed in try block.
pub fn try_block() -> Result<(), i32> {
let _: Result<_, i32> = try {
Err(1)?;
};
Ok(())
}

0 comments on commit 0347280

Please sign in to comment.