use format-args-capture and remove unnecessary nested block

This commit is contained in:
Takayuki Maeda 2022-04-15 15:56:32 +09:00
parent e7575f9670
commit f9188ccef6
17 changed files with 614 additions and 685 deletions

View File

@ -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");
@ -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")))
});
}
}

View File

@ -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(
@ -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
}
@ -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);
@ -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}`"),
}
);
@ -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,
@ -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
@ -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")),
}
}
};

View File

@ -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,
);
}
@ -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(
@ -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
@ -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(
@ -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");
@ -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 \
@ -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);
}
}
@ -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}>`"),
);
}
}
@ -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();
});
@ -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();
},
);
}
}
@ -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 {
@ -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 {

View File

@ -43,8 +43,7 @@ pub(super) fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Ab
tcx.sess,
span,
E0570,
"`{}` is not a supported ABI for the current target",
abi
"`{abi}` is not a supported ABI for the current target",
)
.emit();
}
@ -249,84 +248,84 @@ pub(super) fn check_fn<'a, 'tcx>(
fcx.demand_suptype(span, declared_ret_ty, actual_return_ty);
// Check that a function marked as `#[panic_handler]` has signature `fn(&PanicInfo) -> !`
if let Some(panic_impl_did) = tcx.lang_items().panic_impl() {
if panic_impl_did == hir.local_def_id(fn_id).to_def_id() {
if let Some(panic_info_did) = tcx.lang_items().panic_info() {
if *declared_ret_ty.kind() != ty::Never {
sess.span_err(decl.output.span(), "return type should be `!`");
}
let inputs = fn_sig.inputs();
let span = hir.span(fn_id);
if inputs.len() == 1 {
let arg_is_panic_info = match *inputs[0].kind() {
ty::Ref(region, ty, mutbl) => match *ty.kind() {
ty::Adt(ref adt, _) => {
adt.did() == panic_info_did
&& mutbl == hir::Mutability::Not
&& !region.is_static()
}
_ => false,
},
_ => false,
};
if !arg_is_panic_info {
sess.span_err(decl.inputs[0].span, "argument should be `&PanicInfo`");
}
if let Node::Item(item) = hir.get(fn_id)
&& let ItemKind::Fn(_, ref generics, _) = item.kind
&& !generics.params.is_empty()
{
sess.span_err(span, "should have no type parameters");
}
} else {
let span = sess.source_map().guess_head_span(span);
sess.span_err(span, "function should have one argument");
}
} else {
sess.err("language item required, but not found: `panic_info`");
if let Some(panic_impl_did) = tcx.lang_items().panic_impl()
&& panic_impl_did == hir.local_def_id(fn_id).to_def_id()
{
if let Some(panic_info_did) = tcx.lang_items().panic_info() {
if *declared_ret_ty.kind() != ty::Never {
sess.span_err(decl.output.span(), "return type should be `!`");
}
let inputs = fn_sig.inputs();
let span = hir.span(fn_id);
if inputs.len() == 1 {
let arg_is_panic_info = match *inputs[0].kind() {
ty::Ref(region, ty, mutbl) => match *ty.kind() {
ty::Adt(ref adt, _) => {
adt.did() == panic_info_did
&& mutbl == hir::Mutability::Not
&& !region.is_static()
}
_ => false,
},
_ => false,
};
if !arg_is_panic_info {
sess.span_err(decl.inputs[0].span, "argument should be `&PanicInfo`");
}
if let Node::Item(item) = hir.get(fn_id)
&& let ItemKind::Fn(_, ref generics, _) = item.kind
&& !generics.params.is_empty()
{
sess.span_err(span, "should have no type parameters");
}
} else {
let span = sess.source_map().guess_head_span(span);
sess.span_err(span, "function should have one argument");
}
} else {
sess.err("language item required, but not found: `panic_info`");
}
}
// Check that a function marked as `#[alloc_error_handler]` has signature `fn(Layout) -> !`
if let Some(alloc_error_handler_did) = tcx.lang_items().oom() {
if alloc_error_handler_did == hir.local_def_id(fn_id).to_def_id() {
if let Some(alloc_layout_did) = tcx.lang_items().alloc_layout() {
if *declared_ret_ty.kind() != ty::Never {
sess.span_err(decl.output.span(), "return type should be `!`");
}
let inputs = fn_sig.inputs();
let span = hir.span(fn_id);
if inputs.len() == 1 {
let arg_is_alloc_layout = match inputs[0].kind() {
ty::Adt(ref adt, _) => adt.did() == alloc_layout_did,
_ => false,
};
if !arg_is_alloc_layout {
sess.span_err(decl.inputs[0].span, "argument should be `Layout`");
}
if let Node::Item(item) = hir.get(fn_id)
&& let ItemKind::Fn(_, ref generics, _) = item.kind
&& !generics.params.is_empty()
{
sess.span_err(
span,
"`#[alloc_error_handler]` function should have no type parameters",
);
}
} else {
let span = sess.source_map().guess_head_span(span);
sess.span_err(span, "function should have one argument");
}
} else {
sess.err("language item required, but not found: `alloc_layout`");
if let Some(alloc_error_handler_did) = tcx.lang_items().oom()
&& alloc_error_handler_did == hir.local_def_id(fn_id).to_def_id()
{
if let Some(alloc_layout_did) = tcx.lang_items().alloc_layout() {
if *declared_ret_ty.kind() != ty::Never {
sess.span_err(decl.output.span(), "return type should be `!`");
}
let inputs = fn_sig.inputs();
let span = hir.span(fn_id);
if inputs.len() == 1 {
let arg_is_alloc_layout = match inputs[0].kind() {
ty::Adt(ref adt, _) => adt.did() == alloc_layout_did,
_ => false,
};
if !arg_is_alloc_layout {
sess.span_err(decl.inputs[0].span, "argument should be `Layout`");
}
if let Node::Item(item) = hir.get(fn_id)
&& let ItemKind::Fn(_, ref generics, _) = item.kind
&& !generics.params.is_empty()
{
sess.span_err(
span,
"`#[alloc_error_handler]` function should have no type parameters",
);
}
} else {
let span = sess.source_map().guess_head_span(span);
sess.span_err(span, "function should have one argument");
}
} else {
sess.err("language item required, but not found: `alloc_layout`");
}
}
@ -670,7 +669,7 @@ fn check_opaque_meets_bounds<'tcx>(
Err(ty_err) => {
tcx.sess.delay_span_bug(
span,
&format!("could not unify `{}` with revealed type:\n{}", hidden_type, ty_err,),
&format!("could not unify `{hidden_type}` with revealed type:\n{ty_err}"),
);
}
}
@ -817,10 +816,9 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
tcx.sess,
item.span,
E0044,
"foreign items may not have {} parameters",
kinds,
"foreign items may not have {kinds} parameters",
)
.span_label(item.span, &format!("can't have {} parameters", kinds))
.span_label(item.span, &format!("can't have {kinds} parameters"))
.help(
// FIXME: once we start storing spans for type arguments, turn this
// into a suggestion.
@ -1065,68 +1063,67 @@ pub(super) fn check_representable(tcx: TyCtxt<'_>, sp: Span, item_def_id: LocalD
pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
let t = tcx.type_of(def_id);
if let ty::Adt(def, substs) = t.kind() {
if def.is_struct() {
let fields = &def.non_enum_variant().fields;
if fields.is_empty() {
if let ty::Adt(def, substs) = t.kind()
&& def.is_struct()
{
let fields = &def.non_enum_variant().fields;
if fields.is_empty() {
struct_span_err!(tcx.sess, sp, E0075, "SIMD vector cannot be empty").emit();
return;
}
let e = fields[0].ty(tcx, substs);
if !fields.iter().all(|f| f.ty(tcx, substs) == e) {
struct_span_err!(tcx.sess, sp, E0076, "SIMD vector should be homogeneous")
.span_label(sp, "SIMD elements must have the same type")
.emit();
return;
}
let len = if let ty::Array(_ty, c) = e.kind() {
c.try_eval_usize(tcx, tcx.param_env(def.did()))
} else {
Some(fields.len() as u64)
};
if let Some(len) = len {
if len == 0 {
struct_span_err!(tcx.sess, sp, E0075, "SIMD vector cannot be empty").emit();
return;
}
let e = fields[0].ty(tcx, substs);
if !fields.iter().all(|f| f.ty(tcx, substs) == e) {
struct_span_err!(tcx.sess, sp, E0076, "SIMD vector should be homogeneous")
.span_label(sp, "SIMD elements must have the same type")
.emit();
} else if len > MAX_SIMD_LANES {
struct_span_err!(
tcx.sess,
sp,
E0075,
"SIMD vector cannot have more than {MAX_SIMD_LANES} elements",
)
.emit();
return;
}
}
let len = if let ty::Array(_ty, c) = e.kind() {
c.try_eval_usize(tcx, tcx.param_env(def.did()))
} else {
Some(fields.len() as u64)
};
if let Some(len) = len {
if len == 0 {
struct_span_err!(tcx.sess, sp, E0075, "SIMD vector cannot be empty").emit();
return;
} else if len > MAX_SIMD_LANES {
struct_span_err!(
tcx.sess,
sp,
E0075,
"SIMD vector cannot have more than {} elements",
MAX_SIMD_LANES,
)
.emit();
return;
}
}
// Check that we use types valid for use in the lanes of a SIMD "vector register"
// These are scalar types which directly match a "machine" type
// Yes: Integers, floats, "thin" pointers
// No: char, "fat" pointers, compound types
match e.kind() {
ty::Param(_) => (), // pass struct<T>(T, T, T, T) through, let monomorphization catch errors
ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_) => (), // struct(u8, u8, u8, u8) is ok
ty::Array(t, _) if matches!(t.kind(), ty::Param(_)) => (), // pass struct<T>([T; N]) through, let monomorphization catch errors
ty::Array(t, _clen)
if matches!(
t.kind(),
ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_)
) =>
{ /* struct([f32; 4]) is ok */ }
_ => {
struct_span_err!(
tcx.sess,
sp,
E0077,
"SIMD vector element type should be a \
primitive scalar (integer/float/pointer) type"
)
.emit();
return;
}
// Check that we use types valid for use in the lanes of a SIMD "vector register"
// These are scalar types which directly match a "machine" type
// Yes: Integers, floats, "thin" pointers
// No: char, "fat" pointers, compound types
match e.kind() {
ty::Param(_) => (), // pass struct<T>(T, T, T, T) through, let monomorphization catch errors
ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_) => (), // struct(u8, u8, u8, u8) is ok
ty::Array(t, _) if matches!(t.kind(), ty::Param(_)) => (), // pass struct<T>([T; N]) through, let monomorphization catch errors
ty::Array(t, _clen)
if matches!(
t.kind(),
ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_)
) =>
{ /* struct([f32; 4]) is ok */ }
_ => {
struct_span_err!(
tcx.sess,
sp,
E0077,
"SIMD vector element type should be a \
primitive scalar (integer/float/pointer) type"
)
.emit();
return;
}
}
}
@ -1189,7 +1186,7 @@ pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
ident
)
} else {
format!("...which contains a field of type `{}`", ident)
format!("...which contains a field of type `{ident}`")
},
);
first = false;
@ -1215,13 +1212,12 @@ pub(super) fn check_packed_inner(
stack.push(def_id);
for field in &def.non_enum_variant().fields {
if let ty::Adt(def, _) = field.ty(tcx, substs).kind() {
if !stack.contains(&def.did()) {
if let Some(mut defs) = check_packed_inner(tcx, def.did(), stack) {
defs.push((def.did(), field.ident(tcx).span));
return Some(defs);
}
}
if let ty::Adt(def, _) = field.ty(tcx, substs).kind()
&& !stack.contains(&def.did())
&& let Some(mut defs) = check_packed_inner(tcx, def.did(), stack)
{
defs.push((def.did(), field.ident(tcx).span));
return Some(defs);
}
}
stack.pop();
@ -1370,8 +1366,8 @@ fn check_enum<'tcx>(
"discriminant value `{}` already exists",
discr.val,
)
.span_label(i_span, format!("first use of {}", display_discr_i))
.span_label(span, format!("enum already has {}", display_discr))
.span_label(i_span, format!("first use of {display_discr_i}"))
.span_label(span, format!("enum already has {display_discr}"))
.emit();
}
disr_vals.push(discr);
@ -1393,7 +1389,7 @@ fn display_discriminant_value<'tcx>(
&& let rustc_ast::LitKind::Int(lit_value, _int_kind) = &lit.node
&& evaluated != *lit_value
{
return format!("`{}` (overflowed from `{}`)", evaluated, lit_value);
return format!("`{evaluated}` (overflowed from `{lit_value}`)");
}
}
format!("`{}`", evaluated)
@ -1422,28 +1418,28 @@ pub(super) fn check_type_params_are_used<'tcx>(
}
for leaf in ty.walk() {
if let GenericArgKind::Type(leaf_ty) = leaf.unpack() {
if let ty::Param(param) = leaf_ty.kind() {
debug!("found use of ty param {:?}", param);
params_used.insert(param.index);
}
if let GenericArgKind::Type(leaf_ty) = leaf.unpack()
&& let ty::Param(param) = leaf_ty.kind()
{
debug!("found use of ty param {:?}", param);
params_used.insert(param.index);
}
}
for param in &generics.params {
if !params_used.contains(param.index) {
if let ty::GenericParamDefKind::Type { .. } = param.kind {
let span = tcx.def_span(param.def_id);
struct_span_err!(
tcx.sess,
span,
E0091,
"type parameter `{}` is unused",
param.name,
)
.span_label(span, "unused type parameter")
.emit();
}
if !params_used.contains(param.index)
&& let ty::GenericParamDefKind::Type { .. } = param.kind
{
let span = tcx.def_span(param.def_id);
struct_span_err!(
tcx.sess,
span,
E0091,
"type parameter `{}` is unused",
param.name,
)
.span_label(span, "unused type parameter")
.emit();
}
}
}
@ -1534,10 +1530,10 @@ fn opaque_type_cycle_error(tcx: TyCtxt<'_>, def_id: LocalDefId, span: Span) -> E
for def_id in visitor.0 {
let ty_span = tcx.def_span(def_id);
if !seen.contains(&ty_span) {
err.span_label(ty_span, &format!("returning this opaque type `{}`", ty));
err.span_label(ty_span, &format!("returning this opaque type `{ty}`"));
seen.insert(ty_span);
}
err.span_label(sp, &format!("returning here with type `{}`", ty));
err.span_label(sp, &format!("returning here with type `{ty}`"));
}
}
}

View File

@ -632,11 +632,10 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
let unsize_ty = trait_pred.trait_ref.substs[1].expect_ty();
if let (ty::Dynamic(ref data_a, ..), ty::Dynamic(ref data_b, ..)) =
(self_ty.kind(), unsize_ty.kind())
&& data_a.principal_def_id() != data_b.principal_def_id()
{
if data_a.principal_def_id() != data_b.principal_def_id() {
debug!("coerce_unsized: found trait upcasting coercion");
has_trait_upcasting_coercion = true;
}
debug!("coerce_unsized: found trait upcasting coercion");
has_trait_upcasting_coercion = true;
}
if let ty::Tuple(..) = unsize_ty.kind() {
debug!("coerce_unsized: found unsized tuple coercion");
@ -732,13 +731,12 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
F: FnOnce(Ty<'tcx>) -> Vec<Adjustment<'tcx>>,
G: FnOnce(Ty<'tcx>) -> Vec<Adjustment<'tcx>>,
{
if let ty::FnPtr(fn_ty_b) = b.kind() {
if let (hir::Unsafety::Normal, hir::Unsafety::Unsafe) =
if let ty::FnPtr(fn_ty_b) = b.kind()
&& let (hir::Unsafety::Normal, hir::Unsafety::Unsafe) =
(fn_ty_a.unsafety(), fn_ty_b.unsafety())
{
let unsafe_a = self.tcx.safe_to_unsafe_fn_ty(fn_ty_a);
return self.unify_and(unsafe_a, b, to_unsafe);
}
{
let unsafe_a = self.tcx.safe_to_unsafe_fn_ty(fn_ty_a);
return self.unify_and(unsafe_a, b, to_unsafe);
}
self.unify_and(a, b, normal)
}
@ -783,12 +781,11 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
}
// Safe `#[target_feature]` functions are not assignable to safe fn pointers (RFC 2396).
if let ty::FnDef(def_id, _) = *a.kind() {
if b_sig.unsafety() == hir::Unsafety::Normal
&& !self.tcx.codegen_fn_attrs(def_id).target_features.is_empty()
{
return Err(TypeError::TargetFeatureCast(def_id));
}
if let ty::FnDef(def_id, _) = *a.kind()
&& b_sig.unsafety() == hir::Unsafety::Normal
&& !self.tcx.codegen_fn_attrs(def_id).target_features.is_empty()
{
return Err(TypeError::TargetFeatureCast(def_id));
}
let InferOk { value: a_sig, obligations: o1 } =
@ -1540,11 +1537,11 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
fcx.tcx.hir().get_if_cause(expr.hir_id),
expected.is_unit(),
pointing_at_return_type,
) {
)
// If the block is from an external macro or try (`?`) desugaring, then
// do not suggest adding a semicolon, because there's nowhere to put it.
// See issues #81943 and #87051.
if matches!(
&& matches!(
cond_expr.span.desugaring_kind(),
None | Some(DesugaringKind::WhileLoop)
) && !in_external_macro(fcx.tcx.sess, cond_expr.span)
@ -1552,11 +1549,10 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
cond_expr.kind,
hir::ExprKind::Match(.., hir::MatchSource::TryDesugar)
)
{
err.span_label(cond_expr.span, "expected this to be `()`");
if expr.can_have_side_effects() {
fcx.suggest_semicolon_at_end(cond_expr.span, &mut err);
}
{
err.span_label(cond_expr.span, "expected this to be `()`");
if expr.can_have_side_effects() {
fcx.suggest_semicolon_at_end(cond_expr.span, &mut err);
}
}
fcx.get_node_fn_decl(parent).map(|(fn_decl, _, is_main)| (fn_decl, is_main))
@ -1636,28 +1632,27 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
let has_impl = snippet_iter.next().map_or(false, |s| s == "impl");
// Only suggest `Box<dyn Trait>` if `Trait` in `impl Trait` is object safe.
let mut is_object_safe = false;
if let hir::FnRetTy::Return(ty) = fn_output {
if let hir::FnRetTy::Return(ty) = fn_output
// Get the return type.
if let hir::TyKind::OpaqueDef(..) = ty.kind {
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty);
// Get the `impl Trait`'s `DefId`.
if let ty::Opaque(def_id, _) = ty.kind() {
// Get the `impl Trait`'s `Item` so that we can get its trait bounds and
// get the `Trait`'s `DefId`.
if let hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, .. }) =
fcx.tcx.hir().expect_item(def_id.expect_local()).kind
{
// Are of this `impl Trait`'s traits object safe?
is_object_safe = bounds.iter().all(|bound| {
bound
.trait_ref()
.and_then(|t| t.trait_def_id())
.map_or(false, |def_id| {
fcx.tcx.object_safety_violations(def_id).is_empty()
})
&& let hir::TyKind::OpaqueDef(..) = ty.kind
{
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty);
// Get the `impl Trait`'s `DefId`.
if let ty::Opaque(def_id, _) = ty.kind()
// Get the `impl Trait`'s `Item` so that we can get its trait bounds and
// get the `Trait`'s `DefId`.
&& let hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, .. }) =
fcx.tcx.hir().expect_item(def_id.expect_local()).kind
{
// Are of this `impl Trait`'s traits object safe?
is_object_safe = bounds.iter().all(|bound| {
bound
.trait_ref()
.and_then(|t| t.trait_def_id())
.map_or(false, |def_id| {
fcx.tcx.object_safety_violations(def_id).is_empty()
})
}
}
})
}
};
if has_impl {
@ -1703,7 +1698,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
&& let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty)
&& let ty::Dynamic(..) = ty.kind()
{
return true;
return true;
}
false
}

View File

@ -315,7 +315,7 @@ fn compare_predicate_entailment<'tcx>(
ExplicitSelf::ByReference(_, hir::Mutability::Mut) => {
"&mut self".to_owned()
}
_ => format!("self: {}", ty),
_ => format!("self: {ty}"),
};
// When the `impl` receiver is an arbitrary self type, like `self: Box<Self>`, the
@ -526,7 +526,7 @@ fn compare_self_type<'tcx>(
ExplicitSelf::ByValue => "self".to_owned(),
ExplicitSelf::ByReference(_, hir::Mutability::Not) => "&self".to_owned(),
ExplicitSelf::ByReference(_, hir::Mutability::Mut) => "&mut self".to_owned(),
_ => format!("self: {}", self_arg_ty),
_ => format!("self: {self_arg_ty}"),
}
})
};
@ -544,9 +544,9 @@ fn compare_self_type<'tcx>(
trait_m.name,
self_descr
);
err.span_label(impl_m_span, format!("`{}` used in impl", self_descr));
err.span_label(impl_m_span, format!("`{self_descr}` used in impl"));
if let Some(span) = tcx.hir().span_if_local(trait_m.def_id) {
err.span_label(span, format!("trait method declared without `{}`", self_descr));
err.span_label(span, format!("trait method declared without `{self_descr}`"));
} else {
err.note_trait_signature(trait_m.name.to_string(), trait_m.signature(tcx));
}
@ -564,9 +564,9 @@ fn compare_self_type<'tcx>(
trait_m.name,
self_descr
);
err.span_label(impl_m_span, format!("expected `{}` in impl", self_descr));
err.span_label(impl_m_span, format!("expected `{self_descr}` in impl"));
if let Some(span) = tcx.hir().span_if_local(trait_m.def_id) {
err.span_label(span, format!("`{}` used in trait", self_descr));
err.span_label(span, format!("`{self_descr}` used in trait"));
} else {
err.note_trait_signature(trait_m.name.to_string(), trait_m.signature(tcx));
}
@ -668,7 +668,7 @@ fn compare_number_of_generics<'tcx>(
err.span_label(*span, "");
}
} else {
suffix = Some(format!(", expected {}", trait_count));
suffix = Some(format!(", expected {trait_count}"));
}
if let Some(span) = span {
@ -873,12 +873,10 @@ fn compare_synthetic_generics<'tcx>(
intravisit::walk_ty(self, ty);
if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) =
ty.kind
&& let Res::Def(DefKind::TyParam, def_id) = path.res
&& def_id == self.1
{
if let Res::Def(DefKind::TyParam, def_id) = path.res {
if def_id == self.1 {
self.0 = Some(ty.span);
}
}
self.0 = Some(ty.span);
}
}
}
@ -908,7 +906,7 @@ fn compare_synthetic_generics<'tcx>(
// delete generic parameters
(impl_m.generics.span, String::new()),
// replace param usage with `impl Trait`
(span, format!("impl {}", bounds)),
(span, format!("impl {bounds}")),
],
Applicability::MaybeIncorrect,
);
@ -972,7 +970,7 @@ fn compare_const_param_types<'tcx>(
&format!(
"the const parameter{} has type `{}`, but the declaration \
in trait `{}` has type `{}`",
&impl_ident.map_or_else(|| "".to_string(), |ident| format!(" `{}`", ident)),
&impl_ident.map_or_else(|| "".to_string(), |ident| format!(" `{ident}`")),
impl_ty,
tcx.def_path_str(trait_m.def_id),
trait_ty

View File

@ -241,13 +241,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// We are pointing at the binding's type or initializer value, but it's pattern
// is in a different line, so we point at both.
err.span_label(secondary_span, "expected due to the type of this binding");
err.span_label(primary_span, &format!("expected due to this{}", post_message));
err.span_label(primary_span, &format!("expected due to this{post_message}"));
} else if post_message == "" {
// We are pointing at either the assignment lhs or the binding def pattern.
err.span_label(primary_span, "expected due to the type of this binding");
} else {
// We are pointing at the binding's type or initializer value.
err.span_label(primary_span, &format!("expected due to this{}", post_message));
err.span_label(primary_span, &format!("expected due to this{post_message}"));
}
if !lhs.is_syntactic_place_expr() {
@ -321,7 +321,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"try adding an expression at the end of the block",
return_suggestions
.into_iter()
.map(|r| format!("{}\n{}{}", semicolon, indent, r)),
.map(|r| format!("{semicolon}\n{indent}{r}")),
Applicability::MaybeIncorrect,
);
}
@ -344,10 +344,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let variant_path =
with_no_trimmed_paths!(self.tcx.def_path_str(variant.def_id));
// FIXME #56861: DRYer prelude filtering
if let Some(path) = variant_path.strip_prefix("std::prelude::") {
if let Some((_, path)) = path.split_once("::") {
return Some(path.to_string());
}
if let Some(path) = variant_path.strip_prefix("std::prelude::")
&& let Some((_, path)) = path.split_once("::")
{
return Some(path.to_string());
}
Some(variant_path)
} else {
@ -357,7 +357,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.collect();
let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!("{}: ", ident),
Some(ident) => format!("{ident}: "),
None => String::new(),
};
@ -366,9 +366,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
[variant] => {
// Just a single matching variant.
err.multipart_suggestion_verbose(
&format!("try wrapping the expression in `{}`", variant),
&format!("try wrapping the expression in `{variant}`"),
vec![
(expr.span.shrink_to_lo(), format!("{}{}(", prefix, variant)),
(expr.span.shrink_to_lo(), format!("{prefix}{variant}(")),
(expr.span.shrink_to_hi(), ")".to_string()),
],
Applicability::MaybeIncorrect,
@ -383,7 +383,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
),
compatible_variants.into_iter().map(|variant| {
vec![
(expr.span.shrink_to_lo(), format!("{}{}(", prefix, variant)),
(expr.span.shrink_to_lo(), format!("{prefix}{variant}(")),
(expr.span.shrink_to_hi(), ")".to_string()),
]
}),
@ -680,7 +680,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ if is_range_literal(expr) => true,
_ => false,
};
let sugg_expr = if needs_parens { format!("({})", src) } else { src };
let sugg_expr = if needs_parens { format!("({src})") } else { src };
if let Some(sugg) = self.can_use_as_ref(expr) {
return Some((
@ -693,7 +693,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!("{}: ", ident),
Some(ident) => format!("{ident}: "),
None => String::new(),
};
@ -727,14 +727,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::Mutability::Mut => (
sp,
"consider mutably borrowing here".to_string(),
format!("{}&mut {}", prefix, sugg_expr),
format!("{prefix}&mut {sugg_expr}"),
Applicability::MachineApplicable,
false,
),
hir::Mutability::Not => (
sp,
"consider borrowing here".to_string(),
format!("{}&{}", prefix, sugg_expr),
format!("{prefix}&{sugg_expr}"),
Applicability::MachineApplicable,
false,
),
@ -758,29 +758,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Some(call_span) =
iter::successors(Some(expr.span), |s| s.parent_callsite())
.find(|&s| sp.contains(s))
&& sm.span_to_snippet(call_span).is_ok()
{
if sm.span_to_snippet(call_span).is_ok() {
return Some((
sp.with_hi(call_span.lo()),
"consider removing the borrow".to_string(),
String::new(),
Applicability::MachineApplicable,
true,
));
}
}
return None;
}
if sp.contains(expr.span) {
if sm.span_to_snippet(expr.span).is_ok() {
return Some((
sp.with_hi(expr.span.lo()),
sp.with_hi(call_span.lo()),
"consider removing the borrow".to_string(),
String::new(),
Applicability::MachineApplicable,
true,
));
}
return None;
}
if sp.contains(expr.span)
&& sm.span_to_snippet(expr.span).is_ok()
{
return Some((
sp.with_hi(expr.span.lo()),
"consider removing the borrow".to_string(),
String::new(),
Applicability::MachineApplicable,
true,
));
}
}
(
@ -788,66 +787,65 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&ty::RawPtr(TypeAndMut { ty: ty_b, mutbl: mutbl_b }),
&ty::Ref(_, ty_a, mutbl_a),
) => {
if let Some(steps) = self.deref_steps(ty_a, ty_b) {
if let Some(steps) = self.deref_steps(ty_a, ty_b)
// Only suggest valid if dereferencing needed.
if steps > 0 {
// The pointer type implements `Copy` trait so the suggestion is always valid.
if let Ok(src) = sm.span_to_snippet(sp) {
let derefs = "*".repeat(steps);
if let Some((span, src, applicability)) = match mutbl_b {
&& steps > 0
// The pointer type implements `Copy` trait so the suggestion is always valid.
&& let Ok(src) = sm.span_to_snippet(sp)
{
let derefs = "*".repeat(steps);
if let Some((span, src, applicability)) = match mutbl_b {
hir::Mutability::Mut => {
let new_prefix = "&mut ".to_owned() + &derefs;
match mutbl_a {
hir::Mutability::Mut => {
let new_prefix = "&mut ".to_owned() + &derefs;
match mutbl_a {
hir::Mutability::Mut => {
replace_prefix(&src, "&mut ", &new_prefix).map(|_| {
let pos = sp.lo() + BytePos(5);
let sp = sp.with_lo(pos).with_hi(pos);
(sp, derefs, Applicability::MachineApplicable)
})
}
hir::Mutability::Not => {
replace_prefix(&src, "&", &new_prefix).map(|_| {
let pos = sp.lo() + BytePos(1);
let sp = sp.with_lo(pos).with_hi(pos);
(
sp,
format!("mut {}", derefs),
Applicability::Unspecified,
)
})
}
}
replace_prefix(&src, "&mut ", &new_prefix).map(|_| {
let pos = sp.lo() + BytePos(5);
let sp = sp.with_lo(pos).with_hi(pos);
(sp, derefs, Applicability::MachineApplicable)
})
}
hir::Mutability::Not => {
let new_prefix = "&".to_owned() + &derefs;
match mutbl_a {
hir::Mutability::Mut => {
replace_prefix(&src, "&mut ", &new_prefix).map(|_| {
let lo = sp.lo() + BytePos(1);
let hi = sp.lo() + BytePos(5);
let sp = sp.with_lo(lo).with_hi(hi);
(sp, derefs, Applicability::MachineApplicable)
})
}
hir::Mutability::Not => {
replace_prefix(&src, "&", &new_prefix).map(|_| {
let pos = sp.lo() + BytePos(1);
let sp = sp.with_lo(pos).with_hi(pos);
(sp, derefs, Applicability::MachineApplicable)
})
}
}
replace_prefix(&src, "&", &new_prefix).map(|_| {
let pos = sp.lo() + BytePos(1);
let sp = sp.with_lo(pos).with_hi(pos);
(
sp,
format!("mut {derefs}"),
Applicability::Unspecified,
)
})
}
} {
return Some((
span,
"consider dereferencing".to_string(),
src,
applicability,
true,
));
}
}
hir::Mutability::Not => {
let new_prefix = "&".to_owned() + &derefs;
match mutbl_a {
hir::Mutability::Mut => {
replace_prefix(&src, "&mut ", &new_prefix).map(|_| {
let lo = sp.lo() + BytePos(1);
let hi = sp.lo() + BytePos(5);
let sp = sp.with_lo(lo).with_hi(hi);
(sp, derefs, Applicability::MachineApplicable)
})
}
hir::Mutability::Not => {
replace_prefix(&src, "&", &new_prefix).map(|_| {
let pos = sp.lo() + BytePos(1);
let sp = sp.with_lo(pos).with_hi(pos);
(sp, derefs, Applicability::MachineApplicable)
})
}
}
}
} {
return Some((
span,
"consider dereferencing".to_string(),
src,
applicability,
true,
));
}
}
}
@ -908,7 +906,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Suggest removing `&` if we have removed any, otherwise suggest just
// dereferencing the remaining number of steps.
let message = if remove.is_empty() {
format!("consider {}", deref_kind)
format!("consider {deref_kind}")
} else {
format!(
"consider removing the `{}` and {} instead",
@ -918,7 +916,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!("{}: ", ident),
Some(ident) => format!("{ident}: "),
None => String::new(),
};
@ -994,35 +992,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
};
if let hir::ExprKind::Call(path, args) = &expr.kind {
if let (hir::ExprKind::Path(hir::QPath::TypeRelative(base_ty, path_segment)), 1) =
if let hir::ExprKind::Call(path, args) = &expr.kind
&& let (hir::ExprKind::Path(hir::QPath::TypeRelative(base_ty, path_segment)), 1) =
(&path.kind, args.len())
{
// `expr` is a conversion like `u32::from(val)`, do not suggest anything (#63697).
if let (hir::TyKind::Path(hir::QPath::Resolved(None, base_ty_path)), sym::from) =
(&base_ty.kind, path_segment.ident.name)
{
if let Some(ident) = &base_ty_path.segments.iter().map(|s| s.ident).next() {
match ident.name {
sym::i128
| sym::i64
| sym::i32
| sym::i16
| sym::i8
| sym::u128
| sym::u64
| sym::u32
| sym::u16
| sym::u8
| sym::isize
| sym::usize
if base_ty_path.segments.len() == 1 =>
{
return false;
}
_ => {}
}
// `expr` is a conversion like `u32::from(val)`, do not suggest anything (#63697).
&& let (hir::TyKind::Path(hir::QPath::Resolved(None, base_ty_path)), sym::from) =
(&base_ty.kind, path_segment.ident.name)
{
if let Some(ident) = &base_ty_path.segments.iter().map(|s| s.ident).next() {
match ident.name {
sym::i128
| sym::i64
| sym::i32
| sym::i16
| sym::i8
| sym::u128
| sym::u64
| sym::u32
| sym::u16
| sym::u8
| sym::isize
| sym::usize
if base_ty_path.segments.len() == 1 =>
{
return false;
}
_ => {}
}
}
}
@ -1042,8 +1037,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expected_ty,
);
let lit_msg = format!(
"change the type of the numeric literal from `{}` to `{}`",
checked_ty, expected_ty,
"change the type of the numeric literal from `{checked_ty}` to `{expected_ty}`",
);
let close_paren = if expr.precedence().order() < PREC_POSTFIX {
@ -1054,10 +1048,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
let mut cast_suggestion = sugg.clone();
cast_suggestion
.push((expr.span.shrink_to_hi(), format!("{} as {}", close_paren, expected_ty)));
cast_suggestion.push((expr.span.shrink_to_hi(), format!("{close_paren} as {expected_ty}")));
let mut into_suggestion = sugg.clone();
into_suggestion.push((expr.span.shrink_to_hi(), format!("{}.into()", close_paren)));
into_suggestion.push((expr.span.shrink_to_hi(), format!("{close_paren}.into()")));
let mut suffix_suggestion = sugg.clone();
suffix_suggestion.push((
if matches!(
@ -1074,7 +1067,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
},
if expr.precedence().order() < PREC_POSTFIX {
// Readd `)`
format!("{})", expected_ty)
format!("{expected_ty})")
} else {
expected_ty.to_string()
},
@ -1108,20 +1101,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(lhs_expr_and_src, exp_to_found_is_fallible)
{
let msg = format!(
"you can convert `{}` from `{}` to `{}`, matching the type of `{}`",
lhs_src, expected_ty, checked_ty, src
"you can convert `{lhs_src}` from `{expected_ty}` to `{checked_ty}`, matching the type of `{src}`",
);
let suggestion = vec![
(lhs_expr.span.shrink_to_lo(), format!("{}::from(", checked_ty)),
(lhs_expr.span.shrink_to_lo(), format!("{checked_ty}::from(")),
(lhs_expr.span.shrink_to_hi(), ")".to_string()),
];
(msg, suggestion)
} else {
let msg = format!("{} and panic if the converted value doesn't fit", msg);
let msg = format!("{msg} and panic if the converted value doesn't fit");
let mut suggestion = sugg.clone();
suggestion.push((
expr.span.shrink_to_hi(),
format!("{}.try_into().unwrap()", close_paren),
format!("{close_paren}.try_into().unwrap()"),
));
(msg, suggestion)
};
@ -1151,7 +1143,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// We now know that converting either the lhs or rhs is fallible. Before we
// suggest a fallible conversion, check if the value can never fit in the
// expected type.
let msg = format!("`{}` cannot fit into type `{}`", src, expected_ty);
let msg = format!("`{src}` cannot fit into type `{expected_ty}`");
err.note(&msg);
return;
} else if in_const_context {
@ -1229,7 +1221,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else if can_cast {
// Missing try_into implementation for `f64` to `f32`
err.multipart_suggestion_verbose(
&format!("{}, producing the closest possible value", cast_msg),
&format!("{cast_msg}, producing the closest possible value"),
cast_suggestion,
Applicability::MaybeIncorrect, // lossy conversion
);
@ -1246,7 +1238,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else if can_cast {
// Missing try_into implementation for `{float}` to `{integer}`
err.multipart_suggestion_verbose(
&format!("{}, rounding the float towards zero", msg),
&format!("{msg}, rounding the float towards zero"),
cast_suggestion,
Applicability::MaybeIncorrect, // lossy conversion
);
@ -1258,8 +1250,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if exp.bit_width() > found.bit_width().unwrap_or(256) {
err.multipart_suggestion_verbose(
&format!(
"{}, producing the floating point representation of the integer",
msg,
"{msg}, producing the floating point representation of the integer",
),
into_suggestion,
Applicability::MachineApplicable,
@ -1274,9 +1265,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Missing try_into implementation for `{integer}` to `{float}`
err.multipart_suggestion_verbose(
&format!(
"{}, producing the floating point representation of the integer, \
"{cast_msg}, producing the floating point representation of the integer, \
rounded if necessary",
cast_msg,
),
cast_suggestion,
Applicability::MaybeIncorrect, // lossy conversion
@ -1321,7 +1311,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&ty::Char,
) => {
err.multipart_suggestion_verbose(
&format!("{}, since a `char` always occupies 4 bytes", cast_msg,),
&format!("{cast_msg}, since a `char` always occupies 4 bytes"),
cast_suggestion,
Applicability::MachineApplicable,
);
@ -1333,22 +1323,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Report the type inferred by the return statement.
fn report_closure_inferred_return_type(&self, err: &mut Diagnostic, expected: Ty<'tcx>) {
if let Some(sp) = self.ret_coercion_span.get() {
if let Some(sp) = self.ret_coercion_span.get()
// If the closure has an explicit return type annotation, or if
// the closure's return type has been inferred from outside
// requirements (such as an Fn* trait bound), then a type error
// may occur at the first return expression we see in the closure
// (if it conflicts with the declared return type). Skip adding a
// note in this case, since it would be incorrect.
if !self.return_type_pre_known {
err.span_note(
sp,
&format!(
"return type inferred to be `{}` here",
self.resolve_vars_if_possible(expected)
),
);
}
&& !self.return_type_pre_known
{
err.span_note(
sp,
&format!(
"return type inferred to be `{}` here",
self.resolve_vars_if_possible(expected)
),
);
}
}
}

View File

@ -57,7 +57,7 @@ pub fn check_drop_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), Erro
let span = tcx.def_span(drop_impl_did);
let reported = tcx.sess.delay_span_bug(
span,
&format!("should have been rejected by coherence check: {}", dtor_self_type),
&format!("should have been rejected by coherence check: {dtor_self_type}"),
);
Err(reported)
}
@ -104,8 +104,7 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
item_span,
&format!(
"use the same sequence of generic type, lifetime and const parameters \
as the {} definition",
self_descr,
as the {self_descr} definition",
),
)
.emit();
@ -262,9 +261,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
tcx.sess,
predicate_sp,
E0367,
"`Drop` impl requires `{}` but the {} it is implemented for does not",
predicate,
self_descr,
"`Drop` impl requires `{predicate}` but the {self_descr} it is implemented for does not",
)
.span_note(item_span, "the implementor must specify the same requirement")
.emit();

View File

@ -181,13 +181,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// make this code only run with -Zverbose because it is probably slow
if let Ok(lint_str) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
if !lint_str.contains('\n') {
debug!("expr text: {}", lint_str);
debug!("expr text: {lint_str}");
} else {
let mut lines = lint_str.lines();
if let Some(line0) = lines.next() {
let remaining_lines = lines.count();
debug!("expr text: {}", line0);
debug!("expr text: ...(and {} more lines)", remaining_lines);
debug!("expr text: {line0}");
debug!("expr text: ...(and {remaining_lines} more lines)");
}
}
}
@ -375,8 +375,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expr.span,
oprnd_t,
E0614,
"type `{}` cannot be dereferenced",
oprnd_t,
"type `{oprnd_t}` cannot be dereferenced",
);
let sp = tcx.sess.source_map().start_point(expr.span);
if let Some(sp) =
@ -652,7 +651,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.span_suggestion(
expr.span,
"give it a value of the expected type",
format!("break{} {}", label, val),
format!("break{label} {val}"),
Applicability::HasPlaceholders,
);
}
@ -780,7 +779,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
db.span_label(
span,
format!("expected `{}` because of this return type", snippet),
format!("expected `{snippet}` because of this return type"),
);
}
},
@ -1611,15 +1610,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut truncated_fields_error = String::new();
let remaining_fields_names = match &displayable_field_names[..] {
[field1] => format!("`{}`", field1),
[field1, field2] => format!("`{}` and `{}`", field1, field2),
[field1, field2, field3] => format!("`{}`, `{}` and `{}`", field1, field2, field3),
[field1, field2] => format!("`{field1}` and `{field2}`"),
[field1, field2, field3] => format!("`{field1}`, `{field2}` and `{field3}`"),
_ => {
truncated_fields_error =
format!(" and {} other field{}", len - 3, pluralize!(len - 3));
displayable_field_names
.iter()
.take(3)
.map(|n| format!("`{}`", n))
.map(|n| format!("`{n}`"))
.collect::<Vec<_>>()
.join(", ")
}
@ -1635,10 +1634,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
truncated_fields_error,
adt_ty
);
err.span_label(
span,
format!("missing {}{}", remaining_fields_names, truncated_fields_error),
);
err.span_label(span, format!("missing {remaining_fields_names}{truncated_fields_error}"));
// If the last field is a range literal, but it isn't supposed to be, then they probably
// meant to use functional update syntax.
@ -1693,8 +1689,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx.sess.span_err(
span,
&format!(
"cannot construct `{}` with struct literal syntax due to inaccessible fields",
adt_ty,
"cannot construct `{adt_ty}` with struct literal syntax due to inaccessible fields",
),
);
}
@ -1807,7 +1802,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else {
err.span_label(
field.ident.span,
format!("`{}` does not have this field", ty),
format!("`{ty}` does not have this field"),
);
}
let available_field_names =
@ -1973,8 +1968,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
field.span,
expr_t,
E0610,
"`{}` is a primitive type and therefore doesn't have fields",
expr_t
"`{expr_t}` is a primitive type and therefore doesn't have fields",
)
.emit();
}
@ -2018,7 +2012,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
if add_label {
err.span_label(field_ident.span, &format!("field not found in `{}`", ty));
err.span_label(field_ident.span, &format!("field not found in `{ty}`"));
}
}
@ -2077,10 +2071,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx().sess,
field.span,
E0616,
"field `{}` of {} `{}` is private",
field,
kind_name,
struct_path
"field `{field}` of {kind_name} `{struct_path}` is private",
);
err.span_label(field.span, "private field");
// Also check if an accessible method exists, which is often what is meant.
@ -2088,7 +2079,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{
self.suggest_method_call(
&mut err,
&format!("a method `{}` also exists, call it with parentheses", field),
&format!("a method `{field}` also exists, call it with parentheses"),
field,
expr_t,
expr,
@ -2104,9 +2095,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
field.span,
expr_t,
E0615,
"attempted to take value of method `{}` on type `{}`",
field,
expr_t
"attempted to take value of method `{field}` on type `{expr_t}`",
);
err.span_label(field.span, "method, not a field");
let expr_is_call =
@ -2150,27 +2139,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else {
let mut found = false;
if let ty::RawPtr(ty_and_mut) = expr_t.kind() {
if let ty::Adt(adt_def, _) = ty_and_mut.ty.kind() {
if adt_def.variants().len() == 1
&& adt_def
.variants()
.iter()
.next()
.unwrap()
.fields
.iter()
.any(|f| f.ident(self.tcx) == field)
{
if let Some(dot_loc) = expr_snippet.rfind('.') {
found = true;
err.span_suggestion(
expr.span.with_hi(expr.span.lo() + BytePos::from_usize(dot_loc)),
"to access the field, dereference first",
format!("(*{})", &expr_snippet[0..dot_loc]),
Applicability::MaybeIncorrect,
);
}
if let ty::RawPtr(ty_and_mut) = expr_t.kind()
&& let ty::Adt(adt_def, _) = ty_and_mut.ty.kind()
{
if adt_def.variants().len() == 1
&& adt_def
.variants()
.iter()
.next()
.unwrap()
.fields
.iter()
.any(|f| f.ident(self.tcx) == field)
{
if let Some(dot_loc) = expr_snippet.rfind('.') {
found = true;
err.span_suggestion(
expr.span.with_hi(expr.span.lo() + BytePos::from_usize(dot_loc)),
"to access the field, dereference first",
format!("(*{})", &expr_snippet[0..dot_loc]),
Applicability::MaybeIncorrect,
);
}
}
}
@ -2197,7 +2186,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let param_span = self.tcx.hir().span(param_hir_id);
let param_name = self.tcx.hir().ty_param_name(param_def_id.expect_local());
err.span_label(param_span, &format!("type parameter '{}' declared here", param_name));
err.span_label(param_span, &format!("type parameter '{param_name}' declared here"));
}
fn suggest_fields_on_recordish(
@ -2239,17 +2228,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) {
if let (Some(len), Ok(user_index)) =
(len.try_eval_usize(self.tcx, self.param_env), field.as_str().parse::<u64>())
&& let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span)
{
if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) {
let help = "instead of using tuple indexing, use array indexing";
let suggestion = format!("{}[{}]", base, field);
let applicability = if len < user_index {
Applicability::MachineApplicable
} else {
Applicability::MaybeIncorrect
};
err.span_suggestion(expr.span, help, suggestion, applicability);
}
let help = "instead of using tuple indexing, use array indexing";
let suggestion = format!("{base}[{field}]");
let applicability = if len < user_index {
Applicability::MachineApplicable
} else {
Applicability::MaybeIncorrect
};
err.span_suggestion(expr.span, help, suggestion, applicability);
}
}
@ -2261,8 +2249,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
field: Ident,
) {
if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) {
let msg = format!("`{}` is a raw pointer; try dereferencing it", base);
let suggestion = format!("(*{}).{}", base, field);
let msg = format!("`{base}` is a raw pointer; try dereferencing it");
let suggestion = format!("(*{base}).{field}");
err.span_suggestion(expr.span, &msg, suggestion, Applicability::MaybeIncorrect);
}
}
@ -2281,9 +2269,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
field.span,
expr_t,
E0609,
"no field `{}` on type `{}`",
field,
expr_t
"no field `{field}` on type `{expr_t}`",
);
// try to add a suggestion in case the field is a nested field of a field of the Adt
@ -2307,7 +2293,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.span_suggestion_verbose(
field.span.shrink_to_lo(),
"one of the expressions' fields has a field of the same name",
format!("{}.", field_path_str),
format!("{field_path_str}."),
Applicability::MaybeIncorrect,
);
}
@ -2419,8 +2405,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expr.span,
base_t,
E0608,
"cannot index into a value of type `{}`",
base_t
"cannot index into a value of type `{base_t}`",
);
// Try to give some advice about indexing tuples.
if let ty::Tuple(..) = base_t.kind() {
@ -2434,7 +2419,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.span_suggestion(
expr.span,
"to access tuple elements, use",
format!("{}.{}", snip, i),
format!("{snip}.{i}"),
Applicability::MachineApplicable,
);
needs_note = false;

View File

@ -567,7 +567,7 @@ pub fn check_must_not_suspend_ty<'tcx>(
_ => None,
};
for (i, ty) in fields.iter().enumerate() {
let descr_post = &format!(" in tuple element {}", i);
let descr_post = &format!(" in tuple element {i}");
let span = comps.and_then(|c| c.get(i)).map(|e| e.span).unwrap_or(data.source_span);
if check_must_not_suspend_ty(
fcx,

View File

@ -484,14 +484,14 @@ pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>)
}
Err(_) => {
let msg =
format!("unrecognized platform-specific intrinsic function: `{}`", name);
format!("unrecognized platform-specific intrinsic function: `{name}`");
tcx.sess.struct_span_err(it.span, &msg).emit();
return;
}
}
}
_ => {
let msg = format!("unrecognized platform-specific intrinsic function: `{}`", name);
let msg = format!("unrecognized platform-specific intrinsic function: `{name}`");
tcx.sess.struct_span_err(it.span, &msg).emit();
return;
}

