Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8b2643c

Browse files
committed
Auto merge of rust-lang#134982 - Noratrieb:rollup-ixo5g4w, r=Noratrieb
Rollup of 9 pull requests Successful merges: - rust-lang#132474 (Add more mailmap entries) - rust-lang#133486 (borrowck diagnostics: make `add_move_error_suggestions` use the HIR rather than `SourceMap`) - rust-lang#134063 (dec2flt: Clean up float parsing modules) - rust-lang#134861 (Add GUI test for item info elements color) - rust-lang#134968 (Print how to rebless Python formatting in tidy) - rust-lang#134971 (chore: fix typos) - rust-lang#134972 (add .mailmap entry for myself) - rust-lang#134974 (Revert rust-lang#119515 single line where clause style guide) - rust-lang#134975 (Revert style guide rhs break) r? `@ghost` `@rustbot` modify labels: rollup
2 parents d117b7f + 37d0d1d commit 8b2643c

40 files changed

+1086
-752
lines changed

.mailmap

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Andrew Poelstra <asp11@sfu.ca> <apoelstra@wpsoftware.net>
4848
Anhad Singh <andypythonappdeveloper@gmail.com>
4949
Antoine Plaskowski <antoine.plaskowski@epitech.eu>
5050
Anton Löfgren <anton.lofgren@gmail.com> <alofgren@op5.com>
51+
apiraino <apiraino@users.noreply.github.com> <apiraino@protonmail.com>
5152
Araam Borhanian <avborhanian@gmail.com>
5253
Araam Borhanian <avborhanian@gmail.com> <dobbybabee@gmail.com>
5354
Areski Belaid <areski@gmail.com> areski <areski@gmail.com>
@@ -62,7 +63,10 @@ Austin Seipp <mad.one@gmail.com> <as@hacks.yi.org>
6263
Ayaz Hafiz <ayaz.hafiz.1@gmail.com>
6364
Aydin Kim <ladinjin@hanmail.net> aydin.kim <aydin.kim@samsung.com>
6465
Ayush Mishra <ayushmishra2005@gmail.com>
66+
Ashley Mannix <kodraus@hey.com> <ashleymannix@live.com.au>
6567
asrar <aszenz@gmail.com>
68+
b-naber <bn263@gmx.de>
69+
b-naber <bn263@gmx.de> <b_naber@gmx.de>
6670
BaoshanPang <pangbw@gmail.com>
6771
Barosl Lee <vcs@barosl.com> Barosl LEE <github@barosl.com>
6872
Bastian Kersting <bastian@cmbt.de>
@@ -98,6 +102,8 @@ Caleb Cartwright <caleb.cartwright@outlook.com>
98102
Caleb Jones <code@calebjones.net>
99103
Noah Lev <camelidcamel@gmail.com>
100104
Noah Lev <camelidcamel@gmail.com> <37223377+camelid@users.noreply.github.com>
105+
Catherine <catherine3.flores@gmail.com>
106+
Catherine <catherine3.flores@gmail.com> <catherine.3.flores@gmail.com>
101107
cameron1024 <cameron.studdstreet@gmail.com>
102108
Camille Gillot <gillot.camille@gmail.com>
103109
Carl-Anton Ingmarsson <mail@carlanton.se> <ca.ingmarsson@gmail.com>
@@ -133,11 +139,13 @@ Clement Miao <clementmiao@gmail.com>
133139
Clément Renault <renault.cle@gmail.com>
134140
Cliff Dyer <jcd@sdf.org>
135141
Clinton Ryan <clint.ryan3@gmail.com>
142+
Taylor Cramer <cramertaylorj@gmail.com> <cramertj@google.com>
136143
ember arlynx <ember@lunar.town> <corey@octayn.net>
137144
Crazycolorz5 <Crazycolorz5@gmail.com>
138145
csmoe <35686186+csmoe@users.noreply.github.com>
139146
Cyryl Płotnicki <cyplo@cyplo.net>
140147
Damien Schoof <damien.schoof@gmail.com>
148+
Dan Gohman <dev@sunfishcode.online> <sunfish@mozilla.com>
141149
Dan Robertson <danlrobertson89@gmail.com>
142150
Daniel Campoverde <alx741@riseup.net>
143151
Daniel J Rollins <drollins@financialforce.com>
@@ -179,10 +187,14 @@ Eduardo Bautista <me@eduardobautista.com> <=>
179187
Eduardo Bautista <me@eduardobautista.com> <mail@eduardobautista.com>
180188
Eduardo Broto <ebroto@tutanota.com>
181189
Edward Shen <code@eddie.sh> <xes@meta.com>
190+
Jacob Finkelman <eh2406@wayne.edu>
191+
Jacob Finkelman <eh2406@wayne.edu> <YeomanYaacov@gmail.com>
182192
Elliott Slaughter <elliottslaughter@gmail.com> <eslaughter@mozilla.com>
183193
Elly Fong-Jones <elly@leptoquark.net>
184194
Eric Holk <eric.holk@gmail.com> <eholk@cs.indiana.edu>
185195
Eric Holk <eric.holk@gmail.com> <eholk@mozilla.com>
196+
Eric Holk <eric.holk@gmail.com> <eric@theincredibleholk.org>
197+
Eric Holk <eric.holk@gmail.com> <ericholk@microsoft.com>
186198
Eric Holmes <eric@ejholmes.net>
187199
Eric Reed <ecreed@cs.washington.edu> <ereed@mozilla.com>
188200
Erick Tryzelaar <erick.tryzelaar@gmail.com> <etryzelaar@iqt.org>
@@ -206,6 +218,7 @@ Felix S. Klock II <pnkfelix@pnkfx.org> <pnkfelix@mozilla.com>
206218
Félix Saparelli <felix@passcod.name>
207219
Flaper Fesp <flaper87@gmail.com>
208220
Florian Berger <fbergr@gmail.com>
221+
Florian Gilcher <florian.gilcher@asquera.de> <flo@andersground.net>
209222
Florian Wilkens <mrfloya_github@outlook.com> Florian Wilkens <floya@live.de>
210223
François Mockers <mockersf@gmail.com>
211224
Frank Steffahn <fdsteffahn@gmail.com> <frank.steffahn@stu.uni-kiel.de>
@@ -240,6 +253,8 @@ Herman J. Radtke III <herman@hermanradtke.com> Herman J. Radtke III <hermanradtk
240253
Hirochika Matsumoto <git@hkmatsumoto.com> <matsujika@gmail.com>
241254
Hrvoje Nikšić <hniksic@gmail.com>
242255
Hsiang-Cheng Yang <rick68@users.noreply.github.com>
256+
Huon Wilson <dbau.pp@gmail.com>
257+
Huon Wilson <dbau.pp@gmail.com> <wilson.huon@gmail.com>
243258
Ian Jackson <ijackson@chiark.greenend.org.uk> <ian.jackson@citrix.com>
244259
Ian Jackson <ijackson@chiark.greenend.org.uk> <ijackson+github@slimy.greenend.org.uk>
245260
Ian Jackson <ijackson@chiark.greenend.org.uk> <iwj@xenproject.org>
@@ -252,9 +267,13 @@ ivan tkachenko <me@ratijas.tk>
252267
J. J. Weber <jjweber@gmail.com>
253268
Jack Huey <jack.huey@umassmed.edu> <jackh726@gmail.com>
254269
Jacob <jacob.macritchie@gmail.com>
270+
Jacob Hoffman-Andrews <rust@hoffman-andrews.com> <github@hoffman-andrews.com>
255271
Jacob Greenfield <xales@naveria.com>
256272
Jacob Pratt <jacob@jhpratt.dev> <the.z.cuber@gmail.com>
257273
Jacob Pratt <jacob@jhpratt.dev> <jacopratt@tesla.com>
274+
Jake Goulding <jake.goulding@integer32.com>
275+
Jake Goulding <jake.goulding@integer32.com> <jake.goulding@gmail.com>
276+
Jake Goulding <jake.goulding@integer32.com> <shepmaster@mac.com>
258277
Jake Vossen <jake@vossen.dev>
259278
Jakob Degen <jakob.e.degen@gmail.com> <jakob@degen.com>
260279
Jakob Lautrup Nysom <jako3047@gmail.com>
@@ -287,6 +306,7 @@ Jerry Hardee <hardeejj9@gmail.com>
287306
Jesús Rubio <jesusprubio@gmail.com>
288307
Jethro Beekman <github@jbeekman.nl>
289308
Jian Zeng <knight42@mail.ustc.edu.cn>
309+
Jieyou Xu <jieyouxu@outlook.com>
290310
Jieyou Xu <jieyouxu@outlook.com> <39484203+jieyouxu@users.noreply.github.com>
291311
Jihyun Yu <j.yu@navercorp.com> <yjh0502@gmail.com>
292312
Jihyun Yu <j.yu@navercorp.com> jihyun <jihyun@nablecomm.com>
@@ -322,9 +342,12 @@ Josh Holmer <jholmer.in@gmail.com>
322342
Josh Stone <cuviper@gmail.com> <jistone@redhat.com>
323343
Josh Stone <cuviper@gmail.com> <jistone@fedoraproject.org>
324344
Julia Ryan <juliaryan3.14@gmail.com> <josephryan3.14@gmail.com>
345+
Jubilee Young <workingjubilee@gmail.com> <46493976+workingjubilee@users.noreply.github.com>
346+
Jubilee Young <workingjubilee@gmail.com>
325347
Julian Knodt <julianknodt@gmail.com>
326348
jumbatm <jumbatm@gmail.com> <30644300+jumbatm@users.noreply.github.com>
327349
Junyoung Cho <june0.cho@samsung.com>
350+
Jynn Nelson <github@jyn.dev> <rust@jyn.dev>
328351
Jynn Nelson <github@jyn.dev> <jyn514@gmail.com>
329352
Jynn Nelson <github@jyn.dev> <joshua@yottadb.com>
330353
Jynn Nelson <github@jyn.dev> <jyn.nelson@redjack.com>
@@ -385,12 +408,14 @@ Marcell Pardavi <marcell.pardavi@gmail.com>
385408
Marcus Klaas de Vries <mail@marcusklaas.nl>
386409
Margaret Meyerhofer <mmeyerho@andrew.cmu.edu> <mmeyerho@andrew>
387410
Mark Mansi <markm@cs.wisc.edu>
411+
Mark Mansi <markm@cs.wisc.edu> <m.mim95@gmail.com>
388412
Mark Rousskov <mark.simulacrum@gmail.com>
389413
Mark Sinclair <mark.edward.x@gmail.com>
390414
Mark Sinclair <mark.edward.x@gmail.com> =Mark Sinclair <=125axel125@gmail.com>
391415
Markus Legner <markus@legner.ch>
392416
Markus Westerlind <marwes91@gmail.com> Markus <marwes91@gmail.com>
393417
Martin Carton <cartonmartin+git@gmail.com>
418+
Martin Carton <cartonmartin+git@gmail.com> <cartonmartin@gmail.com>
394419
Martin Habovštiak <martin.habovstiak@gmail.com>
395420
Martin Hafskjold Thoresen <martinhath@gmail.com>
396421
Martin Nordholts <martin.nordholts@codetale.se> <enselic@gmail.com>
@@ -415,6 +440,7 @@ Melody Horn <melody@boringcactus.com> <mathphreak@gmail.com>
415440
Mendes <pedro.mendes.26@gmail.com>
416441
mental <m3nta1@yahoo.com>
417442
mibac138 <5672750+mibac138@users.noreply.github.com>
443+
Michael Howell <michael@notriddle.com> <notriddle+rust-mod@protonmail.com>
418444
Michael Williams <m.t.williams@live.com>
419445
Michael Woerister <michaelwoerister@posteo> <michaelwoerister@gmail>
420446
Michael Woerister <michaelwoerister@posteo> <michaelwoerister@gmail.com>
@@ -431,6 +457,7 @@ Ms2ger <ms2ger@gmail.com> <Ms2ger@gmail.com>
431457
msizanoen1 <qtmlabs@protonmail.com>
432458
Mukilan Thiagarajan <mukilanthiagarajan@gmail.com>
433459
Nadrieril Feneanar <Nadrieril@users.noreply.github.com>
460+
Nadrieril Feneanar <Nadrieril@users.noreply.github.com> <nadrieril+rust@gmail.com>
434461
Nadrieril Feneanar <Nadrieril@users.noreply.github.com> <nadrieril+git@gmail.com>
435462
NAKASHIMA, Makoto <makoto.nksm+github@gmail.com> <makoto.nksm@gmail.com>
436463
NAKASHIMA, Makoto <makoto.nksm+github@gmail.com> <makoto.nksm+github@gmail.com>
@@ -447,15 +474,23 @@ Nicholas Bishop <nbishop@nbishop.net> <nicholasbishop@gmail.com>
447474
Nicholas Bishop <nbishop@nbishop.net> <nicholasbishop@google.com>
448475
Nicholas Nethercote <n.nethercote@gmail.com> <nnethercote@apple.com>
449476
Nicholas Nethercote <n.nethercote@gmail.com> <nnethercote@mozilla.com>
477+
Nick Cameron <nrc@ncameron.org> <ncameron@mozilla.com>
478+
Nick Fitzgerald <fitzgen@gmail.com> <nfitzgerald@mozilla.com>
450479
Nick Platt <platt.nicholas@gmail.com>
451480
Niclas Schwarzlose <15schnic@gmail.com>
452481
Nicolas Abram <abramlujan@gmail.com>
453482
Nicole Mazzuca <npmazzuca@gmail.com>
483+
Niko Matsakis <rust@nikomatsakis.com>
484+
Niko Matsakis <rust@nikomatsakis.com> <niko@alum.mit.edu>
485+
Noratrieb <48135649+Noratrieb@users.noreply.github.com>
454486
Noratrieb <48135649+Noratrieb@users.noreply.github.com> <48135649+Nilstrieb@users.noreply.github.com>
455487
Noratrieb <48135649+Noratrieb@users.noreply.github.com> <nilstrieb@gmail.com>
488+
Noratrieb <48135649+Noratrieb@users.noreply.github.com> <rust@noratrieb.dev>
456489
Noratrieb <48135649+Noratrieb@users.noreply.github.com> <nora@noratrieb.dev>
457490
Nif Ward <nif.ward@gmail.com>
458491
Nika Layzell <nika@thelayzells.com> <michael@thelayzells.com>
492+
Nikita Popov <nikita.ppv@gmail.com>
493+
Nikita Popov <nikita.ppv@gmail.com> <npopov@redhat.com>
459494
NODA Kai <nodakai@gmail.com>
460495
Oğuz Ağcayazı <oguz.agcayazi@gmail.com> <oguz.agcayazi@gmail.com>
461496
Oğuz Ağcayazı <oguz.agcayazi@gmail.com> <ouz.agz@gmail.com>
@@ -516,6 +551,7 @@ Ricky Hosfelt <ricky@hosfelt.io>
516551
Ritiek Malhotra <ritiekmalhotra123@gmail.com>
517552
Rob Arnold <robarnold@cs.cmu.edu>
518553
Rob Arnold <robarnold@cs.cmu.edu> Rob Arnold <robarnold@68-26-94-7.pools.spcsdns.net>
554+
Robert Collins <robertc@robertcollins.net> <robertc+rust@robertcollins.net>
519555
Robert Foss <dev@robertfoss.se> robertfoss <dev@robertfoss.se>
520556
Robert Gawdzik <rgawdzik@hotmail.com> Robert Gawdzik ☢ <rgawdzik@hotmail.com>
521557
Robert Habermeier <rphmeier@gmail.com>
@@ -553,7 +589,15 @@ Simon Sapin <simon@exyr.org> <simon.sapin@exyr.org>
553589
Simonas Kazlauskas <git@kazlauskas.me> Simonas Kazlauskas <github@kazlauskas.me>
554590
Simonas Kazlauskas <git@kazlauskas.me> <simonas+t-compiler@kazlauskas.me>
555591
Siva Prasad <sivaauturic@gmail.com>
592+
Skgland <3877590+Skgland@users.noreply.github.com>
593+
Skgland <3877590+Skgland@users.noreply.github.com> <bb-github@t-online.de>
594+
Skgland <3877590+Skgland@users.noreply.github.com> <bennet.blessmann+github@googlemail.com>
556595
Smittyvb <me@smitop.com>
596+
Sophia June Turner <547158+sophiajt@users.noreply.github.com>
597+
Sophia June Turner <547158+sophiajt@users.noreply.github.com> <547158+jntrnr@users.noreply.github.com>
598+
Sophia June Turner <547158+sophiajt@users.noreply.github.com> <jonathandturner@users.noreply.github.com>
599+
Sophia June Turner <547158+sophiajt@users.noreply.github.com> <jturner@mozilla.com>
600+
Sophia June Turner <547158+sophiajt@users.noreply.github.com> <jonathan.d.turner@gmail.com>
557601
Srinivas Reddy Thatiparthy <thatiparthysreenivas@gmail.com>
558602
Stanislav Tkach <stanislav.tkach@gmail.com>
559603
startling <tdixon51793@gmail.com>
@@ -586,8 +630,10 @@ Tim Diekmann <t.diekmann.3dv@gmail.com>
586630
Tim Hutt <tdhutt@gmail.com>
587631
Tim JIANG <p90eri@gmail.com>
588632
Tim Joseph Dumol <tim@timdumol.com>
633+
Tim Neumann <mail@timnn.me> <timnn@google.com>
589634
Timothy Maloney <tmaloney@pdx.edu>
590635
Tomas Koutsky <tomas@stepnivlk.net>
636+
Tomasz Miąsko <tomasz.miasko@gmail.com>
591637
Torsten Weber <TorstenWeber12@gmail.com>
592638
Torsten Weber <TorstenWeber12@gmail.com> <torstenweber12@gmail.com>
593639
Trevor Gross <tmgross@umich.edu> <t.gross35@gmail.com>
@@ -607,7 +653,7 @@ Valerii Lashmanov <vflashm@gmail.com>
607653
Vitali Haravy <HumaneProgrammer@gmail.com> Vitali Haravy <humaneprogrammer@gmail.com>
608654
Vitaly Shukela <vi0oss@gmail.com>
609655
Waffle Lapkin <waffle.lapkin@gmail.com>
610-
Waffle Lapkin <waffle.lapkin@tasking.com>
656+
Waffle Lapkin <waffle.lapkin@gmail.com> <waffle.lapkin@tasking.com>
611657
Wesley Wiser <wwiser@gmail.com> <wesleywiser@microsoft.com>
612658
whitequark <whitequark@whitequark.org>
613659
William Ting <io@williamting.com> <william.h.ting@gmail.com>

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 118 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#![allow(rustc::diagnostic_outside_of_impl)]
22
#![allow(rustc::untranslatable_diagnostic)]
33

