|
2 | 2 | #![allow(rustc::untranslatable_diagnostic)] |
3 | 3 |
|
4 | 4 | use core::ops::ControlFlow; |
5 | | -use hir::ExprKind; |
| 5 | +use hir::{ExprKind, Param}; |
6 | 6 | use rustc_errors::{Applicability, Diag}; |
7 | 7 | use rustc_hir as hir; |
8 | 8 | use rustc_hir::intravisit::Visitor; |
@@ -725,25 +725,26 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { |
725 | 725 | _ => local_decl.source_info.span, |
726 | 726 | }; |
727 | 727 |
|
728 | | - let def_id = self.body.source.def_id(); |
729 | | - let hir_id = if let Some(local_def_id) = def_id.as_local() |
730 | | - && let Some(body_id) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id) |
731 | | - { |
732 | | - let body = self.infcx.tcx.hir().body(body_id); |
733 | | - BindingFinder { span: pat_span }.visit_body(body).break_value() |
734 | | - } else { |
735 | | - None |
736 | | - }; |
737 | | - |
738 | 728 | // With ref-binding patterns, the mutability suggestion has to apply to |
739 | 729 | // the binding, not the reference (which would be a type error): |
740 | 730 | // |
741 | 731 | // `let &b = a;` -> `let &(mut b) = a;` |
742 | | - if let Some(hir_id) = hir_id |
| 732 | + // or |
| 733 | + // `fn foo(&x: &i32)` -> `fn foo(&(mut x): &i32)` |
| 734 | + let def_id = self.body.source.def_id(); |
| 735 | + if let Some(local_def_id) = def_id.as_local() |
| 736 | + && let Some(body_id) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id) |
| 737 | + && let body = self.infcx.tcx.hir().body(body_id) |
| 738 | + && let Some(hir_id) = (BindingFinder { span: pat_span }).visit_body(body).break_value() |
| 739 | + && let node = self.infcx.tcx.hir_node(hir_id) |
743 | 740 | && let hir::Node::Local(hir::Local { |
744 | 741 | pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. }, |
745 | 742 | .. |
746 | | - }) = self.infcx.tcx.hir_node(hir_id) |
| 743 | + }) |
| 744 | + | hir::Node::Param(Param { |
| 745 | + pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. }, |
| 746 | + .. |
| 747 | + }) = node |
747 | 748 | && let Ok(name) = |
748 | 749 | self.infcx.tcx.sess.source_map().span_to_snippet(local_decl.source_info.span) |
749 | 750 | { |
@@ -1310,6 +1311,16 @@ impl<'tcx> Visitor<'tcx> for BindingFinder { |
1310 | 1311 | hir::intravisit::walk_stmt(self, s) |
1311 | 1312 | } |
1312 | 1313 | } |
| 1314 | + |
| 1315 | + fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) -> Self::Result { |
| 1316 | + if let hir::Pat { kind: hir::PatKind::Ref(_, _), span, .. } = param.pat |
| 1317 | + && *span == self.span |
| 1318 | + { |
| 1319 | + ControlFlow::Break(param.hir_id) |
| 1320 | + } else { |
| 1321 | + ControlFlow::Continue(()) |
| 1322 | + } |
| 1323 | + } |
1313 | 1324 | } |
1314 | 1325 |
|
1315 | 1326 | pub fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symbol>) -> bool { |
|
0 commit comments