View File

@ -553,13 +553,13 @@ fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId, span: S
// `#[link_section]` may contain arbitrary, or even undefined bytes, but it is
// the consumer's responsibility to ensure all bytes that have been read
// have defined values.
if let Ok(alloc) = tcx.eval_static_initializer(id.to_def_id()) {
if alloc.inner().relocations().len() != 0 {
let msg = "statics with a custom `#[link_section]` must be a \
simple list of bytes on the wasm target with no \
extra levels of indirection such as references";
tcx.sess.span_err(span, msg);
}
if let Ok(alloc) = tcx.eval_static_initializer(id.to_def_id())
&& alloc.inner().relocations().len() != 0
{
let msg = "statics with a custom `#[link_section]` must be a \
simple list of bytes on the wasm target with no \
extra levels of indirection such as references";
tcx.sess.span_err(span, msg);
}
}
@ -587,7 +587,7 @@ fn report_forbidden_specialization(
));
}
Err(cname) => {
err.note(&format!("parent implementation is in crate `{}`", cname));
err.note(&format!("parent implementation is in crate `{cname}`"));
}
}
@ -610,10 +610,9 @@ fn missing_items_err(
tcx.sess,
impl_span,
E0046,
"not all trait items implemented, missing: `{}`",
missing_items_msg
"not all trait items implemented, missing: `{missing_items_msg}`",
);
err.span_label(impl_span, format!("missing `{}` in implementation", missing_items_msg));
err.span_label(impl_span, format!("missing `{missing_items_msg}` in implementation"));
// `Span` before impl block closing brace.
let hi = full_impl_span.hi() - BytePos(1);
@ -628,7 +627,7 @@ fn missing_items_err(
for trait_item in missing_items {
let snippet = suggestion_signature(trait_item, tcx);
let code = format!("{}{}\n{}", padding, snippet, padding);
let msg = format!("implement the missing item: `{}`", snippet);
let msg = format!("implement the missing item: `{snippet}`");
let appl = Applicability::HasPlaceholders;
if let Some(span) = tcx.hir().span_if_local(trait_item.def_id) {
err.span_label(span, format!("`{}` from trait", trait_item.name));
@ -653,10 +652,9 @@ fn missing_items_must_implement_one_of_err(
tcx.sess,
impl_span,
E0046,
"not all trait items implemented, missing one of: `{}`",
missing_items_msg
"not all trait items implemented, missing one of: `{missing_items_msg}`",
);
err.span_label(impl_span, format!("missing one of `{}` in implementation", missing_items_msg));
err.span_label(impl_span, format!("missing one of `{missing_items_msg}` in implementation"));
if let Some(annotation_span) = annotation_span {
err.span_note(annotation_span, "required because of this annotation");
@ -749,9 +747,10 @@ fn fn_sig_suggestion<'tcx>(
Some(match ty.kind() {
ty::Param(_) if assoc.fn_has_self_parameter && i == 0 => "self".to_string(),
ty::Ref(reg, ref_ty, mutability) if i == 0 => {
let reg = match &format!("{}", reg)[..] {
"'_" | "" => String::new(),
reg => format!("{} ", reg),
let reg = format!("{reg} ");
let reg = match &reg[..] {
"'_ " | " " => "",
reg => reg,
};
if assoc.fn_has_self_parameter {
match ref_ty.kind() {
@ -759,17 +758,17 @@ fn fn_sig_suggestion<'tcx>(
format!("&{}{}self", reg, mutability.prefix_str())
}
_ => format!("self: {}", ty),
_ => format!("self: {ty}"),
}
} else {
format!("_: {}", ty)
format!("_: {ty}")
}
}
_ => {
if assoc.fn_has_self_parameter && i == 0 {
format!("self: {}", ty)
format!("self: {ty}")
} else {
format!("_: {}", ty)
format!("_: {ty}")
}
}
})
@ -779,7 +778,7 @@ fn fn_sig_suggestion<'tcx>(
.collect::<Vec<String>>()
.join(", ");
let output = sig.output();
let output = if !output.is_unit() { format!(" -> {}", output) } else { String::new() };
let output = if !output.is_unit() { format!(" -> {output}") } else { String::new() };
let unsafety = sig.unsafety.prefix_str();
let (generics, where_clauses) = bounds_from_generic_predicates(tcx, predicates);
@ -789,10 +788,7 @@ fn fn_sig_suggestion<'tcx>(
// lifetimes between the `impl` and the `trait`, but this should be good enough to
// fill in a significant portion of the missing code, and other subsequent
// suggestions can help the user fix the code.
format!(
"{}fn {}{}({}){}{} {{ todo!() }}",
unsafety, ident, generics, args, output, where_clauses
)
format!("{unsafety}fn {ident}{generics}({args}){output}{where_clauses} {{ todo!() }}")
}
/// Return placeholder code for the given associated item.
@ -830,7 +826,7 @@ fn bad_variant_count<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>, sp: Span, d
.map(|variant| tcx.hir().span_if_local(variant.def_id).unwrap())
.collect();
let msg = format!("needs exactly one variant, but has {}", adt.variants().len(),);
let mut err = struct_span_err!(tcx.sess, sp, E0731, "transparent enum {}", msg);
let mut err = struct_span_err!(tcx.sess, sp, E0731, "transparent enum {msg}");
err.span_label(sp, &msg);
if let [start @ .., end] = &*variant_spans {
for variant_span in start {
@ -850,7 +846,7 @@ fn bad_non_zero_sized_fields<'tcx>(
field_spans: impl Iterator<Item = Span>,
sp: Span,
) {
let msg = format!("needs at most one non-zero-sized field, but has {}", field_count);
let msg = format!("needs at most one non-zero-sized field, but has {field_count}");
let mut err = struct_span_err!(
tcx.sess,
sp,
@ -877,7 +873,7 @@ fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span) {
tcx.sess
.source_map()
.span_to_snippet(span)
.map_or_else(|_| String::new(), |s| format!(" `{}`", s)),
.map_or_else(|_| String::new(), |s| format!(" `{s}`",)),
)
.emit();
}

View File

@ -405,16 +405,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut pat_ty = ty;
if let hir::ExprKind::Lit(Spanned { node: ast::LitKind::ByteStr(_), .. }) = lt.kind {
let expected = self.structurally_resolved_type(span, expected);
if let ty::Ref(_, inner_ty, _) = expected.kind() {
if matches!(inner_ty.kind(), ty::Slice(_)) {
let tcx = self.tcx;
trace!(?lt.hir_id.local_id, "polymorphic byte string lit");
self.typeck_results
.borrow_mut()
.treat_byte_string_as_slice
.insert(lt.hir_id.local_id);
pat_ty = tcx.mk_imm_ref(tcx.lifetimes.re_static, tcx.mk_slice(tcx.types.u8));
}
if let ty::Ref(_, inner_ty, _) = expected.kind()
&& matches!(inner_ty.kind(), ty::Slice(_))
{
let tcx = self.tcx;
trace!(?lt.hir_id.local_id, "polymorphic byte string lit");
self.typeck_results
.borrow_mut()
.treat_byte_string_as_slice
.insert(lt.hir_id.local_id);
pat_ty = tcx.mk_imm_ref(tcx.lifetimes.re_static, tcx.mk_slice(tcx.types.u8));
}
}
@ -481,14 +481,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Unify each side with `expected`.
// Subtyping doesn't matter here, as the value is some kind of scalar.
let demand_eqtype = |x: &mut _, y| {
if let Some((ref mut fail, x_ty, x_span)) = *x {
if let Some(mut err) = self.demand_eqtype_pat_diag(x_span, expected, x_ty, ti) {
if let Some((_, y_ty, y_span)) = y {
self.endpoint_has_type(&mut err, y_span, y_ty);
}
err.emit();
*fail = true;
};
if let Some((ref mut fail, x_ty, x_span)) = *x
&& let Some(mut err) = self.demand_eqtype_pat_diag(x_span, expected, x_ty, ti)
{
if let Some((_, y_ty, y_span)) = y {
self.endpoint_has_type(&mut err, y_span, y_ty);
}
err.emit();
*fail = true;
}
};
demand_eqtype(&mut lhs, rhs);
@ -630,7 +630,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Some(mut err) = self.demand_eqtype_pat_diag(span, var_ty, ty, ti) {
let hir = self.tcx.hir();
let var_ty = self.resolve_vars_with_obligations(var_ty);
let msg = format!("first introduced with type `{}` here", var_ty);
let msg = format!("first introduced with type `{var_ty}` here");
err.span_label(hir.span(var_id), msg);
let in_match = hir.parent_iter(var_id).any(|(_, n)| {
matches!(
@ -665,8 +665,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{
err.span_suggestion(
*span,
&format!("did you mean `{}`", snippet),
format!(" &{}", expected),
&format!("did you mean `{snippet}`"),
format!(" &{expected}"),
Applicability::MachineApplicable,
);
}
@ -701,7 +701,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"type `{}` cannot be dereferenced",
type_str
);
err.span_label(span, format!("type `{}` cannot be dereferenced", type_str));
err.span_label(span, format!("type `{type_str}` cannot be dereferenced"));
if self.tcx.sess.teach(&err.get_code().unwrap()) {
err.note(CANNOT_IMPLICITLY_DEREF_POINTER_TRAIT_OBJ);
}
@ -918,7 +918,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
path_str
);
let mut err = struct_span_err!(tcx.sess, pat.span, E0164, "{}", msg);
let mut err = struct_span_err!(tcx.sess, pat.span, E0164, "{msg}");
match res {
Res::Def(DefKind::Fn | DefKind::AssocFn, _) => {
err.span_label(pat.span, "`fn` calls are not allowed in patterns");
@ -1396,8 +1396,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx.sess,
pat.span,
E0769,
"tuple variant `{}` written as struct variant",
path
"tuple variant `{path}` written as struct variant",
);
err.span_suggestion_verbose(
qpath.span().shrink_to_hi().to(pat.span.shrink_to_hi()),
@ -1422,8 +1421,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
sess,
pat.span,
E0638,
"`..` required with {} marked as non-exhaustive",
descr
"`..` required with {descr} marked as non-exhaustive",
);
err.span_suggestion_verbose(
sp_comma,
@ -1442,8 +1440,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"field `{}` bound multiple times in the pattern",
ident
)
.span_label(span, format!("multiple uses of `{}` in pattern", ident))
.span_label(other_field, format!("first use of `{}`", ident))
.span_label(span, format!("multiple uses of `{ident}` in pattern"))
.span_label(other_field, format!("first use of `{ident}`"))
.emit();
}

View File

@ -74,9 +74,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let ty = self.resolve_vars_if_possible(ty);
let mut err = self.tcx.sess.struct_span_err(
span,
&format!("negative integers cannot be used to index on a `{}`", ty),
&format!("negative integers cannot be used to index on a `{ty}`"),
);
err.span_label(span, &format!("cannot use a negative integer for indexing on `{}`", ty));
err.span_label(span, &format!("cannot use a negative integer for indexing on `{ty}`"));
if let (hir::ExprKind::Path(..), Ok(snippet)) =
(&base_expr.kind, self.tcx.sess.source_map().span_to_snippet(base_expr.span))
{
@ -84,10 +84,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.span_suggestion_verbose(
span.shrink_to_lo(),
&format!(
"to access an element starting from the end of the `{}`, compute the index",
ty,
"to access an element starting from the end of the `{ty}`, compute the index",
),
format!("{}.len() ", snippet),
format!("{snippet}.len() "),
Applicability::MachineApplicable,
);
}
@ -314,32 +313,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.typeck_results.borrow_mut().adjustments_mut().remove(expr.hir_id);
if let Some(mut adjustments) = previous_adjustments {
for adjustment in &mut adjustments {
if let Adjust::Deref(Some(ref mut deref)) = adjustment.kind {
if let Some(ok) = self.try_mutable_overloaded_place_op(
if let Adjust::Deref(Some(ref mut deref)) = adjustment.kind
&& let Some(ok) = self.try_mutable_overloaded_place_op(
expr.span,
source,
&[],
PlaceOp::Deref,
) {
let method = self.register_infer_ok_obligations(ok);
if let ty::Ref(region, _, mutbl) = *method.sig.output().kind() {
*deref = OverloadedDeref { region, mutbl, span: deref.span };
}
// If this is a union field, also throw an error for `DerefMut` of `ManuallyDrop` (see RFC 2514).
// This helps avoid accidental drops.
if inside_union
&& source.ty_adt_def().map_or(false, |adt| adt.is_manually_drop())
{
let mut err = self.tcx.sess.struct_span_err(
expr.span,
"not automatically applying `DerefMut` on `ManuallyDrop` union field",
);
err.help(
"writing to this reference calls the destructor for the old value",
);
err.help("add an explicit `*` if that is desired, or call `ptr::write` to not run the destructor");
err.emit();
}
)
{
let method = self.register_infer_ok_obligations(ok);
if let ty::Ref(region, _, mutbl) = *method.sig.output().kind() {
*deref = OverloadedDeref { region, mutbl, span: deref.span };
}
// If this is a union field, also throw an error for `DerefMut` of `ManuallyDrop` (see RFC 2514).
// This helps avoid accidental drops.
if inside_union
&& source.ty_adt_def().map_or(false, |adt| adt.is_manually_drop())
{
let mut err = self.tcx.sess.struct_span_err(
expr.span,
"not automatically applying `DerefMut` on `ManuallyDrop` union field",
);
err.help(
"writing to this reference calls the destructor for the old value",
);
err.help("add an explicit `*` if that is desired, or call `ptr::write` to not run the destructor");
err.emit();
}
}
source = adjustment.target;

View File

@ -317,13 +317,8 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
self.body_id = body_id.hir_id;
self.body_owner = self.tcx.hir().body_owner_def_id(body_id);
let fn_sig = {
match self.typeck_results.borrow().liberated_fn_sigs().get(id) {
Some(f) => *f,
None => {
bug!("No fn-sig entry for id={:?}", id);
}
}
let Some(fn_sig) = self.typeck_results.borrow().liberated_fn_sigs().get(id) else {
bug!("No fn-sig entry for id={:?}", id);
};
// Collect the types from which we create inferred bounds.
@ -642,12 +637,9 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
ignore_err!(self.with_mc(|mc| {
mc.cat_pattern(discr_cmt, root_pat, |sub_cmt, hir::Pat { kind, span, hir_id, .. }| {
// `ref x` pattern
if let PatKind::Binding(..) = kind {
if let Some(ty::BindByReference(mutbl)) =
mc.typeck_results.extract_binding_mode(self.tcx.sess, *hir_id, *span)
{
self.link_region_from_node_type(*span, *hir_id, mutbl, sub_cmt);
}
if let PatKind::Binding(..) = kind
&& let Some(ty::BindByReference(mutbl)) = mc.typeck_results.extract_binding_mode(self.tcx.sess, *hir_id, *span) {
self.link_region_from_node_type(*span, *hir_id, mutbl, sub_cmt);
}
})
}));

View File

@ -862,7 +862,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
diagnostics_builder.span_suggestion(
closure_body_span.with_lo(closure_body_span.lo() + BytePos::from_usize(line1.len())).shrink_to_lo(),
&diagnostic_msg,
format!("\n{}{};", indent, migration_string),
format!("\n{indent}{migration_string};"),
Applicability::MachineApplicable,
);
} else if line1.starts_with('{') {
@ -873,7 +873,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
diagnostics_builder.span_suggestion(
closure_body_span.with_lo(closure_body_span.lo() + BytePos(1)).shrink_to_lo(),
&diagnostic_msg,
format!(" {};", migration_string),
format!(" {migration_string};"),
Applicability::MachineApplicable,
);
} else {
@ -882,7 +882,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
diagnostics_builder.multipart_suggestion(
&diagnostic_msg,
vec![
(closure_body_span.shrink_to_lo(), format!("{{ {}; ", migration_string)),
(closure_body_span.shrink_to_lo(), format!("{{ {migration_string}; ")),
(closure_body_span.shrink_to_hi(), " }".to_string()),
],
Applicability::MachineApplicable
@ -1527,7 +1527,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx.sess.struct_span_err(closure_span, "First Pass analysis includes:");
for (place, capture_info) in capture_information {
let capture_str = construct_capture_info_string(self.tcx, place, capture_info);
let output_str = format!("Capturing {}", capture_str);
let output_str = format!("Capturing {capture_str}");
let span =
capture_info.path_expr_id.map_or(closure_span, |e| self.tcx.hir().span(e));
@ -1552,7 +1552,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let capture_str =
construct_capture_info_string(self.tcx, place, capture_info);
let output_str = format!("Min Capture {}", capture_str);
let output_str = format!("Min Capture {capture_str}");
if capture.info.path_expr_id != capture.info.capture_kind_expr_id {
let path_span = capture_info
@ -1969,7 +1969,7 @@ fn construct_place_string<'tcx>(tcx: TyCtxt<'_>, place: &Place<'tcx>) -> String
projections_str.push_str(proj.as_str());
}
format!("{}[{}]", variable_name, projections_str)
format!("{variable_name}[{projections_str}]")
}
fn construct_capture_kind_reason_string<'tcx>(
@ -1984,13 +1984,13 @@ fn construct_capture_kind_reason_string<'tcx>(
ty::UpvarCapture::ByRef(kind) => format!("{:?}", kind),
};
format!("{} captured as {} here", place_str, capture_kind_str)
format!("{place_str} captured as {capture_kind_str} here")
}
fn construct_path_string<'tcx>(tcx: TyCtxt<'_>, place: &Place<'tcx>) -> String {
let place_str = construct_place_string(tcx, place);
format!("{} used here", place_str)
format!("{place_str} used here")
}
fn construct_capture_info_string<'tcx>(
@ -2004,7 +2004,7 @@ fn construct_capture_info_string<'tcx>(
ty::UpvarCapture::ByValue => "ByValue".into(),
ty::UpvarCapture::ByRef(kind) => format!("{:?}", kind),
};
format!("{} -> {}", place_str, capture_kind_str)
format!("{place_str} -> {capture_kind_str}")
}
fn var_name(tcx: TyCtxt<'_>, var_hir_id: hir::HirId) -> Symbol {
@ -2035,16 +2035,16 @@ fn migration_suggestion_for_2229(
.collect::<Vec<_>>();
let migration_ref_concat =
need_migrations_variables.iter().map(|v| format!("&{}", v)).collect::<Vec<_>>().join(", ");
need_migrations_variables.iter().map(|v| format!("&{v}")).collect::<Vec<_>>().join(", ");
let migration_string = if 1 == need_migrations.len() {
format!("let _ = {}", migration_ref_concat)
format!("let _ = {migration_ref_concat}")
} else {
format!("let _ = ({})", migration_ref_concat)
format!("let _ = ({migration_ref_concat})")
};
let migrated_variables_concat =
need_migrations_variables.iter().map(|v| format!("`{}`", v)).collect::<Vec<_>>().join(", ");
need_migrations_variables.iter().map(|v| format!("`{v}`")).collect::<Vec<_>>().join(", ");
(migration_string, migrated_variables_concat)
}

View File

@ -230,8 +230,7 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
.struct_span_err(
self_ty.span,
&format!(
"first argument of `call` in `{}` lang item must be a reference",
fn_lang_item_name
"first argument of `call` in `{fn_lang_item_name}` lang item must be a reference",
),
)
.emit();
@ -241,8 +240,7 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
.struct_span_err(
*span,
&format!(
"`call` function in `{}` lang item takes exactly two arguments",
fn_lang_item_name
"`call` function in `{fn_lang_item_name}` lang item takes exactly two arguments",
),
)
.emit();
@ -252,8 +250,7 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
.struct_span_err(
trait_item.span,
&format!(
"`call` trait item in `{}` lang item must be a function",
fn_lang_item_name
"`call` trait item in `{fn_lang_item_name}` lang item must be a function",
),
)
.emit();
@ -432,7 +429,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
);
err.span_suggestion(
gat_item_hir.generics.where_clause.tail_span_for_suggestion(),
&format!("add the required where clause{}", plural),
&format!("add the required where clause{plural}"),
suggestion,
Applicability::MachineApplicable,
);
@ -523,7 +520,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
// In our example, requires that `Self: 'a`
if ty_known_to_outlive(tcx, item_hir, param_env, &wf_tys, *ty, *region_a) {
debug!(?ty_idx, ?region_a_idx);
debug!("required clause: {} must outlive {}", ty, region_a);
debug!("required clause: {ty} must outlive {region_a}");
// Translate into the generic parameters of the GAT. In
// our example, the type was `Self`, which will also be
// `Self` in the GAT.
@ -560,7 +557,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
}
if region_known_to_outlive(tcx, item_hir, param_env, &wf_tys, *region_a, *region_b) {
debug!(?region_a_idx, ?region_b_idx);
debug!("required clause: {} must outlive {}", region_a, region_b);
debug!("required clause: {region_a} must outlive {region_b}");
// Translate into the generic parameters of the GAT.
let region_a_param = gat_generics.param_at(*region_a_idx, tcx);
let region_a_param =
@ -869,7 +866,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
)
.span_label(
hir_ty.span,
format!("`{}` doesn't derive both `PartialEq` and `Eq`", ty),
format!("`{ty}` doesn't derive both `PartialEq` and `Eq`"),
)
.emit();
}
@ -884,7 +881,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
ty::RawPtr(_) => Some("raw pointers"),
_ => {
is_ptr = false;
err_ty_str = format!("`{}`", ty);
err_ty_str = format!("`{ty}`");
Some(err_ty_str.as_str())
}
};
@ -894,16 +891,14 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
tcx.sess.span_err(
hir_ty.span,
&format!(
"using {} as const generic parameters is forbidden",
unsupported_type
"using {unsupported_type} as const generic parameters is forbidden",
),
);
} else {
let mut err = tcx.sess.struct_span_err(
hir_ty.span,
&format!(
"{} is forbidden as the type of a const generic parameter",
unsupported_type
"{unsupported_type} is forbidden as the type of a const generic parameter",
),
);
err.note("the only supported types are integers, `bool` and `char`");
@ -1567,9 +1562,8 @@ fn check_method_receiver<'fcx, 'tcx>(
sym::arbitrary_self_types,
span,
&format!(
"`{}` cannot be used as the type of `self` without \
"`{receiver_ty}` cannot be used as the type of `self` without \
the `arbitrary_self_types` feature",
receiver_ty,
),
)
.help(HELP_FOR_SELF_TYPE)
@ -1587,8 +1581,7 @@ fn e0307<'tcx>(fcx: &FnCtxt<'_, 'tcx>, span: Span, receiver_ty: Ty<'_>) {
fcx.tcx.sess.diagnostic(),
span,
E0307,
"invalid `self` parameter type: {}",
receiver_ty,
"invalid `self` parameter type: {receiver_ty}"
)
.note("type of `self` must be `Self` or a type that dereferences to it")
.help(HELP_FOR_SELF_TYPE)
@ -1793,7 +1786,7 @@ fn report_bivariance(
tcx.def_path_str(def_id),
)
} else {
format!("consider removing `{}` or referring to it in a field", param_name)
format!("consider removing `{param_name}` or referring to it in a field")
};
err.help(&msg);
@ -1993,8 +1986,7 @@ fn error_392(
span: Span,
param_name: Symbol,
) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut err =
struct_span_err!(tcx.sess, span, E0392, "parameter `{}` is never used", param_name);
let mut err = struct_span_err!(tcx.sess, span, E0392, "parameter `{param_name}` is never used");
err.span_label(span, "unused parameter");
err
}