Skip to content

Commit 954419a

Browse files
Simplify some nested if statements
1 parent 5a2dd7d commit 954419a

File tree

36 files changed

+339
-379
lines changed

36 files changed

+339
-379
lines changed

compiler/rustc_ast_lowering/src/index.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,24 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
7878

7979
// Make sure that the DepNode of some node coincides with the HirId
8080
// owner of that node.
81-
if cfg!(debug_assertions) {
82-
if hir_id.owner != self.owner {
83-
span_bug!(
84-
span,
85-
"inconsistent HirId at `{:?}` for `{:?}`: \
81+
if cfg!(debug_assertions) && hir_id.owner != self.owner {
82+
span_bug!(
83+
span,
84+
"inconsistent HirId at `{:?}` for `{:?}`: \
8685
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
87-
self.tcx.sess.source_map().span_to_diagnostic_string(span),
88-
node,
89-
self.tcx
90-
.definitions_untracked()
91-
.def_path(self.owner.def_id)
92-
.to_string_no_crate_verbose(),
93-
self.owner,
94-
self.tcx
95-
.definitions_untracked()
96-
.def_path(hir_id.owner.def_id)
97-
.to_string_no_crate_verbose(),
98-
hir_id.owner,
99-
)
100-
}
86+
self.tcx.sess.source_map().span_to_diagnostic_string(span),
87+
node,
88+
self.tcx
89+
.definitions_untracked()
90+
.def_path(self.owner.def_id)
91+
.to_string_no_crate_verbose(),
92+
self.owner,
93+
self.tcx
94+
.definitions_untracked()
95+
.def_path(hir_id.owner.def_id)
96+
.to_string_no_crate_verbose(),
97+
hir_id.owner,
98+
)
10199
}
102100

103101
self.nodes[hir_id.local_id] = ParentedNode { parent: self.parent_node, node };

compiler/rustc_ast_passes/src/ast_validation.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,13 @@ impl<'a> AstValidator<'a> {
447447
fn check_item_safety(&self, span: Span, safety: Safety) {
448448
match self.extern_mod_safety {
449449
Some(extern_safety) => {
450-
if matches!(safety, Safety::Unsafe(_) | Safety::Safe(_)) {
451-
if extern_safety == Safety::Default {
452-
self.dcx().emit_err(errors::InvalidSafetyOnExtern {
453-
item_span: span,
454-
block: Some(self.current_extern_span().shrink_to_lo()),
455-
});
456-
}
450+
if matches!(safety, Safety::Unsafe(_) | Safety::Safe(_))
451+
&& extern_safety == Safety::Default
452+
{
453+
self.dcx().emit_err(errors::InvalidSafetyOnExtern {
454+
item_span: span,
455+
block: Some(self.current_extern_span().shrink_to_lo()),
456+
});
457457
}
458458
}
459459
None => {

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+22-25
Original file line numberDiff line numberDiff line change
@@ -2574,33 +2574,31 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
25742574
}
25752575
impl<'hir> Visitor<'hir> for ExpressionFinder<'hir> {
25762576
fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
2577-
if e.span.contains(self.capture_span) {
2578-
if let hir::ExprKind::Closure(&hir::Closure {
2577+
if e.span.contains(self.capture_span)
2578+
&& let hir::ExprKind::Closure(&hir::Closure {
25792579
kind: hir::ClosureKind::Closure,
25802580
body,
25812581
fn_arg_span,
25822582
fn_decl: hir::FnDecl { inputs, .. },
25832583
..
25842584
}) = e.kind
2585-
&& let hir::Node::Expr(body) = self.tcx.hir_node(body.hir_id)
2586-
{
2587-
self.suggest_arg = "this: &Self".to_string();
2588-
if inputs.len() > 0 {
2589-
self.suggest_arg.push_str(", ");
2590-
}
2591-
self.in_closure = true;
2592-
self.closure_arg_span = fn_arg_span;
2593-
self.visit_expr(body);
2594-
self.in_closure = false;
2585+
&& let hir::Node::Expr(body) = self.tcx.hir_node(body.hir_id)
2586+
{
2587+
self.suggest_arg = "this: &Self".to_string();
2588+
if inputs.len() > 0 {
2589+
self.suggest_arg.push_str(", ");
25952590
}
2591+
self.in_closure = true;
2592+
self.closure_arg_span = fn_arg_span;
2593+
self.visit_expr(body);
2594+
self.in_closure = false;
25962595
}
2597-
if let hir::Expr { kind: hir::ExprKind::Path(path), .. } = e {
2598-
if let hir::QPath::Resolved(_, hir::Path { segments: [seg], .. }) = path
2599-
&& seg.ident.name == kw::SelfLower
2600-
&& self.in_closure
2601-
{
2602-
self.closure_change_spans.push(e.span);
2603-
}
2596+
if let hir::Expr { kind: hir::ExprKind::Path(path), .. } = e
2597+
&& let hir::QPath::Resolved(_, hir::Path { segments: [seg], .. }) = path
2598+
&& seg.ident.name == kw::SelfLower
2599+
&& self.in_closure
2600+
{
2601+
self.closure_change_spans.push(e.span);
26042602
}
26052603
hir::intravisit::walk_expr(self, e);
26062604
}
@@ -2609,20 +2607,19 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
26092607
if let hir::Pat { kind: hir::PatKind::Binding(_, hir_id, _ident, _), .. } =
26102608
local.pat
26112609
&& let Some(init) = local.init
2612-
{
2613-
if let hir::Expr {
2610+
&& let hir::Expr {
26142611
kind:
26152612
hir::ExprKind::Closure(&hir::Closure {
26162613
kind: hir::ClosureKind::Closure,
26172614
..
26182615
}),
26192616
..
26202617
} = init
2621-
&& init.span.contains(self.capture_span)
2622-
{
2623-
self.closure_local_id = Some(*hir_id);
2624-
}
2618+
&& init.span.contains(self.capture_span)
2619+
{
2620+
self.closure_local_id = Some(*hir_id);
26252621
}
2622+
26262623
hir::intravisit::walk_local(self, local);
26272624
}
26282625

compiler/rustc_borrowck/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2069,12 +2069,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
20692069
// no move out from an earlier location) then this is an attempt at initialization
20702070
// of the union - we should error in that case.
20712071
let tcx = this.infcx.tcx;
2072-
if base.ty(this.body(), tcx).ty.is_union() {
2073-
if this.move_data.path_map[mpi].iter().any(|moi| {
2072+
if base.ty(this.body(), tcx).ty.is_union()
2073+
&& this.move_data.path_map[mpi].iter().any(|moi| {
20742074
this.move_data.moves[*moi].source.is_predecessor_of(location, this.body)
2075-
}) {
2076-
return;
2077-
}
2075+
})
2076+
{
2077+
return;
20782078
}
20792079

20802080
this.report_use_of_moved_or_uninitialized(

compiler/rustc_borrowck/src/type_check/liveness/trace.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,11 @@ impl<'a, 'typeck, 'b, 'tcx> LivenessResults<'a, 'typeck, 'b, 'tcx> {
353353
let location = self.cx.elements.to_location(drop_point);
354354
debug_assert_eq!(self.cx.body.terminator_loc(location.block), location,);
355355

356-
if self.cx.initialized_at_terminator(location.block, mpi) {
357-
if self.drop_live_at.insert(drop_point) {
358-
self.drop_locations.push(location);
359-
self.stack.push(drop_point);
360-
}
356+
if self.cx.initialized_at_terminator(location.block, mpi)
357+
&& self.drop_live_at.insert(drop_point)
358+
{
359+
self.drop_locations.push(location);
360+
self.stack.push(drop_point);
361361
}
362362
}
363363

compiler/rustc_codegen_llvm/src/back/lto.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,9 @@ fn prepare_lto(
9292
dcx.emit_err(LtoDylib);
9393
return Err(FatalError);
9494
}
95-
} else if *crate_type == CrateType::ProcMacro {
96-
if !cgcx.opts.unstable_opts.dylib_lto {
97-
dcx.emit_err(LtoProcMacro);
98-
return Err(FatalError);
99-
}
95+
} else if *crate_type == CrateType::ProcMacro && !cgcx.opts.unstable_opts.dylib_lto {
96+
dcx.emit_err(LtoProcMacro);
97+
return Err(FatalError);
10098
}
10199
}
102100

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+19-22
Original file line numberDiff line numberDiff line change
@@ -617,32 +617,29 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
617617
// purpose functions as they wouldn't have the right target features
618618
// enabled. For that reason we also forbid #[inline(always)] as it can't be
619619
// respected.
620-
if !codegen_fn_attrs.target_features.is_empty() {
621-
if codegen_fn_attrs.inline == InlineAttr::Always {
622-
if let Some(span) = inline_span {
623-
tcx.dcx().span_err(
624-
span,
625-
"cannot use `#[inline(always)]` with \
620+
if !codegen_fn_attrs.target_features.is_empty() && codegen_fn_attrs.inline == InlineAttr::Always
621+
{
622+
if let Some(span) = inline_span {
623+
tcx.dcx().span_err(
624+
span,
625+
"cannot use `#[inline(always)]` with \
626626
`#[target_feature]`",
627-
);
628-
}
627+
);
629628
}
630629
}
631630

632-
if !codegen_fn_attrs.no_sanitize.is_empty() {
633-
if codegen_fn_attrs.inline == InlineAttr::Always {
634-
if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) {
635-
let hir_id = tcx.local_def_id_to_hir_id(did);
636-
tcx.node_span_lint(
637-
lint::builtin::INLINE_NO_SANITIZE,
638-
hir_id,
639-
no_sanitize_span,
640-
|lint| {
641-
lint.primary_message("`no_sanitize` will have no effect after inlining");
642-
lint.span_note(inline_span, "inlining requested here");
643-
},
644-
)
645-
}
631+
if !codegen_fn_attrs.no_sanitize.is_empty() && codegen_fn_attrs.inline == InlineAttr::Always {
632+
if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) {
633+
let hir_id = tcx.local_def_id_to_hir_id(did);
634+
tcx.node_span_lint(
635+
lint::builtin::INLINE_NO_SANITIZE,
636+
hir_id,
637+
no_sanitize_span,
638+
|lint| {
639+
lint.primary_message("`no_sanitize` will have no effect after inlining");
640+
lint.span_note(inline_span, "inlining requested here");
641+
},
642+
)
646643
}
647644
}
648645

compiler/rustc_const_eval/src/interpret/eval_context.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_span::Span;
1616
use rustc_target::abi::call::FnAbi;
1717
use rustc_target::abi::{Align, HasDataLayout, Size, TargetDataLayout};
1818
use rustc_trait_selection::traits::ObligationCtxt;
19-
use tracing::{debug, trace};
19+
use tracing::{debug, instrument, trace};
2020

2121
use super::{
2222
err_inval, throw_inval, throw_ub, throw_ub_custom, Frame, FrameInfo, GlobalId, InterpErrorInfo,
@@ -315,6 +315,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
315315

316316
/// Check if the two things are equal in the current param_env, using an infctx to get proper
317317
/// equality checks.
318+
#[instrument(level = "trace", skip(self), ret)]
318319
pub(super) fn eq_in_param_env<T>(&self, a: T, b: T) -> bool
319320
where
320321
T: PartialEq + TypeFoldable<TyCtxt<'tcx>> + ToTrace<'tcx>,
@@ -330,13 +331,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
330331
// equate the two trait refs after normalization
331332
let a = ocx.normalize(&cause, self.param_env, a);
332333
let b = ocx.normalize(&cause, self.param_env, b);
333-
if ocx.eq(&cause, self.param_env, a, b).is_ok() {
334-
if ocx.select_all_or_error().is_empty() {
335-
// All good.
336-
return true;
337-
}
334+
335+
if let Err(terr) = ocx.eq(&cause, self.param_env, a, b) {
336+
trace!(?terr);
337+
return false;
338+
}
339+
340+
let errors = ocx.select_all_or_error();
341+
if !errors.is_empty() {
342+
trace!(?errors);
343+
return false;
338344
}
339-
return false;
345+
346+
// All good.
347+
true
340348
}
341349

342350
/// Walks up the callstack from the intrinsic's callsite, searching for the first callsite in a

compiler/rustc_driver_impl/src/pretty.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,8 @@ impl<'tcx> PrintExtra<'tcx> {
222222
}
223223

224224
pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
225-
if ppm.needs_analysis() {
226-
if ex.tcx().analysis(()).is_err() {
227-
FatalError.raise();
228-
}
225+
if ppm.needs_analysis() && ex.tcx().analysis(()).is_err() {
226+
FatalError.raise();
229227
}
230228

231229
let (src, src_name) = get_source(sess);

compiler/rustc_hir_analysis/src/check/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,15 @@ fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId) {
186186

187187
if let Ok(alloc) = tcx.eval_static_initializer(id.to_def_id())
188188
&& alloc.inner().provenance().ptrs().len() != 0
189-
{
190-
if attrs
189+
&& attrs
191190
.link_section
192191
.map(|link_section| !link_section.as_str().starts_with(".init_array"))
193192
.unwrap()
194-
{
195-
let msg = "statics with a custom `#[link_section]` must be a \
193+
{
194+
let msg = "statics with a custom `#[link_section]` must be a \
196195
simple list of bytes on the wasm target with no \
197196
extra levels of indirection such as references";
198-
tcx.dcx().span_err(tcx.def_span(id), msg);
199-
}
197+
tcx.dcx().span_err(tcx.def_span(id), msg);
200198
}
201199
}
202200

compiler/rustc_hir_analysis/src/coherence/mod.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,15 @@ fn enforce_trait_manually_implementable(
5353
) -> Result<(), ErrorGuaranteed> {
5454
let impl_header_span = tcx.def_span(impl_def_id);
5555

56-
if tcx.is_lang_item(trait_def_id, LangItem::Freeze) {
57-
if !tcx.features().freeze_impls {
58-
feature_err(
59-
&tcx.sess,
60-
sym::freeze_impls,
61-
impl_header_span,
62-
"explicit impls for the `Freeze` trait are not permitted",
63-
)
64-
.with_span_label(impl_header_span, format!("impl of `Freeze` not allowed"))
65-
.emit();
66-
}
56+
if tcx.is_lang_item(trait_def_id, LangItem::Freeze) && !tcx.features().freeze_impls {
57+
feature_err(
58+
&tcx.sess,
59+
sym::freeze_impls,
60+
impl_header_span,
61+
"explicit impls for the `Freeze` trait are not permitted",
62+
)
63+
.with_span_label(impl_header_span, format!("impl of `Freeze` not allowed"))
64+
.emit();
6765
}
6866

6967
// Disallow *all* explicit impls of traits marked `#[rustc_deny_explicit_impl]`

compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -827,20 +827,18 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
827827

828828
if num_generic_args_supplied_to_trait + num_assoc_fn_excess_args
829829
== num_trait_generics_except_self
830+
&& let Some(span) = self.gen_args.span_ext()
831+
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
830832
{
831-
if let Some(span) = self.gen_args.span_ext()
832-
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
833-
{
834-
let sugg = vec![
835-
(
836-
self.path_segment.ident.span,
837-
format!("{}::{}", snippet, self.path_segment.ident),
838-
),
839-
(span.with_lo(self.path_segment.ident.span.hi()), "".to_owned()),
840-
];
833+
let sugg = vec![
834+
(
835+
self.path_segment.ident.span,
836+
format!("{}::{}", snippet, self.path_segment.ident),
837+
),
838+
(span.with_lo(self.path_segment.ident.span.hi()), "".to_owned()),
839+
];
841840

842-
err.multipart_suggestion(msg, sugg, Applicability::MaybeIncorrect);
843-
}
841+
err.multipart_suggestion(msg, sugg, Applicability::MaybeIncorrect);
844842
}
845843
}
846844
}

compiler/rustc_hir_typeck/src/cast.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -732,12 +732,11 @@ impl<'a, 'tcx> CastCheck<'tcx> {
732732
}
733733
_ => return Err(CastError::NonScalar),
734734
};
735-
if let ty::Adt(adt_def, _) = *self.expr_ty.kind() {
736-
if adt_def.did().krate != LOCAL_CRATE {
737-
if adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive) {
738-
return Err(CastError::ForeignNonExhaustiveAdt);
739-
}
740-
}
735+
if let ty::Adt(adt_def, _) = *self.expr_ty.kind()
736+
&& adt_def.did().krate != LOCAL_CRATE
737+
&& adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive)
738+
{
739+
return Err(CastError::ForeignNonExhaustiveAdt);
741740
}
742741
match (t_from, t_cast) {
743742
// These types have invariants! can't cast into them.

0 commit comments

Comments
 (0)