1- use clippy_utils:: diagnostics:: { span_lint_and_sugg , span_lint_and_then} ;
1+ use clippy_utils:: diagnostics:: span_lint_and_then;
22use clippy_utils:: source:: snippet_with_applicability;
33use clippy_utils:: ty:: { is_copy, is_type_diagnostic_item} ;
44use 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(
2727 && is_expr_untyped_identity_function ( cx, map_arg)
2828 && let Some ( call_span) = expr. span . trim_start ( caller. span )
2929 {
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+ } ;
3940
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 " ) ) ] ;
4849
49- let ident = snippet_with_applicability ( cx. sess ( ) , ident. span , "_" , & mut app) ;
50+ let ident = snippet_with_applicability ( cx. sess ( ) , ident. span , "_" , & mut app) ;
5051
51- span_lint_and_then ( cx, MAP_IDENTITY , call_span, MSG , |diag| {
5252 diag. multipart_suggestion (
5353 format ! ( "remove the call to `{name}`, and make `{ident}` mutable" ) ,
5454 suggs,
5555 app,
5656 ) ;
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 )
6757 } 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+ } ;
7069
71- span_lint_and_then ( cx, MAP_IDENTITY , call_span, MSG , |diag| {
7270 diag. span_suggestion ( main_sugg. 0 , format ! ( "remove the call to `{name}`" ) , main_sugg. 1 , app) ;
7371
7472 let note = if let Some ( method_requiring_mut) = method_requiring_mut {
@@ -77,18 +75,10 @@ pub(super) fn check(
7775 "this must be made mutable" . to_string ( )
7876 } ;
7977 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) ;
8181 }
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+ } ) ;
9383 }
9484}
0 commit comments