Skip to content

Commit fa281fd

Browse files
authored
Rollup merge of #96065 - TaKO8Ki:use-format-args-capture-and-remove-unnecessary-nested-blocks, r=compiler-errors
Refactor: Use `format-args-capture` and remove unnecessary nested blocks in rustc_typeck
2 parents 18a7ce3 + f9188cc commit fa281fd

17 files changed

+605
-676
lines changed

compiler/rustc_typeck/src/check/_match.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
260260
&mut |err| {
261261
if let Some((span, msg)) = &ret_reason {
262262
err.span_label(*span, msg.as_str());
263-
} else if let ExprKind::Block(block, _) = &then_expr.kind {
264-
if let Some(expr) = &block.expr {
265-
err.span_label(expr.span, "found here".to_string());
266-
}
263+
} else if let ExprKind::Block(block, _) = &then_expr.kind
264+
&& let Some(expr) = &block.expr
265+
{
266+
err.span_label(expr.span, "found here".to_string());
267267
}
268268
err.note("`if` expressions without `else` evaluate to `()`");
269269
err.help("consider adding an `else` block that evaluates to the expected type");
@@ -293,7 +293,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
293293
return self.get_fn_decl(hir_id).and_then(|(fn_decl, _)| {
294294
let span = fn_decl.output.span();
295295
let snippet = self.tcx.sess.source_map().span_to_snippet(span).ok()?;
296-
Some((span, format!("expected `{}` because of this return type", snippet)))
296+
Some((span, format!("expected `{snippet}` because of this return type")))
297297
});
298298
}
299299
}

compiler/rustc_typeck/src/check/callee.rs

+24-27
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn check_legal_trait_for_method_call(
4343
let (sp, suggestion) = receiver
4444
.and_then(|s| tcx.sess.source_map().span_to_snippet(s).ok())
4545
.filter(|snippet| !snippet.is_empty())
46-
.map(|snippet| (expr_span, format!("drop({})", snippet)))
46+
.map(|snippet| (expr_span, format!("drop({snippet})")))
4747
.unwrap_or_else(|| (span, "drop".to_string()));
4848

4949
err.span_suggestion(
@@ -315,17 +315,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
315315
hir::ExprKind::Tup(exp),
316316
hir::ExprKind::Call(_, args),
317317
) = (parent_node, &callee_expr.kind, &call_expr.kind)
318+
&& args.len() == exp.len()
318319
{
319-
if args.len() == exp.len() {
320-
let start = callee_expr.span.shrink_to_hi();
321-
err.span_suggestion(
322-
start,
323-
"consider separating array elements with a comma",
324-
",".to_string(),
325-
Applicability::MaybeIncorrect,
326-
);
327-
return true;
328-
}
320+
let start = callee_expr.span.shrink_to_hi();
321+
err.span_suggestion(
322+
start,
323+
"consider separating array elements with a comma",
324+
",".to_string(),
325+
Applicability::MaybeIncorrect,
326+
);
327+
return true;
329328
}
330329
false
331330
}
@@ -373,15 +372,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
373372
ref t => {
374373
let mut unit_variant = None;
375374
let mut removal_span = call_expr.span;
376-
if let ty::Adt(adt_def, ..) = t {
377-
if adt_def.is_enum() {
378-
if let hir::ExprKind::Call(expr, _) = call_expr.kind {
379-
removal_span =
380-
expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
381-
unit_variant =
382-
self.tcx.sess.source_map().span_to_snippet(expr.span).ok();
383-
}
384-
}
375+
if let ty::Adt(adt_def, ..) = t
376+
&& adt_def.is_enum()
377+
&& let hir::ExprKind::Call(expr, _) = call_expr.kind
378+
{
379+
removal_span =
380+
expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
381+
unit_variant =
382+
self.tcx.sess.source_map().span_to_snippet(expr.span).ok();
385383
}
386384

387385
let callee_ty = self.resolve_vars_if_possible(callee_ty);
@@ -392,8 +390,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
392390
E0618,
393391
"expected function, found {}",
394392
match unit_variant {
395-
Some(ref path) => format!("enum variant `{}`", path),
396-
None => format!("`{}`", callee_ty),
393+
Some(ref path) => format!("enum variant `{path}`"),
394+
None => format!("`{callee_ty}`"),
397395
}
398396
);
399397

@@ -408,8 +406,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
408406
err.span_suggestion_verbose(
409407
removal_span,
410408
&format!(
411-
"`{}` is a unit variant, you need to write it without the parentheses",
412-
path
409+
"`{path}` is a unit variant, you need to write it without the parentheses",
413410
),
414411
String::new(),
415412
Applicability::MachineApplicable,
@@ -452,14 +449,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
452449
if let Some(span) = self.tcx.hir().res_span(def) {
453450
let callee_ty = callee_ty.to_string();
454451
let label = match (unit_variant, inner_callee_path) {
455-
(Some(path), _) => Some(format!("`{}` defined here", path)),
452+
(Some(path), _) => Some(format!("`{path}` defined here")),
456453
(_, Some(hir::QPath::Resolved(_, path))) => self
457454
.tcx
458455
.sess
459456
.source_map()
460457
.span_to_snippet(path.span)
461458
.ok()
462-
.map(|p| format!("`{}` defined here returns `{}`", p, callee_ty)),
459+
.map(|p| format!("`{p}` defined here returns `{callee_ty}`")),
463460
_ => {
464461
match def {
465462
// Emit a different diagnostic for local variables, as they are not
@@ -475,7 +472,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
475472
self.tcx.def_path_str(def_id),
476473
))
477474
}
478-
_ => Some(format!("`{}` defined here", callee_ty)),
475+
_ => Some(format!("`{callee_ty}` defined here")),
479476
}
480477
}
481478
};

