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
+ } ,
4
8
} ;
5
9
use rustc_ast:: util:: parser:: PREC_POSTFIX ;
6
10
use 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 } ;
8
12
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
9
13
use rustc_middle:: lint:: in_external_macro;
10
14
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
@@ -37,6 +41,7 @@ declare_clippy_lint! {
37
41
declare_lint_pass ! ( ManualMap => [ MANUAL_MAP ] ) ;
38
42
39
43
impl LateLintPass < ' _ > for ManualMap {
44
+ #[ allow( clippy:: too_many_lines) ]
40
45
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
41
46
if in_external_macro ( cx. sess ( ) , expr. span ) {
42
47
return ;
@@ -88,14 +93,17 @@ impl LateLintPass<'_> for ManualMap {
88
93
None => return ,
89
94
} ;
90
95
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
+
91
102
// Determine which binding mode to use.
92
103
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 {
99
107
Some ( Mutability :: Mut ) => ".as_mut()" ,
100
108
Some ( Mutability :: Not ) => ".as_ref()" ,
101
109
None => "" ,
@@ -118,6 +126,13 @@ impl LateLintPass<'_> for ManualMap {
118
126
if let Some ( func) = can_pass_as_func ( cx, some_binding, some_expr) {
119
127
snippet_with_applicability ( cx, func. span , ".." , & mut app) . into_owned ( )
120
128
} 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
+
121
136
// `ref` and `ref mut` annotations were handled earlier.
122
137
let annotation = if matches ! ( annotation, BindingAnnotation :: Mutable ) {
123
138
"mut "
@@ -161,10 +176,7 @@ impl LateLintPass<'_> for ManualMap {
161
176
fn can_pass_as_func ( cx : & LateContext < ' tcx > , binding : Ident , expr : & ' tcx Expr < ' _ > ) -> Option < & ' tcx Expr < ' tcx > > {
162
177
match expr. kind {
163
178
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 ( ) =>
168
180
{
169
181
Some ( func)
170
182
} ,
0 commit comments