@@ -6,12 +6,12 @@ use clippy_utils::source::{indent_of, reindent_multiline, snippet_opt};
66use clippy_utils:: ty:: is_type_diagnostic_item;
77use if_chain:: if_chain;
88use rustc_errors:: Applicability ;
9- use rustc_hir:: { Arm , Expr , ExprKind , Pat , PatKind } ;
9+ use rustc_hir:: { hir_id :: HirId , intravisit :: FnKind , Arm , Body , Expr , ExprKind , FnDecl , Pat , PatKind , StmtKind } ;
1010use rustc_lint:: LintContext ;
1111use rustc_lint:: { LateContext , LateLintPass } ;
1212use rustc_middle:: lint:: in_external_macro;
1313use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
14- use rustc_span:: sym;
14+ use rustc_span:: { source_map :: Span , sym} ;
1515
1616declare_clippy_lint ! {
1717 /// **What it does:**
@@ -44,11 +44,34 @@ declare_clippy_lint! {
4444declare_lint_pass ! ( ManualUnwrapOr => [ MANUAL_UNWRAP_OR ] ) ;
4545
4646impl LateLintPass < ' _ > for ManualUnwrapOr {
47- fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
48- if in_external_macro ( cx. sess ( ) , expr. span ) {
47+ fn check_fn (
48+ & mut self ,
49+ cx : & LateContext < ' tcx > ,
50+ kind : FnKind < ' tcx > ,
51+ _: & ' tcx FnDecl < ' tcx > ,
52+ body : & ' tcx Body < ' tcx > ,
53+ span : Span ,
54+ _: HirId ,
55+ ) {
56+ if in_external_macro ( cx. sess ( ) , span) {
4957 return ;
5058 }
51- lint_manual_unwrap_or ( cx, expr) ;
59+ if_chain ! {
60+ if let FnKind :: ItemFn ( _, _, header, _) = kind;
61+ if !header. is_const( ) ;
62+ let expr = & body. value;
63+ if let ExprKind :: Block ( block, _) = expr. kind;
64+ then {
65+ for stmt in block. stmts {
66+ if let StmtKind :: Expr ( expr) | StmtKind :: Semi ( expr) = & stmt. kind {
67+ lint_manual_unwrap_or( cx, expr) ;
68+ }
69+ }
70+ if let Some ( expr) = block. expr {
71+ lint_manual_unwrap_or( cx, expr) ;
72+ }
73+ }
74+ }
5275 }
5376}
5477
0 commit comments