diff options
| author | y21 <30553356+y21@users.noreply.github.com> | 2023-07-05 12:30:39 +0200 |
|---|---|---|
| committer | y21 <30553356+y21@users.noreply.github.com> | 2023-07-05 13:03:34 +0200 |
| commit | 6868c0a74bb1edeff8c078bb8b09994075e826cb (patch) | |
| tree | 0e0a56af5af8f829823d99f95d5334cd4082c5c7 | |
| parent | ba3bd8f0f1255a7899d17a451c959c9307c9f854 (diff) | |
| download | rust-6868c0a74bb1edeff8c078bb8b09994075e826cb.tar.gz rust-6868c0a74bb1edeff8c078bb8b09994075e826cb.zip | |
[`unnecessary_literal_unwrap`]: don't lint if binding initializer is expn
| -rw-r--r-- | clippy_lints/src/methods/unnecessary_literal_unwrap.rs | 5 | ||||
| -rw-r--r-- | tests/ui/unnecessary_literal_unwrap.fixed | 10 | ||||
| -rw-r--r-- | tests/ui/unnecessary_literal_unwrap.rs | 10 |
3 files changed, 25 insertions, 0 deletions
diff --git a/clippy_lints/src/methods/unnecessary_literal_unwrap.rs b/clippy_lints/src/methods/unnecessary_literal_unwrap.rs index 7877f6a386c..ce93d7c3eb3 100644 --- a/clippy_lints/src/methods/unnecessary_literal_unwrap.rs +++ b/clippy_lints/src/methods/unnecessary_literal_unwrap.rs @@ -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 }; diff --git a/tests/ui/unnecessary_literal_unwrap.fixed b/tests/ui/unnecessary_literal_unwrap.fixed index 630a1bea3c8..092bf78feab 100644 --- a/tests/ui/unnecessary_literal_unwrap.fixed +++ b/tests/ui/unnecessary_literal_unwrap.fixed @@ -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(); diff --git a/tests/ui/unnecessary_literal_unwrap.rs b/tests/ui/unnecessary_literal_unwrap.rs index 14f92cb370f..b9df893d1d2 100644 --- a/tests/ui/unnecessary_literal_unwrap.rs +++ b/tests/ui/unnecessary_literal_unwrap.rs @@ -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(); |