compiler/rustc_typeck/src/check/cast.rs

+70-76
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
322322
err.span_suggestion(
323323
self.span,
324324
"compare with zero instead",
325-
format!("{} != 0", snippet),
325+
format!("{snippet} != 0"),
326326
Applicability::MachineApplicable,
327327
);
328328
}
@@ -373,8 +373,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
373373
let mut sugg = None;
374374
let mut sugg_mutref = false;
375375
if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
376-
if let ty::RawPtr(TypeAndMut { ty: expr_ty, .. }) = *self.expr_ty.kind() {
377-
if fcx
376+
if let ty::RawPtr(TypeAndMut { ty: expr_ty, .. }) = *self.expr_ty.kind()
377+
&& fcx
378378
.try_coerce(
379379
self.expr,
380380
fcx.tcx.mk_ref(
@@ -386,27 +386,25 @@ impl<'a, 'tcx> CastCheck<'tcx> {
386386
None,
387387
)
388388
.is_ok()
389-
{
390-
sugg = Some((format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
391-
}
392-
} else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind() {
393-
if expr_mutbl == Mutability::Not
394-
&& mutbl == Mutability::Mut
395-
&& fcx
396-
.try_coerce(
397-
self.expr,
398-
fcx.tcx.mk_ref(
399-
expr_reg,
400-
TypeAndMut { ty: expr_ty, mutbl: Mutability::Mut },
401-
),
402-
self.cast_ty,
403-
AllowTwoPhase::No,
404-
None,
405-
)
406-
.is_ok()
407-
{
408-
sugg_mutref = true;
409-
}
389+
{
390+
sugg = Some((format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
391+
} else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind()
392+
&& expr_mutbl == Mutability::Not
393+
&& mutbl == Mutability::Mut
394+
&& fcx
395+
.try_coerce(
396+
self.expr,
397+
fcx.tcx.mk_ref(
398+
expr_reg,
399+
TypeAndMut { ty: expr_ty, mutbl: Mutability::Mut },
400+
),
401+
self.cast_ty,
402+
AllowTwoPhase::No,
403+
None,
404+
)
405+
.is_ok()
406+
{
407+
sugg_mutref = true;
410408
}
411409

412410
if !sugg_mutref
@@ -423,8 +421,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
423421
{
424422
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
425423
}
426-
} else if let ty::RawPtr(TypeAndMut { mutbl, .. }) = *self.cast_ty.kind() {
427-
if fcx
424+
} else if let ty::RawPtr(TypeAndMut { mutbl, .. }) = *self.cast_ty.kind()
425+
&& fcx
428426
.try_coerce(
429427
self.expr,
430428
fcx.tcx.mk_ref(
@@ -436,9 +434,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
436434
None,
437435
)
438436
.is_ok()
439-
{
440-
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
441-
}
437+
{
438+
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
442439
}
443440
if sugg_mutref {
444441
err.span_label(self.span, "invalid cast");
@@ -483,28 +480,28 @@ impl<'a, 'tcx> CastCheck<'tcx> {
483480
) {
484481
let mut label = true;
485482
// Check `impl From<self.expr_ty> for self.cast_ty {}` for accurate suggestion:
486-
if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span) {
487-
if let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From) {
488-
let ty = fcx.resolve_vars_if_possible(self.cast_ty);
489-
// Erase regions to avoid panic in `prove_value` when calling
490-
// `type_implements_trait`.
491-
let ty = fcx.tcx.erase_regions(ty);
492-
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
493-
let expr_ty = fcx.tcx.erase_regions(expr_ty);
494-
let ty_params = fcx.tcx.mk_substs_trait(expr_ty, &[]);
495-
if fcx
496-
.infcx
497-
.type_implements_trait(from_trait, ty, ty_params, fcx.param_env)
498-
.must_apply_modulo_regions()
499-
{
500-
label = false;
501-
err.span_suggestion(
502-
self.span,
503-
"consider using the `From` trait instead",
504-
format!("{}::from({})", self.cast_ty, snippet),
505-
Applicability::MaybeIncorrect,
506-
);
507-
}
483+
if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span)
484+
&& let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From)
485+
{
486+
let ty = fcx.resolve_vars_if_possible(self.cast_ty);
487+
// Erase regions to avoid panic in `prove_value` when calling
488+
// `type_implements_trait`.
489+
let ty = fcx.tcx.erase_regions(ty);
490+
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
491+
let expr_ty = fcx.tcx.erase_regions(expr_ty);
492+
let ty_params = fcx.tcx.mk_substs_trait(expr_ty, &[]);
493+
if fcx
494+
.infcx
495+
.type_implements_trait(from_trait, ty, ty_params, fcx.param_env)
496+
.must_apply_modulo_regions()
497+
{
498+
label = false;
499+
err.span_suggestion(
500+
self.span,
501+
"consider using the `From` trait instead",
502+
format!("{}::from({})", self.cast_ty, snippet),
503+
Applicability::MaybeIncorrect,
504+
);
508505
}
509506
}
510507
let msg = "an `as` expression can only be used to convert between primitive \
@@ -627,10 +624,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
627624
}
628625
}
629626
} else {
630-
let msg = &format!(
631-
"consider using an implicit coercion to `&{}{}` instead",
632-
mtstr, tstr
633-
);
627+
let msg =
628+
&format!("consider using an implicit coercion to `&{mtstr}{tstr}` instead");
634629
err.span_help(self.span, msg);
635630
}
636631
}
@@ -640,14 +635,14 @@ impl<'a, 'tcx> CastCheck<'tcx> {
640635
err.span_suggestion(
641636
self.cast_span,
642637
"you can cast to a `Box` instead",
643-
format!("Box<{}>", s),
638+
format!("Box<{s}>"),
644639
Applicability::MachineApplicable,
645640
);
646641
}
647642
Err(_) => {
648643
err.span_help(
649644
self.cast_span,
650-
&format!("you might have meant `Box<{}>`", tstr),
645+
&format!("you might have meant `Box<{tstr}>`"),
651646
);
652647
}
653648
}
@@ -678,8 +673,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
678673
))
679674
.help(&format!(
680675
"cast can be replaced by coercion; this might \
681-
require {}a temporary variable",
682-
type_asc_or
676+
require {type_asc_or}a temporary variable"
683677
))
684678
.emit();
685679
});
@@ -969,21 +963,21 @@ impl<'a, 'tcx> CastCheck<'tcx> {
969963
}
970964

