Skip to content

Commit 3ba1275

Browse files
authored
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.
2 parents c3d1be7 + af8d911 commit 3ba1275

File tree

61 files changed

+561
-669
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+561
-669
lines changed

compiler/rustc_ast/src/entry.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,16 @@ pub fn entry_point_type(
4545
EntryPointType::Start
4646
} else if attr::contains_name(attrs, sym::rustc_main) {
4747
EntryPointType::RustcMainAttr
48-
} else {
49-
if let Some(name) = name
50-
&& name == sym::main
51-
{
52-
if at_root {
53-
// This is a top-level function so it can be `main`.
54-
EntryPointType::MainNamed
55-
} else {
56-
EntryPointType::OtherMain
57-
}
48+
} else if let Some(name) = name
49+
&& name == sym::main
50+
{
51+
if at_root {
52+
// This is a top-level function so it can be `main`.
53+
EntryPointType::MainNamed
5854
} else {
59-
EntryPointType::None
55+
EntryPointType::OtherMain
6056
}
57+
} else {
58+
EntryPointType::None
6159
}
6260
}

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 17 additions & 19 deletions
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_lowering/src/item.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -628,13 +628,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
628628
.map_or(Const::No, |attr| Const::Yes(attr.span)),
629629
_ => Const::No,
630630
}
631+
} else if self.tcx.is_const_trait(def_id) {
632+
// FIXME(effects) span
633+
Const::Yes(self.tcx.def_ident_span(def_id).unwrap())
631634
} else {
632-
if self.tcx.is_const_trait(def_id) {
633-
// FIXME(effects) span
634-
Const::Yes(self.tcx.def_ident_span(def_id).unwrap())
635-
} else {
636-
Const::No
637-
}
635+
Const::No
638636
}
639637
} else {
640638
Const::No

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 7 additions & 7 deletions
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

Lines changed: 22 additions & 25 deletions
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

Lines changed: 5 additions & 5 deletions
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/region_infer/values.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@ impl LivenessValues {
118118
debug!("LivenessValues::add_location(region={:?}, location={:?})", region, location);
119119
if let Some(points) = &mut self.points {
120120
points.insert(region, point);
121-
} else {
122-
if self.elements.point_in_range(point) {
123-
self.live_regions.as_mut().unwrap().insert(region);
124-
}
121+
} else if self.elements.point_in_range(point) {
122+
self.live_regions.as_mut().unwrap().insert(region);
125123
}
126124

127125
// When available, record the loans flowing into this region as live at the given point.
@@ -137,10 +135,8 @@ impl LivenessValues {
137135
debug!("LivenessValues::add_points(region={:?}, points={:?})", region, points);
138136
if let Some(this) = &mut self.points {
139137
this.union_row(region, points);
140-
} else {
141-
if points.iter().any(|point| self.elements.point_in_range(point)) {
142-
self.live_regions.as_mut().unwrap().insert(region);
143-
}
138+
} else if points.iter().any(|point| self.elements.point_in_range(point)) {
139+
self.live_regions.as_mut().unwrap().insert(region);
144140
}
145141

146142
// When available, record the loans flowing into this region as live at the given points.

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

Lines changed: 5 additions & 5 deletions
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_builtin_macros/src/asm.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,11 @@ pub fn parse_asm_args<'a>(
235235
continue;
236236
}
237237
args.named_args.insert(name, slot);
238-
} else {
239-
if !args.named_args.is_empty() || !args.reg_args.is_empty() {
240-
let named = args.named_args.values().map(|p| args.operands[*p].1).collect();
241-
let explicit = args.reg_args.iter().map(|p| args.operands[p].1).collect();
238+
} else if !args.named_args.is_empty() || !args.reg_args.is_empty() {
239+
let named = args.named_args.values().map(|p| args.operands[*p].1).collect();
240+
let explicit = args.reg_args.iter().map(|p| args.operands[p].1).collect();
242241

243-
dcx.emit_err(errors::AsmPositionalAfter { span, named, explicit });
244-
}
242+
dcx.emit_err(errors::AsmPositionalAfter { span, named, explicit });
245243
}
246244
}
247245

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 3 additions & 5 deletions
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

0 commit comments

Comments
 (0)