Skip to content

Commit 507d1c2

Browse files
committed
Auto merge of #11110 - y21:unnecessary_literal_unwrap_ignore_expn, r=Jarcho
[`unnecessary_literal_unwrap`]: don't lint if binding initializer comes from expansion Fixes #11109 changelog: [`unnecessary_literal_unwrap`]: don't lint if binding initializer comes from expansion
2 parents ebd8d31 + 6868c0a commit 507d1c2

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

clippy_lints/src/methods/unnecessary_literal_unwrap.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ pub(super) fn check(
2929
args: &[hir::Expr<'_>],
3030
) {
3131
let init = clippy_utils::expr_or_init(cx, recv);
32+
if init.span.from_expansion() {
33+
// don't lint if the receiver or binding initializer comes from a macro
34+
// (e.g. `let x = option_env!(..); x.unwrap()`)
35+
return;
36+
}
3237

3338
let (constructor, call_args, ty) = if let hir::ExprKind::Call(call, call_args) = init.kind {
3439
let Some(qpath) = call.qpath_opt() else { return };

tests/ui/unnecessary_literal_unwrap.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ fn unwrap_methods_result() {
6868
1;
6969
}
7070

71+
fn unwrap_from_binding() {
72+
macro_rules! from_macro {
73+
() => {
74+
Some("")
75+
};
76+
}
77+
let val = from_macro!();
78+
let _ = val.unwrap_or("");
79+
}
80+
7181
fn main() {
7282
unwrap_option_some();
7383
unwrap_option_none();

tests/ui/unnecessary_literal_unwrap.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ fn unwrap_methods_result() {
6868
Ok::<_, ()>(1).unwrap_or_else(|_| 2);
6969
}
7070

71+
fn unwrap_from_binding() {
72+
macro_rules! from_macro {
73+
() => {
74+
Some("")
75+
};
76+
}
77+
let val = from_macro!();
78+
let _ = val.unwrap_or("");
79+
}
80+
7181
fn main() {
7282
unwrap_option_some();
7383
unwrap_option_none();

0 commit comments

Comments
 (0)