971965
fn cenum_impl_drop_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
972-
if let ty::Adt(d, _) = self.expr_ty.kind() {
973-
if d.has_dtor(fcx.tcx) {
974-
fcx.tcx.struct_span_lint_hir(
975-
lint::builtin::CENUM_IMPL_DROP_CAST,
976-
self.expr.hir_id,
977-
self.span,
978-
|err| {
979-
err.build(&format!(
980-
"cannot cast enum `{}` into integer `{}` because it implements `Drop`",
981-
self.expr_ty, self.cast_ty
982-
))
983-
.emit();
984-
},
985-
);
986-
}
966+
if let ty::Adt(d, _) = self.expr_ty.kind()
967+
&& d.has_dtor(fcx.tcx)
968+
{
969+
fcx.tcx.struct_span_lint_hir(
970+
lint::builtin::CENUM_IMPL_DROP_CAST,
971+
self.expr.hir_id,
972+
self.span,
973+
|err| {
974+
err.build(&format!(
975+
"cannot cast enum `{}` into integer `{}` because it implements `Drop`",
976+
self.expr_ty, self.cast_ty
977+
))
978+
.emit();
979+
},
980+
);
987981
}
988982
}
989983

@@ -1007,7 +1001,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10071001
err.span_suggestion(
10081002
self.span,
10091003
msg,
1010-
format!("({}).addr(){}", snippet, scalar_cast),
1004+
format!("({snippet}).addr(){scalar_cast}"),
10111005
Applicability::MaybeIncorrect
10121006
);
10131007
} else {
@@ -1038,7 +1032,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10381032
err.span_suggestion(
10391033
self.span,
10401034
msg,
1041-
format!("(...).with_addr({})", snippet),
1035+
format!("(...).with_addr({snippet})"),
10421036
Applicability::HasPlaceholders,
10431037
);
10441038
} else {

0 commit comments

Comments
 (0)