1
1
use clippy_utils:: diagnostics:: span_lint_and_then;
2
2
use clippy_utils:: get_enclosing_block;
3
- use clippy_utils:: source:: snippet ;
3
+ use clippy_utils:: source:: snippet_with_context ;
4
4
use clippy_utils:: ty:: { implements_trait, is_copy} ;
5
5
use rustc_errors:: Applicability ;
6
6
use rustc_hir:: def:: Res ;
@@ -61,12 +61,13 @@ pub(crate) fn check<'tcx>(
61
61
e. span ,
62
62
"needlessly taken reference of both operands" ,
63
63
|diag| {
64
- let lsnip = snippet ( cx, l. span , "..." ) . to_string ( ) ;
65
- let rsnip = snippet ( cx, r. span , "..." ) . to_string ( ) ;
64
+ let mut applicability = Applicability :: MachineApplicable ;
65
+ let ( lsnip, _) = snippet_with_context ( cx, l. span , e. span . ctxt ( ) , "..." , & mut applicability) ;
66
+ let ( rsnip, _) = snippet_with_context ( cx, r. span , e. span . ctxt ( ) , "..." , & mut applicability) ;
66
67
diag. multipart_suggestion (
67
68
"use the values directly" ,
68
- vec ! [ ( left. span, lsnip) , ( right. span, rsnip) ] ,
69
- Applicability :: MachineApplicable ,
69
+ vec ! [ ( left. span, lsnip. to_string ( ) ) , ( right. span, rsnip. to_string ( ) ) ] ,
70
+ applicability ,
70
71
) ;
71
72
} ,
72
73
) ;
@@ -80,13 +81,9 @@ pub(crate) fn check<'tcx>(
80
81
e. span ,
81
82
"needlessly taken reference of left operand" ,
82
83
|diag| {
83
- let lsnip = snippet ( cx, l. span , "..." ) . to_string ( ) ;
84
- diag. span_suggestion (
85
- left. span ,
86
- "use the left value directly" ,
87
- lsnip,
88
- Applicability :: MachineApplicable ,
89
- ) ;
84
+ let mut applicability = Applicability :: MachineApplicable ;
85
+ let ( lsnip, _) = snippet_with_context ( cx, l. span , e. span . ctxt ( ) , "..." , & mut applicability) ;
86
+ diag. span_suggestion ( left. span , "use the left value directly" , lsnip, applicability) ;
90
87
} ,
91
88
) ;
92
89
} else if !lcpy
@@ -99,7 +96,8 @@ pub(crate) fn check<'tcx>(
99
96
e. span ,
100
97
"needlessly taken reference of right operand" ,
101
98
|diag| {
102
- let rsnip = snippet ( cx, r. span , "..." ) . to_string ( ) ;
99
+ let mut applicability = Applicability :: MachineApplicable ;
100
+ let ( rsnip, _) = snippet_with_context ( cx, r. span , e. span . ctxt ( ) , "..." , & mut applicability) ;
103
101
diag. span_suggestion (
104
102
right. span ,
105
103
"use the right value directly" ,
@@ -131,7 +129,8 @@ pub(crate) fn check<'tcx>(
131
129
e. span ,
132
130
"needlessly taken reference of left operand" ,
133
131
|diag| {
134
- let lsnip = snippet ( cx, l. span , "..." ) . to_string ( ) ;
132
+ let mut applicability = Applicability :: MachineApplicable ;
133
+ let ( lsnip, _) = snippet_with_context ( cx, l. span , e. span . ctxt ( ) , "..." , & mut applicability) ;
135
134
diag. span_suggestion (
136
135
left. span ,
137
136
"use the left value directly" ,
@@ -158,7 +157,8 @@ pub(crate) fn check<'tcx>(
158
157
&& implements_trait ( cx, cx. typeck_results ( ) . expr_ty ( left) , trait_id, & [ rty. into ( ) ] )
159
158
{
160
159
span_lint_and_then ( cx, OP_REF , e. span , "taken reference of right operand" , |diag| {
161
- let rsnip = snippet ( cx, r. span , "..." ) . to_string ( ) ;
160
+ let mut applicability = Applicability :: MachineApplicable ;
161
+ let ( rsnip, _) = snippet_with_context ( cx, r. span , e. span . ctxt ( ) , "..." , & mut applicability) ;
162
162
diag. span_suggestion (
163
163
right. span ,
164
164
"use the right value directly" ,
0 commit comments