1- use crate :: utils:: {
2- is_type_diagnostic_item, match_def_path, paths, peel_hir_expr_refs, peel_mid_ty_refs_is_mutable,
3- snippet_with_applicability, span_lint_and_sugg,
1+ use crate :: {
2+ map_unit_fn:: OPTION_MAP_UNIT_FN ,
3+ matches:: MATCH_AS_REF ,
4+ utils:: {
5+ is_allowed, is_type_diagnostic_item, match_def_path, match_var, paths, peel_hir_expr_refs,
6+ peel_mid_ty_refs_is_mutable, snippet_with_applicability, span_lint_and_sugg,
7+ } ,
48} ;
59use rustc_ast:: util:: parser:: PREC_POSTFIX ;
610use rustc_errors:: Applicability ;
7- use rustc_hir:: { Arm , BindingAnnotation , Block , Expr , ExprKind , Mutability , Pat , PatKind , Path , QPath } ;
11+ use rustc_hir:: { Arm , BindingAnnotation , Block , Expr , ExprKind , Mutability , Pat , PatKind , QPath } ;
812use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
913use rustc_middle:: lint:: in_external_macro;
1014use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
@@ -37,6 +41,7 @@ declare_clippy_lint! {
3741declare_lint_pass ! ( ManualMap => [ MANUAL_MAP ] ) ;
3842
3943impl LateLintPass < ' _ > for ManualMap {
44+ #[ allow( clippy:: too_many_lines) ]
4045 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
4146 if in_external_macro ( cx. sess ( ) , expr. span ) {
4247 return ;
@@ -88,14 +93,17 @@ impl LateLintPass<'_> for ManualMap {
8893 None => return ,
8994 } ;
9095
96+ if cx. typeck_results ( ) . expr_ty ( some_expr) == cx. tcx . types . unit
97+ && !is_allowed ( cx, OPTION_MAP_UNIT_FN , expr. hir_id )
98+ {
99+ return ;
100+ }
101+
91102 // Determine which binding mode to use.
92103 let explicit_ref = some_pat. contains_explicit_ref_binding ( ) ;
93- let binding_mutability = explicit_ref. or ( if ty_ref_count != pat_ref_count {
94- Some ( ty_mutability)
95- } else {
96- None
97- } ) ;
98- let as_ref_str = match binding_mutability {
104+ let binding_ref = explicit_ref. or_else ( || ( ty_ref_count != pat_ref_count) . then ( || ty_mutability) ) ;
105+
106+ let as_ref_str = match binding_ref {
99107 Some ( Mutability :: Mut ) => ".as_mut()" ,
100108 Some ( Mutability :: Not ) => ".as_ref()" ,
101109 None => "" ,
@@ -118,6 +126,13 @@ impl LateLintPass<'_> for ManualMap {
118126 if let Some ( func) = can_pass_as_func ( cx, some_binding, some_expr) {
119127 snippet_with_applicability ( cx, func. span , ".." , & mut app) . into_owned ( )
120128 } else {
129+ if match_var ( some_expr, some_binding. name )
130+ && !is_allowed ( cx, MATCH_AS_REF , expr. hir_id )
131+ && binding_ref. is_some ( )
132+ {
133+ return ;
134+ }
135+
121136 // `ref` and `ref mut` annotations were handled earlier.
122137 let annotation = if matches ! ( annotation, BindingAnnotation :: Mutable ) {
123138 "mut "
@@ -161,10 +176,7 @@ impl LateLintPass<'_> for ManualMap {
161176fn can_pass_as_func ( cx : & LateContext < ' tcx > , binding : Ident , expr : & ' tcx Expr < ' _ > ) -> Option < & ' tcx Expr < ' tcx > > {
162177 match expr. kind {
163178 ExprKind :: Call ( func, [ arg] )
164- if matches ! ( arg. kind,
165- ExprKind :: Path ( QPath :: Resolved ( None , Path { segments: [ path] , ..} ) )
166- if path. ident == binding
167- ) && cx. typeck_results ( ) . expr_adjustments ( arg) . is_empty ( ) =>
179+ if match_var ( arg, binding. name ) && cx. typeck_results ( ) . expr_adjustments ( arg) . is_empty ( ) =>
168180 {
169181 Some ( func)
170182 } ,
0 commit comments