Skip to content

Commit

Permalink
[unnecessary_literal_unwrap]: don't lint if binding initializer is …
Browse files Browse the repository at this point in the history
…expn
  • Loading branch information
y21 committed Jul 5, 2023
1 parent ba3bd8f commit 6868c0a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions clippy_lints/src/methods/unnecessary_literal_unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ pub(super) fn check(
args: &[hir::Expr<'_>],
) {
let init = clippy_utils::expr_or_init(cx, recv);
if init.span.from_expansion() {
// don't lint if the receiver or binding initializer comes from a macro
// (e.g. `let x = option_env!(..); x.unwrap()`)
return;
}

let (constructor, call_args, ty) = if let hir::ExprKind::Call(call, call_args) = init.kind {
let Some(qpath) = call.qpath_opt() else { return };
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/unnecessary_literal_unwrap.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ fn unwrap_methods_result() {
1;
}

fn unwrap_from_binding() {
macro_rules! from_macro {
() => {
Some("")
};
}
let val = from_macro!();
let _ = val.unwrap_or("");
}

fn main() {
unwrap_option_some();
unwrap_option_none();
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/unnecessary_literal_unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ fn unwrap_methods_result() {
Ok::<_, ()>(1).unwrap_or_else(|_| 2);
}

fn unwrap_from_binding() {
macro_rules! from_macro {
() => {
Some("")
};
}
let val = from_macro!();
let _ = val.unwrap_or("");
}

fn main() {
unwrap_option_some();
unwrap_option_none();
Expand Down

0 comments on commit 6868c0a

Please sign in to comment.