Skip to content

Commit

Permalink
Rollup merge of #96065 - TaKO8Ki:use-format-args-capture-and-remove…
Browse files Browse the repository at this point in the history
…-unnecessary-nested-blocks, r=compiler-errors

Refactor: Use `format-args-capture` and remove unnecessary nested blocks in rustc_typeck
  • Loading branch information
Dylan-DPC authored Apr 15, 2022
2 parents 18a7ce3 + f9188cc commit fa281fd
Show file tree
Hide file tree
Showing 17 changed files with 605 additions and 676 deletions.
10 changes: 5 additions & 5 deletions compiler/rustc_typeck/src/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&mut |err| {
if let Some((span, msg)) = &ret_reason {
err.span_label(*span, msg.as_str());
} else if let ExprKind::Block(block, _) = &then_expr.kind {
if let Some(expr) = &block.expr {
err.span_label(expr.span, "found here".to_string());
}
} else if let ExprKind::Block(block, _) = &then_expr.kind
&& let Some(expr) = &block.expr
{
err.span_label(expr.span, "found here".to_string());
}
err.note("`if` expressions without `else` evaluate to `()`");
err.help("consider adding an `else` block that evaluates to the expected type");
Expand Down Expand Up @@ -293,7 +293,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return self.get_fn_decl(hir_id).and_then(|(fn_decl, _)| {
let span = fn_decl.output.span();
let snippet = self.tcx.sess.source_map().span_to_snippet(span).ok()?;
Some((span, format!("expected `{}` because of this return type", snippet)))
Some((span, format!("expected `{snippet}` because of this return type")))
});
}
}
Expand Down
51 changes: 24 additions & 27 deletions compiler/rustc_typeck/src/check/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn check_legal_trait_for_method_call(
let (sp, suggestion) = receiver
.and_then(|s| tcx.sess.source_map().span_to_snippet(s).ok())
.filter(|snippet| !snippet.is_empty())
.map(|snippet| (expr_span, format!("drop({})", snippet)))
.map(|snippet| (expr_span, format!("drop({snippet})")))
.unwrap_or_else(|| (span, "drop".to_string()));

err.span_suggestion(
Expand Down Expand Up @@ -315,17 +315,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::ExprKind::Tup(exp),
hir::ExprKind::Call(_, args),
) = (parent_node, &callee_expr.kind, &call_expr.kind)
&& args.len() == exp.len()
{
if args.len() == exp.len() {
let start = callee_expr.span.shrink_to_hi();
err.span_suggestion(
start,
"consider separating array elements with a comma",
",".to_string(),
Applicability::MaybeIncorrect,
);
return true;
}
let start = callee_expr.span.shrink_to_hi();
err.span_suggestion(
start,
"consider separating array elements with a comma",
",".to_string(),
Applicability::MaybeIncorrect,
);
return true;
}
false
}
Expand Down Expand Up @@ -373,15 +372,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ref t => {
let mut unit_variant = None;
let mut removal_span = call_expr.span;
if let ty::Adt(adt_def, ..) = t {
if adt_def.is_enum() {
if let hir::ExprKind::Call(expr, _) = call_expr.kind {
removal_span =
expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
unit_variant =
self.tcx.sess.source_map().span_to_snippet(expr.span).ok();
}
}
if let ty::Adt(adt_def, ..) = t
&& adt_def.is_enum()
&& let hir::ExprKind::Call(expr, _) = call_expr.kind
{
removal_span =
expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
unit_variant =
self.tcx.sess.source_map().span_to_snippet(expr.span).ok();
}

let callee_ty = self.resolve_vars_if_possible(callee_ty);
Expand All @@ -392,8 +390,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
E0618,
"expected function, found {}",
match unit_variant {
Some(ref path) => format!("enum variant `{}`", path),
None => format!("`{}`", callee_ty),
Some(ref path) => format!("enum variant `{path}`"),
None => format!("`{callee_ty}`"),
}
);

Expand All @@ -408,8 +406,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.span_suggestion_verbose(
removal_span,
&format!(
"`{}` is a unit variant, you need to write it without the parentheses",
path
"`{path}` is a unit variant, you need to write it without the parentheses",
),
String::new(),
Applicability::MachineApplicable,
Expand Down Expand Up @@ -452,14 +449,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Some(span) = self.tcx.hir().res_span(def) {
let callee_ty = callee_ty.to_string();
let label = match (unit_variant, inner_callee_path) {
(Some(path), _) => Some(format!("`{}` defined here", path)),
(Some(path), _) => Some(format!("`{path}` defined here")),
(_, Some(hir::QPath::Resolved(_, path))) => self
.tcx
.sess
.source_map()
.span_to_snippet(path.span)
.ok()
.map(|p| format!("`{}` defined here returns `{}`", p, callee_ty)),
.map(|p| format!("`{p}` defined here returns `{callee_ty}`")),
_ => {
match def {
// Emit a different diagnostic for local variables, as they are not
Expand All @@ -475,7 +472,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx.def_path_str(def_id),
))
}
_ => Some(format!("`{}` defined here", callee_ty)),
_ => Some(format!("`{callee_ty}` defined here")),
}
}
};
Expand Down
146 changes: 70 additions & 76 deletions compiler/rustc_typeck/src/check/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
err.span_suggestion(
self.span,
"compare with zero instead",
format!("{} != 0", snippet),
format!("{snippet} != 0"),
Applicability::MachineApplicable,
);
}
Expand Down Expand Up @@ -373,8 +373,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
let mut sugg = None;
let mut sugg_mutref = false;
if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
if let ty::RawPtr(TypeAndMut { ty: expr_ty, .. }) = *self.expr_ty.kind() {
if fcx
if let ty::RawPtr(TypeAndMut { ty: expr_ty, .. }) = *self.expr_ty.kind()
&& fcx
.try_coerce(
self.expr,
fcx.tcx.mk_ref(
Expand All @@ -386,27 +386,25 @@ impl<'a, 'tcx> CastCheck<'tcx> {
None,
)
.is_ok()
{
sugg = Some((format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
}
} else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind() {
if expr_mutbl == Mutability::Not
&& mutbl == Mutability::Mut
&& fcx
.try_coerce(
self.expr,
fcx.tcx.mk_ref(
expr_reg,
TypeAndMut { ty: expr_ty, mutbl: Mutability::Mut },
),
self.cast_ty,
AllowTwoPhase::No,
None,
)
.is_ok()
{
sugg_mutref = true;
}
{
sugg = Some((format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
} else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind()
&& expr_mutbl == Mutability::Not
&& mutbl == Mutability::Mut
&& fcx
.try_coerce(
self.expr,
fcx.tcx.mk_ref(
expr_reg,
TypeAndMut { ty: expr_ty, mutbl: Mutability::Mut },
),
self.cast_ty,
AllowTwoPhase::No,
None,
)
.is_ok()
{
sugg_mutref = true;
}

if !sugg_mutref
Expand All @@ -423,8 +421,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
{
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
}
} else if let ty::RawPtr(TypeAndMut { mutbl, .. }) = *self.cast_ty.kind() {
if fcx
} else if let ty::RawPtr(TypeAndMut { mutbl, .. }) = *self.cast_ty.kind()
&& fcx
.try_coerce(
self.expr,
fcx.tcx.mk_ref(
Expand All @@ -436,9 +434,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
None,
)
.is_ok()
{
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
}
{
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
}
if sugg_mutref {
err.span_label(self.span, "invalid cast");
Expand Down Expand Up @@ -483,28 +480,28 @@ impl<'a, 'tcx> CastCheck<'tcx> {
) {
let mut label = true;
// Check `impl From<self.expr_ty> for self.cast_ty {}` for accurate suggestion:
if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span) {
if let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From) {
let ty = fcx.resolve_vars_if_possible(self.cast_ty);
// Erase regions to avoid panic in `prove_value` when calling
// `type_implements_trait`.
let ty = fcx.tcx.erase_regions(ty);
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
let expr_ty = fcx.tcx.erase_regions(expr_ty);
let ty_params = fcx.tcx.mk_substs_trait(expr_ty, &[]);
if fcx
.infcx
.type_implements_trait(from_trait, ty, ty_params, fcx.param_env)
.must_apply_modulo_regions()
{
label = false;
err.span_suggestion(
self.span,
"consider using the `From` trait instead",
format!("{}::from({})", self.cast_ty, snippet),
Applicability::MaybeIncorrect,
);
}
if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span)
&& let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From)
{
let ty = fcx.resolve_vars_if_possible(self.cast_ty);
// Erase regions to avoid panic in `prove_value` when calling
// `type_implements_trait`.
let ty = fcx.tcx.erase_regions(ty);
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
let expr_ty = fcx.tcx.erase_regions(expr_ty);
let ty_params = fcx.tcx.mk_substs_trait(expr_ty, &[]);
if fcx
.infcx
.type_implements_trait(from_trait, ty, ty_params, fcx.param_env)
.must_apply_modulo_regions()
{
label = false;
err.span_suggestion(
self.span,
"consider using the `From` trait instead",
format!("{}::from({})", self.cast_ty, snippet),
Applicability::MaybeIncorrect,
);
}
}
let msg = "an `as` expression can only be used to convert between primitive \
Expand Down Expand Up @@ -627,10 +624,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
}
}
} else {
let msg = &format!(
"consider using an implicit coercion to `&{}{}` instead",
mtstr, tstr
);
let msg =
&format!("consider using an implicit coercion to `&{mtstr}{tstr}` instead");
err.span_help(self.span, msg);
}
}
Expand All @@ -640,14 +635,14 @@ impl<'a, 'tcx> CastCheck<'tcx> {
err.span_suggestion(
self.cast_span,
"you can cast to a `Box` instead",
format!("Box<{}>", s),
format!("Box<{s}>"),
Applicability::MachineApplicable,
);
}
Err(_) => {
err.span_help(
self.cast_span,
&format!("you might have meant `Box<{}>`", tstr),
&format!("you might have meant `Box<{tstr}>`"),
);
}
}
Expand Down Expand Up @@ -678,8 +673,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
))
.help(&format!(
"cast can be replaced by coercion; this might \
require {}a temporary variable",
type_asc_or
require {type_asc_or}a temporary variable"
))
.emit();
});
Expand Down Expand Up @@ -969,21 +963,21 @@ impl<'a, 'tcx> CastCheck<'tcx> {
}

