Skip to content

Commit 124e68b

Browse files
committed
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.
2 parents 014230c + c27f52d commit 124e68b

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

clippy_lints/src/manual_unwrap_or_default.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use rustc_session::declare_lint_pass;
77
use rustc_span::sym;
88

99
use clippy_utils::diagnostics::span_lint_and_sugg;
10-
use clippy_utils::is_default_equivalent;
1110
use clippy_utils::source::snippet_opt;
1211
use clippy_utils::ty::implements_trait;
12+
use clippy_utils::{in_constant, is_default_equivalent};
1313

1414
declare_clippy_lint! {
1515
/// ### What it does
@@ -172,7 +172,7 @@ fn handle_if_let<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
172172

173173
impl<'tcx> LateLintPass<'tcx> for ManualUnwrapOrDefault {
174174
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
175-
if expr.span.from_expansion() {
175+
if expr.span.from_expansion() || in_constant(cx, expr.hir_id) {
176176
return;
177177
}
178178
if !handle_match(cx, expr) {

tests/ui/manual_unwrap_or_default.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ unsafe fn no_deref_ptr(a: Option<i32>, b: *const Option<i32>) -> i32 {
2626
_ => 0,
2727
}
2828
}
29+
30+
const fn issue_12568(opt: Option<bool>) -> bool {
31+
match opt {
32+
Some(s) => s,
33+
None => false,
34+
}
35+
}

tests/ui/manual_unwrap_or_default.rs

+7
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,10 @@ unsafe fn no_deref_ptr(a: Option<i32>, b: *const Option<i32>) -> i32 {
5050
_ => 0,
5151
}
5252
}
53+
54+
const fn issue_12568(opt: Option<bool>) -> bool {
55+
match opt {
56+
Some(s) => s,
57+
None => false,
58+
}
59+
}

0 commit comments

Comments
 (0)