unwrap_used, expect_used: accept macro result as receiver (#14575)

changelog: [`unwrap_used`, `expect_used`]: lint even when the receiver
is a macro expansion result

This also paves the way for expanding more method call lints to expanded
receivers or arguments.

Fixes rust-lang/rust-clippy#13455
This commit is contained in:
Jason Newcomb 2025-05-07 08:09:47 +00:00 committed by GitHub
commit ca78fb4031
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 105 additions and 44 deletions

View file

@ -66,3 +66,20 @@ fn main() {
SOME.expect("Still not three?");
}
}
mod with_expansion {
macro_rules! open {
($file:expr) => {
std::fs::File::open($file)
};
}
fn test(file: &str) {
use std::io::Read;
let mut s = String::new();
let _ = open!(file).unwrap(); //~ unwrap_used
let _ = open!(file).expect("can open"); //~ expect_used
let _ = open!(file).unwrap_err(); //~ unwrap_used
let _ = open!(file).expect_err("can open"); //~ expect_used
}
}

View file

@ -50,5 +50,37 @@ LL | a.expect_err("Hello error!");
|
= note: if this value is an `Ok`, it will panic
error: aborting due to 6 previous errors
error: used `unwrap()` on a `Result` value
--> tests/ui/unwrap_expect_used.rs:80:17
|
LL | let _ = open!(file).unwrap();
| ^^^^^^^^^^^^^^^^^^^^
|
= note: if this value is an `Err`, it will panic
error: used `expect()` on a `Result` value
--> tests/ui/unwrap_expect_used.rs:81:17
|
LL | let _ = open!(file).expect("can open");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: if this value is an `Err`, it will panic
error: used `unwrap_err()` on a `Result` value
--> tests/ui/unwrap_expect_used.rs:82:17
|
LL | let _ = open!(file).unwrap_err();
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: if this value is an `Ok`, it will panic
error: used `expect_err()` on a `Result` value
--> tests/ui/unwrap_expect_used.rs:83:17
|
LL | let _ = open!(file).expect_err("can open");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: if this value is an `Ok`, it will panic
error: aborting due to 10 previous errors