fn cenum_impl_drop_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
if let ty::Adt(d, _) = self.expr_ty.kind() {
if d.has_dtor(fcx.tcx) {
fcx.tcx.struct_span_lint_hir(
lint::builtin::CENUM_IMPL_DROP_CAST,
self.expr.hir_id,
self.span,
|err| {
err.build(&format!(
"cannot cast enum `{}` into integer `{}` because it implements `Drop`",
self.expr_ty, self.cast_ty
))
.emit();
},
);
}
if let ty::Adt(d, _) = self.expr_ty.kind()
&& d.has_dtor(fcx.tcx)
{
fcx.tcx.struct_span_lint_hir(
lint::builtin::CENUM_IMPL_DROP_CAST,
self.expr.hir_id,
self.span,
|err| {
err.build(&format!(
"cannot cast enum `{}` into integer `{}` because it implements `Drop`",
self.expr_ty, self.cast_ty
))
.emit();
},
);
}
}

Expand All @@ -1007,7 +1001,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
err.span_suggestion(
self.span,
msg,
format!("({}).addr(){}", snippet, scalar_cast),
format!("({snippet}).addr(){scalar_cast}"),
Applicability::MaybeIncorrect
);
} else {
Expand Down Expand Up @@ -1038,7 +1032,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
err.span_suggestion(
self.span,
msg,
format!("(...).with_addr({})", snippet),
format!("(...).with_addr({snippet})"),
Applicability::HasPlaceholders,
);
} else {
Expand Down
Loading

0 comments on commit fa281fd

Please sign in to comment.