1
Fork 0

Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister

Simplify some nested `if` statements

Applies some but not all instances of `clippy::collapsible_if`. Some ended up looking worse afterwards, though, so I left those out. Also applies instances of `clippy::collapsible_else_if`

Review with whitespace disabled please.
This commit is contained in:
Stuart Cook 2024-09-12 20:37:16 +10:00 committed by GitHub
commit 3ba12756d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
61 changed files with 563 additions and 671 deletions

View file

@ -1151,42 +1151,40 @@ pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
"type has conflicting packed and align representation hints"
)
.emit();
} else {
if let Some(def_spans) = check_packed_inner(tcx, def.did(), &mut vec![]) {
let mut err = struct_span_code_err!(
tcx.dcx(),
sp,
E0588,
"packed type cannot transitively contain a `#[repr(align)]` type"
);
} else if let Some(def_spans) = check_packed_inner(tcx, def.did(), &mut vec![]) {
let mut err = struct_span_code_err!(
tcx.dcx(),
sp,
E0588,
"packed type cannot transitively contain a `#[repr(align)]` type"
);
err.span_note(
tcx.def_span(def_spans[0].0),
format!("`{}` has a `#[repr(align)]` attribute", tcx.item_name(def_spans[0].0)),
);
err.span_note(
tcx.def_span(def_spans[0].0),
format!("`{}` has a `#[repr(align)]` attribute", tcx.item_name(def_spans[0].0)),
);
if def_spans.len() > 2 {
let mut first = true;
for (adt_def, span) in def_spans.iter().skip(1).rev() {
let ident = tcx.item_name(*adt_def);
err.span_note(
*span,
if first {
format!(
"`{}` contains a field of type `{}`",
tcx.type_of(def.did()).instantiate_identity(),
ident
)
} else {
format!("...which contains a field of type `{ident}`")
},
);
first = false;
}
if def_spans.len() > 2 {
let mut first = true;
for (adt_def, span) in def_spans.iter().skip(1).rev() {
let ident = tcx.item_name(*adt_def);
err.span_note(
*span,
if first {
format!(
"`{}` contains a field of type `{}`",
tcx.type_of(def.did()).instantiate_identity(),
ident
)
} else {
format!("...which contains a field of type `{ident}`")
},
);
first = false;
}
err.emit();
}
err.emit();
}
}
}

View file

@ -186,17 +186,15 @@ fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId) {
if let Ok(alloc) = tcx.eval_static_initializer(id.to_def_id())
&& alloc.inner().provenance().ptrs().len() != 0
{
if attrs
&& attrs
.link_section
.map(|link_section| !link_section.as_str().starts_with(".init_array"))
.unwrap()
{
let msg = "statics with a custom `#[link_section]` must be a \
{
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.dcx().span_err(tcx.def_span(id), msg);
}
tcx.dcx().span_err(tcx.def_span(id), msg);
}
}

View file

@ -53,17 +53,15 @@ fn enforce_trait_manually_implementable(
) -> Result<(), ErrorGuaranteed> {
let impl_header_span = tcx.def_span(impl_def_id);
if tcx.is_lang_item(trait_def_id, LangItem::Freeze) {
if !tcx.features().freeze_impls {
feature_err(
&tcx.sess,
sym::freeze_impls,
impl_header_span,
"explicit impls for the `Freeze` trait are not permitted",
)
.with_span_label(impl_header_span, format!("impl of `Freeze` not allowed"))
.emit();
}
if tcx.is_lang_item(trait_def_id, LangItem::Freeze) && !tcx.features().freeze_impls {
feature_err(
&tcx.sess,
sym::freeze_impls,
impl_header_span,
"explicit impls for the `Freeze` trait are not permitted",
)
.with_span_label(impl_header_span, format!("impl of `Freeze` not allowed"))
.emit();
}
// Disallow *all* explicit impls of traits marked `#[rustc_deny_explicit_impl]`

View file

@ -381,24 +381,22 @@ pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>(
}
mir_opaque_ty.ty
} else if let Some(guar) = tables.tainted_by_errors {
// Some error in the owner fn prevented us from populating
// the `concrete_opaque_types` table.
Ty::new_error(tcx, guar)
} else {
if let Some(guar) = tables.tainted_by_errors {
// Some error in the owner fn prevented us from populating
// the `concrete_opaque_types` table.
Ty::new_error(tcx, guar)
// Fall back to the RPIT we inferred during HIR typeck
if let Some(hir_opaque_ty) = hir_opaque_ty {
hir_opaque_ty.ty
} else {
// Fall back to the RPIT we inferred during HIR typeck
if let Some(hir_opaque_ty) = hir_opaque_ty {
hir_opaque_ty.ty
} else {
// We failed to resolve the opaque type or it
// resolves to itself. We interpret this as the
// no values of the hidden type ever being constructed,
// so we can just make the hidden type be `!`.
// For backwards compatibility reasons, we fall back to
// `()` until we the diverging default is changed.
Ty::new_diverging_default(tcx)
}
// We failed to resolve the opaque type or it
// resolves to itself. We interpret this as the
// no values of the hidden type ever being constructed,
// so we can just make the hidden type be `!`.
// For backwards compatibility reasons, we fall back to
// `()` until we the diverging default is changed.
Ty::new_diverging_default(tcx)
}
}
}

View file

@ -827,20 +827,18 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
if num_generic_args_supplied_to_trait + num_assoc_fn_excess_args
== num_trait_generics_except_self
&& let Some(span) = self.gen_args.span_ext()
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
{
if let Some(span) = self.gen_args.span_ext()
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
{
let sugg = vec![
(
self.path_segment.ident.span,
format!("{}::{}", snippet, self.path_segment.ident),
),
(span.with_lo(self.path_segment.ident.span.hi()), "".to_owned()),
];
let sugg = vec![
(
self.path_segment.ident.span,
format!("{}::{}", snippet, self.path_segment.ident),
),
(span.with_lo(self.path_segment.ident.span.hi()), "".to_owned()),
];
err.multipart_suggestion(msg, sugg, Applicability::MaybeIncorrect);
}
err.multipart_suggestion(msg, sugg, Applicability::MaybeIncorrect);
}
}
}

View file

@ -562,13 +562,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
tcx.const_param_default(param.def_id)
.instantiate(tcx, preceding_args)
.into()
} else if infer_args {
self.lowerer.ct_infer(Some(param), self.span).into()
} else {
if infer_args {
self.lowerer.ct_infer(Some(param), self.span).into()
} else {
// We've already errored above about the mismatch.
ty::Const::new_misc_error(tcx).into()
}
// We've already errored above about the mismatch.
ty::Const::new_misc_error(tcx).into()
}
}
}