Skip to content

Commit 3ba1275

Browse files
authored
Rollup merge of rust-lang#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

+9-11
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

+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_lowering/src/item.rs

+4-6
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

+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/region_infer/values.rs

+4-8
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

+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_builtin_macros/src/asm.rs

+4-6
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

+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/back/link.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,10 @@ pub fn each_linked_rlib(
281281
let used_crate_source = &info.used_crate_source[&cnum];
282282
if let Some((path, _)) = &used_crate_source.rlib {
283283
f(cnum, path);
284+
} else if used_crate_source.rmeta.is_some() {
285+
return Err(errors::LinkRlibError::OnlyRmetaFound { crate_name });
284286
} else {
285-
if used_crate_source.rmeta.is_some() {
286-
return Err(errors::LinkRlibError::OnlyRmetaFound { crate_name });
287-
} else {
288-
return Err(errors::LinkRlibError::NotFound { crate_name });
289-
}
287+
return Err(errors::LinkRlibError::NotFound { crate_name });
290288
}
291289
}
292290
Ok(())
@@ -628,12 +626,10 @@ fn link_staticlib(
628626
let used_crate_source = &codegen_results.crate_info.used_crate_source[&cnum];
629627
if let Some((path, _)) = &used_crate_source.dylib {
630628
all_rust_dylibs.push(&**path);
629+
} else if used_crate_source.rmeta.is_some() {
630+
sess.dcx().emit_fatal(errors::LinkRlibError::OnlyRmetaFound { crate_name });
631631
} else {
632-
if used_crate_source.rmeta.is_some() {
633-
sess.dcx().emit_fatal(errors::LinkRlibError::OnlyRmetaFound { crate_name });
634-
} else {
635-
sess.dcx().emit_fatal(errors::LinkRlibError::NotFound { crate_name });
636-
}
632+
sess.dcx().emit_fatal(errors::LinkRlibError::NotFound { crate_name });
637633
}
638634
}
639635

@@ -1972,10 +1968,8 @@ fn add_late_link_args(
19721968
if let Some(args) = sess.target.late_link_args_dynamic.get(&flavor) {
19731969
cmd.verbatim_args(args.iter().map(Deref::deref));
19741970
}
1975-
} else {
1976-
if let Some(args) = sess.target.late_link_args_static.get(&flavor) {
1977-
cmd.verbatim_args(args.iter().map(Deref::deref));
1978-
}
1971+
} else if let Some(args) = sess.target.late_link_args_static.get(&flavor) {
1972+
cmd.verbatim_args(args.iter().map(Deref::deref));
19791973
}
19801974
if let Some(args) = sess.target.late_link_args.get(&flavor) {
19811975
cmd.verbatim_args(args.iter().map(Deref::deref));
@@ -2635,10 +2629,8 @@ fn add_native_libs_from_crate(
26352629
if link_static {
26362630
cmd.link_staticlib_by_name(name, verbatim, false);
26372631
}
2638-
} else {
2639-
if link_dynamic {
2640-
cmd.link_dylib_by_name(name, verbatim, true);
2641-
}
2632+
} else if link_dynamic {
2633+
cmd.link_dylib_by_name(name, verbatim, true);
26422634
}
26432635
}
26442636
NativeLibKind::Framework { as_needed } => {

compiler/rustc_codegen_ssa/src/back/linker.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -791,14 +791,12 @@ impl<'a> Linker for GccLinker<'a> {
791791
self.link_arg("-exported_symbols_list").link_arg(path);
792792
} else if self.sess.target.is_like_solaris {
793793
self.link_arg("-M").link_arg(path);
794+
} else if is_windows {
795+
self.link_arg(path);
794796
} else {
795-
if is_windows {
796-
self.link_arg(path);
797-
} else {
798-
let mut arg = OsString::from("--version-script=");
799-
arg.push(path);
800-
self.link_arg(arg).link_arg("--no-undefined-version");
801-
}
797+
let mut arg = OsString::from("--version-script=");
798+
arg.push(path);
799+
self.link_arg(arg).link_arg("--no-undefined-version");
802800
}
803801
}
804802

0 commit comments

Comments
 (0)