Skip to content

Commit 56161b2

Browse files
committed
Auto merge of rust-lang#6917 - MysteryJump:fix-manual-unwrap-or-const-fn, r=giraffate
Fix false-positive `manual_unwrap_or` inside const fn Fixes rust-lang#6898 changelog: Fix false-positive for manual_unwrap_or in const fn.
2 parents 5b3e61d + 02ceeb5 commit 56161b2

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

clippy_lints/src/manual_unwrap_or.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::consts::constant_simple;
22
use crate::utils;
3-
use crate::utils::{path_to_local_id, sugg};
3+
use crate::utils::{in_constant, path_to_local_id, sugg};
44
use clippy_utils::diagnostics::span_lint_and_sugg;
55
use clippy_utils::source::{indent_of, reindent_multiline, snippet_opt};
66
use clippy_utils::ty::is_type_diagnostic_item;
@@ -45,7 +45,7 @@ declare_lint_pass!(ManualUnwrapOr => [MANUAL_UNWRAP_OR]);
4545

4646
impl LateLintPass<'_> for ManualUnwrapOr {
4747
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
48-
if in_external_macro(cx.sess(), expr.span) {
48+
if in_external_macro(cx.sess(), expr.span) || in_constant(cx, expr.hir_id) {
4949
return;
5050
}
5151
lint_manual_unwrap_or(cx, expr);

tests/ui/manual_unwrap_or.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,19 @@ fn result_unwrap_or() {
136136
};
137137
}
138138

139+
// don't lint in const fn
140+
const fn const_fn_option_unwrap_or() {
141+
match Some(1) {
142+
Some(s) => s,
143+
None => 0,
144+
};
145+
}
146+
147+
const fn const_fn_result_unwrap_or() {
148+
match Ok::<&str, &str>("Alice") {
149+
Ok(s) => s,
150+
Err(_) => "Bob",
151+
};
152+
}
153+
139154
fn main() {}

tests/ui/manual_unwrap_or.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,19 @@ fn result_unwrap_or() {
175175
};
176176
}
177177

178+
// don't lint in const fn
179+
const fn const_fn_option_unwrap_or() {
180+
match Some(1) {
181+
Some(s) => s,
182+
None => 0,
183+
};
184+
}
185+
186+
const fn const_fn_result_unwrap_or() {
187+
match Ok::<&str, &str>("Alice") {
188+
Ok(s) => s,
189+
Err(_) => "Bob",
190+
};
191+
}
192+
178193
fn main() {}

0 commit comments

Comments
 (0)