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 } ;
22use clippy_utils:: source:: { snippet_with_applicability, snippet_with_context} ;
33use clippy_utils:: sugg:: has_enclosing_paren;
44use clippy_utils:: ty:: peel_mid_ty_refs;
@@ -135,6 +135,7 @@ pub struct Dereferencing {
135135struct StateData {
136136 /// Span of the top level expression
137137 span : Span ,
138+ hir_id : HirId ,
138139}
139140
140141enum State {
@@ -169,6 +170,8 @@ struct RefPat {
169170 app : Applicability ,
170171 /// All the replacements which need to be made.
171172 replacements : Vec < ( Span , String ) > ,
173+ /// The [`HirId`] that the lint should be emitted at.
174+ hir_id : HirId ,
172175}
173176
174177impl < ' tcx > LateLintPass < ' tcx > for Dereferencing {
@@ -222,7 +225,10 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
222225 is_final_ufcs : matches ! ( expr. kind, ExprKind :: Call ( ..) ) ,
223226 target_mut,
224227 } ,
225- StateData { span : expr. span } ,
228+ StateData {
229+ span : expr. span ,
230+ hir_id : expr. hir_id ,
231+ } ,
226232 ) ) ;
227233 } ,
228234 RefOp :: AddrOf => {
@@ -294,7 +300,10 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
294300 required_precedence,
295301 msg,
296302 } ,
297- StateData { span : expr. span } ,
303+ StateData {
304+ span : expr. span ,
305+ hir_id : expr. hir_id ,
306+ } ,
298307 ) ) ;
299308 }
300309 } ,
@@ -387,6 +396,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
387396 spans: vec![ pat. span] ,
388397 app,
389398 replacements: vec![ ( pat. span, snip. into( ) ) ] ,
399+ hir_id: pat. hir_id
390400 } ) ,
391401 ) ;
392402 }
@@ -399,13 +409,15 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
399409 for pat in self . ref_locals . drain ( ..) . filter_map ( |( _, x) | x) {
400410 let replacements = pat. replacements ;
401411 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 (
403418 cx,
404- if pat. always_deref {
405- NEEDLESS_BORROW
406- } else {
407- REF_BINDING_TO_REFERENCE
408- } ,
419+ lint,
420+ pat. hir_id ,
409421 pat. spans ,
410422 "this pattern creates a reference to a reference" ,
411423 |diag| {
@@ -642,19 +654,14 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, state: State, data: S
642654 } => {
643655 let mut app = Applicability :: MachineApplicable ;
644656 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) {
652659 format ! ( "({})" , snip)
653660 } else {
654661 snip. into ( )
655- } ,
656- app ,
657- ) ;
662+ } ;
663+ diag . span_suggestion ( data . span , "change this to" , sugg , app ) ;
664+ } ) ;
658665 } ,
659666 }
660667}
0 commit comments