Auto merge of #6917 - MysteryJump:fix-manual-unwrap-or-const-fn, r=giraffate

Fix false-positive `manual_unwrap_or` inside const fn

Fixes #6898

changelog:  Fix false-positive for manual_unwrap_or in const fn.
This commit is contained in:
bors 2021-03-17 04:34:15 +00:00
commit 56161b2982
3 changed files with 32 additions and 2 deletions

View File

@ -1,6 +1,6 @@
use crate::consts::constant_simple;
use crate::utils;
use crate::utils::{path_to_local_id, sugg};
use crate::utils::{in_constant, path_to_local_id, sugg};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::{indent_of, reindent_multiline, snippet_opt};
use clippy_utils::ty::is_type_diagnostic_item;
@ -45,7 +45,7 @@
impl LateLintPass<'_> for ManualUnwrapOr {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if in_external_macro(cx.sess(), expr.span) {
if in_external_macro(cx.sess(), expr.span) || in_constant(cx, expr.hir_id) {
return;
}
lint_manual_unwrap_or(cx, expr);

View File

@ -136,4 +136,19 @@ fn result_unwrap_or() {
};
}
// don't lint in const fn
const fn const_fn_option_unwrap_or() {
match Some(1) {
Some(s) => s,
None => 0,
};
}
const fn const_fn_result_unwrap_or() {
match Ok::<&str, &str>("Alice") {
Ok(s) => s,
Err(_) => "Bob",
};
}
fn main() {}

View File

@ -175,4 +175,19 @@ fn method(self) -> Option<i32> {
};
}
// don't lint in const fn
const fn const_fn_option_unwrap_or() {
match Some(1) {
Some(s) => s,
None => 0,
};
}
const fn const_fn_result_unwrap_or() {
match Ok::<&str, &str>("Alice") {
Ok(s) => s,
Err(_) => "Bob",
};
}
fn main() {}