Skip to content

Commit

Permalink
Fix a manual_unwrap_or FP with deref coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
giraffate committed May 17, 2021
1 parent 48dad26 commit ebf065e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clippy_lints/src/manual_unwrap_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rustc_hir::{Arm, Expr, ExprKind, PatKind};
use rustc_lint::LintContext;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::adjustment::Adjust;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;

Expand Down Expand Up @@ -87,6 +88,8 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if let PatKind::Binding(_, binding_hir_id, ..) = unwrap_pat.kind;
if path_to_local_id(unwrap_arm.body, binding_hir_id);
if !contains_return_break_continue_macro(or_arm.body);
if !cx.typeck_results().expr_adjustments(unwrap_arm.body).iter()
.any(|a| matches!(a.kind, Adjust::Deref(Some(..))));
then {
Some(or_arm)
} else {
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/manual_unwrap_or.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,12 @@ mod issue6965 {
}
}

use std::rc::Rc;
fn format_name(name: Option<&Rc<str>>) -> &str {
match name {
None => "<anon>",
Some(name) => name,
}
}

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/manual_unwrap_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,12 @@ mod issue6965 {
}
}

use std::rc::Rc;
fn format_name(name: Option<&Rc<str>>) -> &str {
match name {
None => "<anon>",
Some(name) => name,
}
}

fn main() {}

0 comments on commit ebf065e

Please sign in to comment.