1
- use clippy_utils:: diagnostics:: { span_lint_and_sugg , span_lint_and_then} ;
1
+ use clippy_utils:: diagnostics:: span_lint_and_then;
2
2
use clippy_utils:: source:: snippet_with_applicability;
3
3
use clippy_utils:: ty:: { is_copy, is_type_diagnostic_item} ;
4
4
use clippy_utils:: { is_expr_untyped_identity_function, is_mutable, is_trait_method, path_to_local_with_projections} ;
@@ -27,48 +27,46 @@ pub(super) fn check(
27
27
&& is_expr_untyped_identity_function ( cx, map_arg)
28
28
&& let Some ( call_span) = expr. span . trim_start ( caller. span )
29
29
{
30
- let main_sugg = ( call_span, String :: new ( ) ) ;
31
- let mut app = if is_copy ( cx, caller_ty) {
32
- // there is technically a behavioral change here for `Copy` iterators, where
33
- // `iter.map(|x| x).next()` would mutate a temporary copy of the iterator and
34
- // changing it to `iter.next()` mutates iter directly
35
- Applicability :: Unspecified
36
- } else {
37
- Applicability :: MachineApplicable
38
- } ;
30
+ span_lint_and_then ( cx, MAP_IDENTITY , call_span, MSG , |diag| {
31
+ let main_sugg = ( call_span, String :: new ( ) ) ;
32
+ let mut app = if is_copy ( cx, caller_ty) {
33
+ // there is technically a behavioral change here for `Copy` iterators, where
34
+ // `iter.map(|x| x).next()` would mutate a temporary copy of the iterator and
35
+ // changing it to `iter.next()` mutates iter directly
36
+ Applicability :: Unspecified
37
+ } else {
38
+ Applicability :: MachineApplicable
39
+ } ;
39
40
40
- let needs_to_be_mutable = cx. typeck_results ( ) . expr_ty_adjusted ( expr) . is_mutable_ptr ( ) ;
41
- if needs_to_be_mutable && !is_mutable ( cx, caller) {
42
- if let Some ( hir_id) = path_to_local_with_projections ( caller)
43
- && let Node :: Pat ( pat) = cx. tcx . hir_node ( hir_id)
44
- && let PatKind :: Binding ( _, _, ident, _) = pat. kind
45
- {
46
- // We can reach the binding -- suggest making it mutable
47
- let suggs = vec ! [ main_sugg, ( ident. span. shrink_to_lo( ) , String :: from( "mut " ) ) ] ;
41
+ let needs_to_be_mutable = cx. typeck_results ( ) . expr_ty_adjusted ( expr) . is_mutable_ptr ( ) ;
42
+ if needs_to_be_mutable && !is_mutable ( cx, caller) {
43
+ if let Some ( hir_id) = path_to_local_with_projections ( caller)
44
+ && let Node :: Pat ( pat) = cx. tcx . hir_node ( hir_id)
45
+ && let PatKind :: Binding ( _, _, ident, _) = pat. kind
46
+ {
47
+ // We can reach the binding -- suggest making it mutable
48
+ let suggs = vec ! [ main_sugg, ( ident. span. shrink_to_lo( ) , String :: from( "mut " ) ) ] ;
48
49
49
- let ident = snippet_with_applicability ( cx. sess ( ) , ident. span , "_" , & mut app) ;
50
+ let ident = snippet_with_applicability ( cx. sess ( ) , ident. span , "_" , & mut app) ;
50
51
51
- span_lint_and_then ( cx, MAP_IDENTITY , call_span, MSG , |diag| {
52
52
diag. multipart_suggestion (
53
53
format ! ( "remove the call to `{name}`, and make `{ident}` mutable" ) ,
54
54
suggs,
55
55
app,
56
56
) ;
57
- } ) ;
58
- } else {
59
- // If we can't make the binding mutable, prevent the suggestion from being automatically applied,
60
- // and add a complementary help message.
61
- app = Applicability :: Unspecified ;
62
-
63
- let method_requiring_mut = if let Node :: Expr ( expr) = cx. tcx . parent_hir_node ( expr. hir_id )
64
- && let ExprKind :: MethodCall ( method, ..) = expr. kind
65
- {
66
- Some ( method. ident )
67
57
} else {
68
- None
69
- } ;
58
+ // If we can't make the binding mutable, prevent the suggestion from being automatically applied,
59
+ // and add a complementary help message.
60
+ app = Applicability :: Unspecified ;
61
+
62
+ let method_requiring_mut = if let Node :: Expr ( expr) = cx. tcx . parent_hir_node ( expr. hir_id )
63
+ && let ExprKind :: MethodCall ( method, ..) = expr. kind
64
+ {
65
+ Some ( method. ident )
66
+ } else {
67
+ None
68
+ } ;
70
69
71
- span_lint_and_then ( cx, MAP_IDENTITY , call_span, MSG , |diag| {
72
70
diag. span_suggestion ( main_sugg. 0 , format ! ( "remove the call to `{name}`" ) , main_sugg. 1 , app) ;
73
71
74
72
let note = if let Some ( method_requiring_mut) = method_requiring_mut {
@@ -77,18 +75,10 @@ pub(super) fn check(
77
75
"this must be made mutable" . to_string ( )
78
76
} ;
79
77
diag. span_note ( caller. span , note) ;
80
- } ) ;
78
+ }
79
+ } else {
80
+ diag. span_suggestion ( main_sugg. 0 , format ! ( "remove the call to `{name}`" ) , main_sugg. 1 , app) ;
81
81
}
82
- } else {
83
- span_lint_and_sugg (
84
- cx,
85
- MAP_IDENTITY ,
86
- main_sugg. 0 ,
87
- MSG ,
88
- format ! ( "remove the call to `{name}`" ) ,
89
- main_sugg. 1 ,
90
- app,
91
- ) ;
92
- }
82
+ } ) ;
93
83
}
94
84
}
0 commit comments