1
- use clippy_utils:: diagnostics:: { span_lint_and_sugg, span_lint_and_then } ;
1
+ use clippy_utils:: diagnostics:: { span_lint_and_sugg, span_lint_hir_and_then } ;
2
2
use clippy_utils:: source:: { snippet_with_applicability, snippet_with_context} ;
3
3
use clippy_utils:: sugg:: has_enclosing_paren;
4
4
use clippy_utils:: ty:: peel_mid_ty_refs;
@@ -135,6 +135,7 @@ pub struct Dereferencing {
135
135
struct StateData {
136
136
/// Span of the top level expression
137
137
span : Span ,
138
+ hir_id : HirId ,
138
139
}
139
140
140
141
enum State {
@@ -169,6 +170,8 @@ struct RefPat {
169
170
app : Applicability ,
170
171
/// All the replacements which need to be made.
171
172
replacements : Vec < ( Span , String ) > ,
173
+ /// The [`HirId`] that the lint should be emitted at.
174
+ hir_id : HirId ,
172
175
}
173
176
174
177
impl < ' tcx > LateLintPass < ' tcx > for Dereferencing {
@@ -222,7 +225,10 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
222
225
is_final_ufcs : matches ! ( expr. kind, ExprKind :: Call ( ..) ) ,
223
226
target_mut,
224
227
} ,
225
- StateData { span : expr. span } ,
228
+ StateData {
229
+ span : expr. span ,
230
+ hir_id : expr. hir_id ,
231
+ } ,
226
232
) ) ;
227
233
} ,
228
234
RefOp :: AddrOf => {
@@ -294,7 +300,10 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
294
300
required_precedence,
295
301
msg,
296
302
} ,
297
- StateData { span : expr. span } ,
303
+ StateData {
304
+ span : expr. span ,
305
+ hir_id : expr. hir_id ,
306
+ } ,
298
307
) ) ;
299
308
}
300
309
} ,
@@ -387,6 +396,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
387
396
spans: vec![ pat. span] ,
388
397
app,
389
398
replacements: vec![ ( pat. span, snip. into( ) ) ] ,
399
+ hir_id: pat. hir_id
390
400
} ) ,
391
401
) ;
392
402
}
@@ -399,13 +409,15 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
399
409
for pat in self . ref_locals . drain ( ..) . filter_map ( |( _, x) | x) {
400
410
let replacements = pat. replacements ;
401
411
let app = pat. app ;
402
- span_lint_and_then (
412
+ let lint = if pat. always_deref {
413
+ NEEDLESS_BORROW
414
+ } else {
415
+ REF_BINDING_TO_REFERENCE
416
+ } ;
417
+ span_lint_hir_and_then (
403
418
cx,
404
- if pat. always_deref {
405
- NEEDLESS_BORROW
406
- } else {
407
- REF_BINDING_TO_REFERENCE
408
- } ,
419
+ lint,
420
+ pat. hir_id ,
409
421
pat. spans ,
410
422
"this pattern creates a reference to a reference" ,
411
423
|diag| {
@@ -642,19 +654,14 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, state: State, data: S
642
654
} => {
643
655
let mut app = Applicability :: MachineApplicable ;
644
656
let snip = snippet_with_context ( cx, expr. span , data. span . ctxt ( ) , ".." , & mut app) . 0 ;
645
- span_lint_and_sugg (
646
- cx,
647
- NEEDLESS_BORROW ,
648
- data. span ,
649
- msg,
650
- "change this to" ,
651
- if required_precedence > expr. precedence ( ) . order ( ) && !has_enclosing_paren ( & snip) {
657
+ span_lint_hir_and_then ( cx, NEEDLESS_BORROW , data. hir_id , data. span , msg, |diag| {
658
+ let sugg = if required_precedence > expr. precedence ( ) . order ( ) && !has_enclosing_paren ( & snip) {
652
659
format ! ( "({})" , snip)
653
660
} else {
654
661
snip. into ( )
655
- } ,
656
- app ,
657
- ) ;
662
+ } ;
663
+ diag . span_suggestion ( data . span , "change this to" , sugg , app ) ;
664
+ } ) ;
658
665
} ,
659
666
}
660
667
}
0 commit comments