diff --git a/clippy_lints/src/matches/manual_unwrap_or.rs b/clippy_lints/src/matches/manual_unwrap_or.rs index 0940fc3219bb..85a08f81c2f3 100644 --- a/clippy_lints/src/matches/manual_unwrap_or.rs +++ b/clippy_lints/src/matches/manual_unwrap_or.rs @@ -35,6 +35,17 @@ pub(super) fn check_if_let<'tcx>( else_expr: &'tcx Expr<'_>, ) { let ty = cx.typeck_results().expr_ty(let_expr); + let then_ty = cx.typeck_results().expr_ty(then_expr); + // The signature is `fn unwrap_or(self: Option, default: T) -> T`. + // When `expr_adjustments(then_expr).is_empty()`, `T` should equate to `default`'s type. + // Otherwise, type error will occur. + if cx.typeck_results().expr_adjustments(then_expr).is_empty() + && let rustc_middle::ty::Adt(_did, args) = ty.kind() + && let Some(some_ty) = args.first().and_then(|arg| arg.as_type()) + && some_ty != then_ty + { + return; + } check_and_lint(cx, expr, let_pat, let_expr, then_expr, peel_blocks(else_expr), ty); } diff --git a/tests/ui/manual_unwrap_or.fixed b/tests/ui/manual_unwrap_or.fixed index 74afa00e12f2..07e4bdd483a8 100644 --- a/tests/ui/manual_unwrap_or.fixed +++ b/tests/ui/manual_unwrap_or.fixed @@ -234,4 +234,20 @@ fn implicit_deref_ref() { }; } +mod issue_13018 { + use std::collections::HashMap; + + type RefName = i32; + pub fn get(index: &HashMap>, id: usize) -> &[RefName] { + if let Some(names) = index.get(&id) { names } else { &[] } + } + + pub fn get_match(index: &HashMap>, id: usize) -> &[RefName] { + match index.get(&id) { + Some(names) => names, + None => &[], + } + } +} + fn main() {} diff --git a/tests/ui/manual_unwrap_or.rs b/tests/ui/manual_unwrap_or.rs index 2d01b8ceaaa7..cdbe51a41212 100644 --- a/tests/ui/manual_unwrap_or.rs +++ b/tests/ui/manual_unwrap_or.rs @@ -284,4 +284,20 @@ fn implicit_deref_ref() { }; } +mod issue_13018 { + use std::collections::HashMap; + + type RefName = i32; + pub fn get(index: &HashMap>, id: usize) -> &[RefName] { + if let Some(names) = index.get(&id) { names } else { &[] } + } + + pub fn get_match(index: &HashMap>, id: usize) -> &[RefName] { + match index.get(&id) { + Some(names) => names, + None => &[], + } + } +} + fn main() {}