4+
use rustc_data_structures::fx::FxHashSet;
45
use rustc_errors::{Applicability, Diag};
56
use rustc_hir::intravisit::Visitor;
6-
use rustc_hir::{CaptureBy, ExprKind, HirId, Node};
7+
use rustc_hir::{self as hir, CaptureBy, ExprKind, HirId, Node};
78
use rustc_middle::bug;
89
use rustc_middle::mir::*;
910
use rustc_middle::ty::{self, Ty};
@@ -683,48 +684,126 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
683684
}
684685

685686
fn add_move_error_suggestions(&self, err: &mut Diag<'_>, binds_to: &[Local]) {
686-
let mut suggestions: Vec<(Span, String, String)> = Vec::new();
687+
/// A HIR visitor to associate each binding with a `&` or `&mut` that could be removed to
688+
/// make it bind by reference instead (if possible)
689+
struct BindingFinder<'tcx> {
690+
typeck_results: &'tcx ty::TypeckResults<'tcx>,
691+
hir: rustc_middle::hir::map::Map<'tcx>,
692+
/// Input: the span of the pattern we're finding bindings in
693+
pat_span: Span,
694+
/// Input: the spans of the bindings we're providing suggestions for
695+
binding_spans: Vec<Span>,
696+
/// Internal state: have we reached the pattern we're finding bindings in?
697+
found_pat: bool,
698+
/// Internal state: the innermost `&` or `&mut` "above" the visitor
699+
ref_pat: Option<&'tcx hir::Pat<'tcx>>,
700+
/// Internal state: could removing a `&` give bindings unexpected types?
701+
has_adjustments: bool,
702+
/// Output: for each input binding, the `&` or `&mut` to remove to make it by-ref
703+
ref_pat_for_binding: Vec<(Span, Option<&'tcx hir::Pat<'tcx>>)>,
704+
/// Output: ref patterns that can't be removed straightforwardly
705+
cannot_remove: FxHashSet<HirId>,
706+
}
707+
impl<'tcx> Visitor<'tcx> for BindingFinder<'tcx> {
708+
type NestedFilter = rustc_middle::hir::nested_filter::OnlyBodies;
709+
710+
fn nested_visit_map(&mut self) -> Self::Map {
711+
self.hir
712+
}
713+
714+
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) -> Self::Result {
715+
// Don't walk into const patterns or anything else that might confuse this
716+
if !self.found_pat {
717+
hir::intravisit::walk_expr(self, ex)
718+
}
719+
}
720+
721+
fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
722+
if p.span == self.pat_span {
723+
self.found_pat = true;
724+
}
725+
726+
let parent_has_adjustments = self.has_adjustments;
727+
self.has_adjustments |=
728+
self.typeck_results.pat_adjustments().contains_key(p.hir_id);
729+
730+
// Track the innermost `&` or `&mut` enclosing bindings, to suggest removing it.
731+
let parent_ref_pat = self.ref_pat;
732+
if let hir::PatKind::Ref(..) = p.kind {
733+
self.ref_pat = Some(p);
734+
// To avoid edition-dependent logic to figure out how many refs this `&` can
735+
// peel off, simply don't remove the "parent" `&`.
736+
self.cannot_remove.extend(parent_ref_pat.map(|r| r.hir_id));
737+
if self.has_adjustments {
738+
// Removing this `&` could give child bindings unexpected types, so don't.
739+
self.cannot_remove.insert(p.hir_id);
740+
// As long the `&` stays, child patterns' types should be as expected.
741+
self.has_adjustments = false;
742+
}
743+
}
744+
745+
if let hir::PatKind::Binding(_, _, ident, _) = p.kind {
746+
// the spans in `binding_spans` encompass both the ident and binding mode
747+
if let Some(&bind_sp) =
748+
self.binding_spans.iter().find(|bind_sp| bind_sp.contains(ident.span))
749+
{
750+
self.ref_pat_for_binding.push((bind_sp, self.ref_pat));
751+
} else {
752+
// we've encountered a binding that we're not reporting a move error for.
753+
// we don't want to change its type, so don't remove the surrounding `&`.
754+
if let Some(ref_pat) = self.ref_pat {
755+
self.cannot_remove.insert(ref_pat.hir_id);
756+
}
757+
}
758+
}
759+
760+
hir::intravisit::walk_pat(self, p);
761+
self.ref_pat = parent_ref_pat;
762+
self.has_adjustments = parent_has_adjustments;
763+
}
764+
}
765+
let mut pat_span = None;
766+
let mut binding_spans = Vec::new();
687767
for local in binds_to {
688768
let bind_to = &self.body.local_decls[*local];
689-
if let LocalInfo::User(BindingForm::Var(VarBindingForm { pat_span, .. })) =
769+
if let LocalInfo::User(BindingForm::Var(VarBindingForm { pat_span: pat_sp, .. })) =
690770
*bind_to.local_info()
691771
{
692-
let Ok(pat_snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(pat_span)
693-
else {
694-
continue;
695-
};
696-
let Some(stripped) = pat_snippet.strip_prefix('&') else {
697-
suggestions.push((
698-
bind_to.source_info.span.shrink_to_lo(),
699-
"consider borrowing the pattern binding".to_string(),
700-
"ref ".to_string(),
701-
));
702-
continue;
703-
};
704-
let inner_pat_snippet = stripped.trim_start();
705-
let (pat_span, suggestion, to_remove) = if inner_pat_snippet.starts_with("mut")
706-
&& inner_pat_snippet["mut".len()..].starts_with(rustc_lexer::is_whitespace)
707-
{
708-
let inner_pat_snippet = inner_pat_snippet["mut".len()..].trim_start();
709-
let pat_span = pat_span.with_hi(
710-
pat_span.lo()
711-
+ BytePos((pat_snippet.len() - inner_pat_snippet.len()) as u32),
712-
);
713-
(pat_span, String::new(), "mutable borrow")
714-
} else {
715-
let pat_span = pat_span.with_hi(
716-
pat_span.lo()
717-
+ BytePos(
718-
(pat_snippet.len() - inner_pat_snippet.trim_start().len()) as u32,
719-
),
720-
);
721-
(pat_span, String::new(), "borrow")
722-
};
723-
suggestions.push((
724-
pat_span,
725-
format!("consider removing the {to_remove}"),
726-
suggestion,
727-
));
772+
pat_span = Some(pat_sp);
773+
binding_spans.push(bind_to.source_info.span);
774+
}
775+
}
776+
let Some(pat_span) = pat_span else { return };
777+
778+
let hir = self.infcx.tcx.hir();
779+
let Some(body) = hir.maybe_body_owned_by(self.mir_def_id()) else { return };
780+
let typeck_results = self.infcx.tcx.typeck(self.mir_def_id());
781+
let mut finder = BindingFinder {
782+
typeck_results,
783+
hir,
784+
pat_span,
785+
binding_spans,
786+
found_pat: false,
787+
ref_pat: None,
788+
has_adjustments: false,
789+
ref_pat_for_binding: Vec::new(),
790+
cannot_remove: FxHashSet::default(),
791+
};
792+
finder.visit_body(body);
793+
794+
let mut suggestions = Vec::new();
795+
for (binding_span, opt_ref_pat) in finder.ref_pat_for_binding {
796+
if let Some(ref_pat) = opt_ref_pat
797+
&& !finder.cannot_remove.contains(&ref_pat.hir_id)
798+
&& let hir::PatKind::Ref(subpat, mutbl) = ref_pat.kind
799+
&& let Some(ref_span) = ref_pat.span.trim_end(subpat.span)
800+
{
801+
let mutable_str = if mutbl.is_mut() { "mutable " } else { "" };
802+
let msg = format!("consider removing the {mutable_str}borrow");
803+
suggestions.push((ref_span, msg, "".to_string()));
804+
} else {
805+
let msg = "consider borrowing the pattern binding".to_string();
806+
suggestions.push((binding_span.shrink_to_lo(), msg, "ref ".to_string()));
728807
}
729808
}
730809
suggestions.sort_unstable_by_key(|&(span, _, _)| span);

0 commit comments

Comments
 (0)