diff --git a/clippy_lints/src/manual_unwrap_or.rs b/clippy_lints/src/manual_unwrap_or.rs index 203f018a9e26..615e2d5c2af4 100644 --- a/clippy_lints/src/manual_unwrap_or.rs +++ b/clippy_lints/src/manual_unwrap_or.rs @@ -1,17 +1,17 @@ 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; use if_chain::if_chain; use rustc_errors::Applicability; -use rustc_hir::{hir_id::HirId, intravisit::FnKind, Arm, Body, Expr, ExprKind, FnDecl, Pat, PatKind, StmtKind}; +use rustc_hir::{Arm, Expr, ExprKind, Pat, PatKind}; use rustc_lint::LintContext; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::lint::in_external_macro; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use rustc_span::{source_map::Span, sym}; +use rustc_span::sym; declare_clippy_lint! { /// **What it does:** @@ -44,34 +44,11 @@ declare_clippy_lint! { declare_lint_pass!(ManualUnwrapOr => [MANUAL_UNWRAP_OR]); impl LateLintPass<'_> for ManualUnwrapOr { - fn check_fn( - &mut self, - cx: &LateContext<'tcx>, - kind: FnKind<'tcx>, - _: &'tcx FnDecl<'tcx>, - body: &'tcx Body<'tcx>, - span: Span, - _: HirId, - ) { - if in_external_macro(cx.sess(), span) { + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { + if in_external_macro(cx.sess(), expr.span) || in_constant(cx, expr.hir_id) { return; } - if_chain! { - if let FnKind::ItemFn(_, _, header, _) = kind; - if !header.is_const(); - let expr = &body.value; - if let ExprKind::Block(block, _) = expr.kind; - then { - for stmt in block.stmts { - if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = &stmt.kind { - lint_manual_unwrap_or(cx, expr); - } - } - if let Some(expr) = block.expr { - lint_manual_unwrap_or(cx, expr); - } - } - } + lint_manual_unwrap_or(cx, expr); } }