about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-03-28 02:00:07 +0000
committerbors <bors@rust-lang.org>2024-03-28 02:00:07 +0000
commit124e68bef8be61aa151ff33bea325c832728146f (patch)
tree89676f0cf20eb511f92a32e1f794c252b03af9b2
parent014230ce1698cd2eed1916129f84e86130ef693b (diff)
parentc27f52d611928ed0bb11665e03de21484c2061aa (diff)
downloadrust-124e68bef8be61aa151ff33bea325c832728146f.tar.gz
rust-124e68bef8be61aa151ff33bea325c832728146f.zip
Auto merge of #12570 - J-ZhengLi:issue12569, r=Jarcho
allow [`manual_unwrap_or_default`] in const function

closes: #12568

---

changelog: allow [`manual_unwrap_or_default`] in const function

This is a small fix, I was originally decided to fix it along with `#12568` but there are some problems needs to be addressed (which is why my branch is called `issue12569` 😆 ), so I decide to open a separated PR to fix them one at a time.
-rw-r--r--clippy_lints/src/manual_unwrap_or_default.rs4
-rw-r--r--tests/ui/manual_unwrap_or_default.fixed7
-rw-r--r--tests/ui/manual_unwrap_or_default.rs7
3 files changed, 16 insertions, 2 deletions
diff --git a/clippy_lints/src/manual_unwrap_or_default.rs b/clippy_lints/src/manual_unwrap_or_default.rs
index fc6b0d6f9f6..8f189cf6e09 100644
--- a/clippy_lints/src/manual_unwrap_or_default.rs
+++ b/clippy_lints/src/manual_unwrap_or_default.rs
@@ -7,9 +7,9 @@ use rustc_session::declare_lint_pass;
 use rustc_span::sym;
 
 use clippy_utils::diagnostics::span_lint_and_sugg;
-use clippy_utils::is_default_equivalent;
 use clippy_utils::source::snippet_opt;
 use clippy_utils::ty::implements_trait;
+use clippy_utils::{in_constant, is_default_equivalent};
 
 declare_clippy_lint! {
     /// ### What it does
@@ -172,7 +172,7 @@ fn handle_if_let<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
 
 impl<'tcx> LateLintPass<'tcx> for ManualUnwrapOrDefault {
     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
-        if expr.span.from_expansion() {
+        if expr.span.from_expansion() || in_constant(cx, expr.hir_id) {
             return;
         }
         if !handle_match(cx, expr) {
diff --git a/tests/ui/manual_unwrap_or_default.fixed b/tests/ui/manual_unwrap_or_default.fixed
index b24967242eb..182c9b317b6 100644
--- a/tests/ui/manual_unwrap_or_default.fixed
+++ b/tests/ui/manual_unwrap_or_default.fixed
@@ -26,3 +26,10 @@ unsafe fn no_deref_ptr(a: Option<i32>, b: *const Option<i32>) -> i32 {
         _ => 0,
     }
 }
+
+const fn issue_12568(opt: Option<bool>) -> bool {
+    match opt {
+        Some(s) => s,
+        None => false,
+    }
+}
diff --git a/tests/ui/manual_unwrap_or_default.rs b/tests/ui/manual_unwrap_or_default.rs
index ed5e54b4e14..e8eaddc1d14 100644
--- a/tests/ui/manual_unwrap_or_default.rs
+++ b/tests/ui/manual_unwrap_or_default.rs
@@ -50,3 +50,10 @@ unsafe fn no_deref_ptr(a: Option<i32>, b: *const Option<i32>) -> i32 {
         _ => 0,
     }
 }
+
+const fn issue_12568(opt: Option<bool>) -> bool {
+    match opt {
+        Some(s) => s,
+        None => false,
+    }
+}