Skip to content

Commit

Permalink
let-chain fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Nov 16, 2023
1 parent 7114f6a commit 3e26c52
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2777,8 +2777,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|| lt.kind == MissingLifetimeKind::Underscore)
{
let pre = if lt.kind == MissingLifetimeKind::Ampersand
&& let Some((kind, _span)) =
self.diagnostic_metadata.current_function
&& let Some((kind, _span)) = self.diagnostic_metadata.current_function
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
&& !sig.decl.inputs.is_empty()
&& let sugg = sig
Expand All @@ -2803,7 +2802,6 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
.collect::<Vec<_>>()
&& !sugg.is_empty()
{

let (the, s) = if sig.decl.inputs.len() == 1 {
("the", "")
} else {
Expand All @@ -2819,9 +2817,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
);
"...or alternatively, you might want"
} else if (lt.kind == MissingLifetimeKind::Ampersand
|| lt.kind == MissingLifetimeKind::Underscore)
&& let Some((kind, _span)) =
self.diagnostic_metadata.current_function
|| lt.kind == MissingLifetimeKind::Underscore)
&& let Some((kind, _span)) = self.diagnostic_metadata.current_function
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
&& let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output
&& !sig.decl.inputs.is_empty()
Expand All @@ -2842,26 +2839,25 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
// So we look at every ref in the trait bound. If there's any, we
// suggest
// fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()>
let mut lt_finder = LifetimeFinder {
lifetime: lt.span,
found: None,
seen: vec![],
};
let mut lt_finder =
LifetimeFinder { lifetime: lt.span, found: None, seen: vec![] };
for bound in arg_refs {
if let ast::GenericBound::Trait(trait_ref, _) = bound {
lt_finder.visit_trait_ref(&trait_ref.trait_ref);
}
}
lt_finder.visit_ty(ret_ty);
let spans_suggs: Vec<_> = lt_finder.seen.iter().filter_map(|ty| {
match &ty.kind {
let spans_suggs: Vec<_> = lt_finder
.seen
.iter()
.filter_map(|ty| match &ty.kind {
TyKind::Ref(_, mut_ty) => {
let span = ty.span.with_hi(mut_ty.ty.span.lo());
Some((span, "&'a ".to_string()))
}
_ => None
}
}).collect();
_ => None,
})
.collect();
self.suggest_introducing_lifetime(
err,
None,
Expand All @@ -2882,20 +2878,16 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
};
let mut owned_sugg = lt.kind == MissingLifetimeKind::Ampersand;
let mut sugg = vec![(lt.span, String::new())];
if let Some((kind, _span)) =
self.diagnostic_metadata.current_function
if let Some((kind, _span)) = self.diagnostic_metadata.current_function
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
&& let ast::FnRetTy::Ty(ty) = &sig.decl.output
{
let mut lt_finder = LifetimeFinder {
lifetime: lt.span,
found: None,
seen: vec![],
};
let mut lt_finder =
LifetimeFinder { lifetime: lt.span, found: None, seen: vec![] };
lt_finder.visit_ty(&ty);

if let [Ty { span, kind: TyKind::Ref(_, mut_ty), ..}]
= &lt_finder.seen[..]
if let [Ty { span, kind: TyKind::Ref(_, mut_ty), .. }] =
&lt_finder.seen[..]
{
// We might have a situation like
// fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()>
Expand All @@ -2910,9 +2902,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
// Check if the path being borrowed is likely to be owned.
let path: Vec<_> = Segment::from_path(path);
match self.resolve_path(&path, Some(TypeNS), None) {
PathResult::Module(
ModuleOrUniformRoot::Module(module),
) => {
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
match module.res() {
Some(Res::PrimTy(PrimTy::Str)) => {
// Don't suggest `-> str`, suggest `-> String`.
Expand All @@ -2932,7 +2922,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
| DefKind::TyParam,
_,
)) => {}
_ => { // Do not suggest in all other cases.
_ => {
// Do not suggest in all other cases.
owned_sugg = false;
}
}
Expand All @@ -2957,12 +2948,14 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
| DefKind::TyParam,
_,
) => {}
_ => { // Do not suggest in all other cases.
_ => {
// Do not suggest in all other cases.
owned_sugg = false;
}
}
}
_ => { // Do not suggest in all other cases.
_ => {
// Do not suggest in all other cases.
owned_sugg = false;
}
}
Expand Down

0 comments on commit 3e26c52

Please sign in to comment.