Skip to content

Commit bfce653

Browse files
committed
Fix new clippy::collapsible_if warnings.
1 parent 708ef68 commit bfce653

File tree

25 files changed

+528
-567
lines changed

25 files changed

+528
-567
lines changed

crates/rustc_codegen_spirv/build.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,17 @@ pub(super) fn elf_e_flags(architecture: Architecture, sess: &Session) -> u32 {",
262262
}
263263
if line.starts_with('[') {
264264
toml_directive = Some(line);
265-
} else if toml_directive == Some("[dependencies]") {
266-
if let Some((name, _)) = line.split_once(" = ") {
267-
// HACK(eddyb) ignore a weird edge case.
268-
if name == "thorin-dwp" {
269-
continue;
270-
}
271-
let extern_crate = format!("extern crate {};", name.replace('-', "_"));
272-
if !all_extern_crates.contains(&extern_crate) {
273-
writeln(&mut all_extern_crates, "#[allow(unused_extern_crates)]");
274-
writeln(&mut all_extern_crates, &extern_crate);
275-
}
265+
} else if toml_directive == Some("[dependencies]")
266+
&& let Some((name, _)) = line.split_once(" = ")
267+
{
268+
// HACK(eddyb) ignore a weird edge case.
269+
if name == "thorin-dwp" {
270+
continue;
271+
}
272+
let extern_crate = format!("extern crate {};", name.replace('-', "_"));
273+
if !all_extern_crates.contains(&extern_crate) {
274+
writeln(&mut all_extern_crates, "#[allow(unused_extern_crates)]");
275+
writeln(&mut all_extern_crates, &extern_crate);
276276
}
277277
}
278278
}

crates/rustc_codegen_spirv/src/abi.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,11 @@ impl<'tcx> ConvSpirvType<'tcx> for TyAndLayout<'tcx> {
542542

543543
let attrs = AggregatedSpirvAttributes::parse(cx, cx.tcx.get_attrs_unchecked(adt.did()));
544544

545-
if let Some(intrinsic_type_attr) = attrs.intrinsic_type.map(|attr| attr.value) {
546-
if let Ok(spirv_type) =
545+
if let Some(intrinsic_type_attr) = attrs.intrinsic_type.map(|attr| attr.value)
546+
&& let Ok(spirv_type) =
547547
trans_intrinsic_type(cx, span, *self, args, intrinsic_type_attr)
548-
{
549-
return spirv_type;
550-
}
548+
{
549+
return spirv_type;
551550
}
552551
}
553552

@@ -613,12 +612,12 @@ impl<'tcx> ConvSpirvType<'tcx> for TyAndLayout<'tcx> {
613612
};
614613
// FIXME(eddyb) use `ArrayVec` here.
615614
let mut field_names = Vec::new();
616-
if let TyKind::Adt(adt, _) = self.ty.kind() {
617-
if let Variants::Single { index } = self.variants {
618-
for i in self.fields.index_by_increasing_offset() {
619-
let field = &adt.variants()[index].fields[FieldIdx::new(i)];
620-
field_names.push(field.name);
621-
}
615+
if let TyKind::Adt(adt, _) = self.ty.kind()
616+
&& let Variants::Single { index } = self.variants
617+
{
618+
for i in self.fields.index_by_increasing_offset() {
619+
let field = &adt.variants()[index].fields[FieldIdx::new(i)];
620+
field_names.push(field.name);
622621
}
623622
}
624623
SpirvType::Adt {
@@ -918,10 +917,10 @@ fn trans_struct_or_union<'tcx>(
918917
let mut field_offsets = Vec::new();
919918
let mut field_names = Vec::new();
920919
for i in ty.fields.index_by_increasing_offset() {
921-
if let Some(expected_field_idx) = union_case {
922-
if i != expected_field_idx.as_usize() {
923-
continue;
924-
}
920+
if let Some(expected_field_idx) = union_case
921+
&& i != expected_field_idx.as_usize()
922+
{
923+
continue;
925924
}
926925

927926
let field_ty = ty.field(cx, i);
@@ -995,10 +994,11 @@ impl<'tcx> From<TyAndLayout<'tcx>> for TyLayoutNameKey<'tcx> {
995994
impl fmt::Display for TyLayoutNameKey<'_> {
996995
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
997996
write!(f, "{}", self.ty)?;
998-
if let (TyKind::Adt(def, _), Some(index)) = (self.ty.kind(), self.variant) {
999-
if def.is_enum() && !def.variants().is_empty() {
1000-
write!(f, "::{}", def.variants()[index].name)?;
1001-
}
997+
if let (TyKind::Adt(def, _), Some(index)) = (self.ty.kind(), self.variant)
998+
&& def.is_enum()
999+
&& !def.variants().is_empty()
1000+
{
1001+
write!(f, "::{}", def.variants()[index].name)?;
10021002
}
10031003
if let (TyKind::Coroutine(_, _), Some(index)) = (self.ty.kind(), self.variant) {
10041004
write!(f, "::{}", CoroutineArgs::variant_name(index))?;

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 52 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,14 +1791,12 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
17911791
// nightlies - and that PR happens to remove the last GEP that can be
17921792
// emitted with any "structured" (struct/array) indices, beyond the
17931793
// "first index" (which acts as `<*T>::offset` aka "pointer arithmetic").
1794-
if let &[ptr_base_index, structured_index] = indices {
1795-
if self.builder.lookup_const_scalar(ptr_base_index) == Some(0) {
1796-
if let SpirvType::Array { element, .. } | SpirvType::RuntimeArray { element, .. } =
1797-
self.lookup_type(ty)
1798-
{
1799-
return self.maybe_inbounds_gep(element, ptr, &[structured_index], true);
1800-
}
1801-
}
1794+
if let &[ptr_base_index, structured_index] = indices
1795+
&& self.builder.lookup_const_scalar(ptr_base_index) == Some(0)
1796+
&& let SpirvType::Array { element, .. } | SpirvType::RuntimeArray { element, .. } =
1797+
self.lookup_type(ty)
1798+
{
1799+
return self.maybe_inbounds_gep(element, ptr, &[structured_index], true);
18021800
}
18031801

18041802
self.maybe_inbounds_gep(ty, ptr, indices, true)
@@ -1883,22 +1881,20 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
18831881
// capabilities. For example, casting a f16 constant to f32 will directly
18841882
// create a f32 constant, avoiding the need for Float16 capability if it is
18851883
// not used elsewhere.
1886-
if let Some(const_val) = self.builder.lookup_const_scalar(val) {
1887-
if let (SpirvType::Float(src_width), SpirvType::Float(dst_width)) =
1884+
if let Some(const_val) = self.builder.lookup_const_scalar(val)
1885+
&& let (SpirvType::Float(src_width), SpirvType::Float(dst_width)) =
18881886
(self.lookup_type(val.ty), self.lookup_type(dest_ty))
1889-
{
1890-
if src_width < dst_width {
1891-
// Convert the bit representation to the actual float value
1892-
let float_val = match src_width {
1893-
32 => Some(f32::from_bits(const_val as u32) as f64),
1894-
64 => Some(f64::from_bits(const_val as u64)),
1895-
_ => None,
1896-
};
1887+
&& src_width < dst_width
1888+
{
1889+
// Convert the bit representation to the actual float value
1890+
let float_val = match src_width {
1891+
32 => Some(f32::from_bits(const_val as u32) as f64),
1892+
64 => Some(f64::from_bits(const_val as u64)),
1893+
_ => None,
1894+
};
18971895

1898-
if let Some(val) = float_val {
1899-
return self.constant_float(dest_ty, val);
1900-
}
1901-
}
1896+
if let Some(val) = float_val {
1897+
return self.constant_float(dest_ty, val);
19021898
}
19031899
}
19041900

@@ -3105,14 +3101,11 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
31053101
let const_slice_as_elem_ids = |ptr_id: Word, len: usize| {
31063102
if let SpirvConst::PtrTo { pointee } =
31073103
self.builder.lookup_const_by_id(ptr_id)?
3108-
{
3109-
if let SpirvConst::Composite(elems) =
3104+
&& let SpirvConst::Composite(elems) =
31103105
self.builder.lookup_const_by_id(pointee)?
3111-
{
3112-
if elems.len() == len {
3113-
return Some(elems);
3114-
}
3115-
}
3106+
&& elems.len() == len
3107+
{
3108+
return Some(elems);
31163109
}
31173110
None
31183111
};
@@ -3147,12 +3140,11 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
31473140
] = args[..]
31483141
{
31493142
// Optional `&'static panic::Location<'static>`.
3150-
if other_args.len() <= 1 {
3151-
if let Some(const_msg) = const_str_as_utf8(&[a_id, b_id]) {
3152-
decoded_format_args.const_pieces =
3153-
Some([const_msg].into_iter().collect());
3154-
return Ok(decoded_format_args);
3155-
}
3143+
if other_args.len() <= 1
3144+
&& let Some(const_msg) = const_str_as_utf8(&[a_id, b_id])
3145+
{
3146+
decoded_format_args.const_pieces = Some([const_msg].into_iter().collect());
3147+
return Ok(decoded_format_args);
31563148
}
31573149
}
31583150

@@ -3267,12 +3259,11 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
32673259
}
32683260

32693261
// HACK(eddyb) avoid the logic below that assumes only ID operands
3270-
if inst.class.opcode == Op::CompositeExtract {
3271-
if let (Some(r), &[Operand::IdRef(x), Operand::LiteralBit32(i)]) =
3262+
if inst.class.opcode == Op::CompositeExtract
3263+
&& let (Some(r), &[Operand::IdRef(x), Operand::LiteralBit32(i)]) =
32723264
(inst.result_id, &inst.operands[..])
3273-
{
3274-
return Some(Inst::CompositeExtract(r, x, i));
3275-
}
3265+
{
3266+
return Some(Inst::CompositeExtract(r, x, i));
32763267
}
32773268

32783269
// HACK(eddyb) all instructions accepted below
@@ -3483,8 +3474,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
34833474
// access chain cases that `inbounds_gep` can now cause.
34843475
if let Inst::InBoundsAccessChain(dst_field_ptr, dst_base_ptr, 0) =
34853476
copy_to_rt_args_array_insts[0]
3486-
{
3487-
if let Some(mut prev_insts) = try_rev_take(1) {
3477+
&& let Some(mut prev_insts) = try_rev_take(1) {
34883478
assert_eq!(prev_insts.len(), 1);
34893479
let prev_inst = prev_insts.pop().unwrap();
34903480

@@ -3503,7 +3493,6 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
35033493
}
35043494
}
35053495
}
3506-
}
35073496

35083497
match copy_to_rt_args_array_insts[..] {
35093498
[
@@ -3855,27 +3844,27 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
38553844
// Since From is only implemented for safe conversions (widening conversions that preserve
38563845
// the numeric value), we can directly create a constant of the target type for primitive
38573846
// numeric types.
3858-
if let [arg] = args {
3859-
if let Some(const_val) = self.builder.lookup_const_scalar(*arg) {
3860-
use rustc_middle::ty::FloatTy;
3861-
let optimized_result = match (source_ty.kind(), target_ty.kind()) {
3862-
// Integer widening conversions
3863-
(ty::Uint(_), ty::Uint(_)) | (ty::Int(_), ty::Int(_)) => {
3864-
Some(self.constant_int(result_type, const_val))
3865-
}
3866-
// Float widening conversions
3867-
// TODO(@LegNeato): Handle more float types
3868-
(ty::Float(FloatTy::F32), ty::Float(FloatTy::F64)) => {
3869-
let float_val = f32::from_bits(const_val as u32) as f64;
3870-
Some(self.constant_float(result_type, float_val))
3871-
}
3872-
// No optimization for narrowing conversions or unsupported types
3873-
_ => None,
3874-
};
3875-
3876-
if let Some(result) = optimized_result {
3877-
return result;
3847+
if let [arg] = args
3848+
&& let Some(const_val) = self.builder.lookup_const_scalar(*arg)
3849+
{
3850+
use rustc_middle::ty::FloatTy;
3851+
let optimized_result = match (source_ty.kind(), target_ty.kind()) {
3852+
// Integer widening conversions
3853+
(ty::Uint(_), ty::Uint(_)) | (ty::Int(_), ty::Int(_)) => {
3854+
Some(self.constant_int(result_type, const_val))
38783855
}
3856+
// Float widening conversions
3857+
// TODO(@LegNeato): Handle more float types
3858+
(ty::Float(FloatTy::F32), ty::Float(FloatTy::F64)) => {
3859+
let float_val = f32::from_bits(const_val as u32) as f64;
3860+
Some(self.constant_float(result_type, float_val))
3861+
}
3862+
// No optimization for narrowing conversions or unsupported types
3863+
_ => None,
3864+
};
3865+
3866+
if let Some(result) = optimized_result {
3867+
return result;
38793868
}
38803869
}
38813870
}

crates/rustc_codegen_spirv/src/builder/spirv_asm.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,13 @@ impl<'a, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'tcx> {
126126
| InlineAsmOperandRef::Label { .. } => (None, None),
127127
};
128128

129-
if let Some(in_value) = in_value {
130-
if let (BackendRepr::Scalar(scalar), OperandValue::Immediate(in_value_spv)) =
129+
if let Some(in_value) = in_value
130+
&& let (BackendRepr::Scalar(scalar), OperandValue::Immediate(in_value_spv)) =
131131
(in_value.layout.backend_repr, &mut in_value.val)
132-
{
133-
if let Primitive::Pointer(_) = scalar.primitive() {
134-
let in_value_precise_type = in_value.layout.spirv_type(self.span(), self);
135-
*in_value_spv = self.pointercast(*in_value_spv, in_value_precise_type);
136-
}
137-
}
132+
&& let Primitive::Pointer(_) = scalar.primitive()
133+
{
134+
let in_value_precise_type = in_value.layout.spirv_type(self.span(), self);
135+
*in_value_spv = self.pointercast(*in_value_spv, in_value_precise_type);
138136
}
139137
if let Some(out_place) = out_place {
140138
let out_place_precise_type = out_place.layout.spirv_type(self.span(), self);

crates/rustc_codegen_spirv/src/codegen_cx/constant.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -343,18 +343,15 @@ impl<'tcx> CodegenCx<'tcx> {
343343
pub fn const_bitcast(&self, val: SpirvValue, ty: Word) -> SpirvValue {
344344
// HACK(eddyb) special-case `const_data_from_alloc` + `static_addr_of`
345345
// as the old `from_const_alloc` (now `OperandRef::from_const_alloc`).
346-
if let SpirvValueKind::IllegalConst(_) = val.kind {
347-
if let Some(SpirvConst::PtrTo { pointee }) = self.builder.lookup_const(val) {
348-
if let Some(SpirvConst::ConstDataFromAlloc(alloc)) =
349-
self.builder.lookup_const_by_id(pointee)
350-
{
351-
if let SpirvType::Pointer { pointee } = self.lookup_type(ty) {
352-
let mut offset = Size::ZERO;
353-
let init = self.read_from_const_alloc(alloc, &mut offset, pointee);
354-
return self.static_addr_of(init, alloc.inner().align, None);
355-
}
356-
}
357-
}
346+
if let SpirvValueKind::IllegalConst(_) = val.kind
347+
&& let Some(SpirvConst::PtrTo { pointee }) = self.builder.lookup_const(val)
348+
&& let Some(SpirvConst::ConstDataFromAlloc(alloc)) =
349+
self.builder.lookup_const_by_id(pointee)
350+
&& let SpirvType::Pointer { pointee } = self.lookup_type(ty)
351+
{
352+
let mut offset = Size::ZERO;
353+
let init = self.read_from_const_alloc(alloc, &mut offset, pointee);
354+
return self.static_addr_of(init, alloc.inner().align, None);
358355
}
359356

360357
if val.ty == ty {

crates/rustc_codegen_spirv/src/codegen_cx/declare.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,25 @@ impl<'tcx> CodegenCx<'tcx> {
168168
}
169169

170170
// Check if this is a From trait implementation
171-
if let Some(impl_def_id) = self.tcx.impl_of_method(def_id) {
172-
if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_def_id) {
173-
let trait_def_id = trait_ref.skip_binder().def_id;
174-
175-
// Check if this is the From trait.
176-
let trait_path = self.tcx.def_path_str(trait_def_id);
177-
if matches!(
178-
trait_path.as_str(),
179-
"core::convert::From" | "std::convert::From"
180-
) {
181-
// Extract the source and target types from the trait substitutions
182-
let trait_args = trait_ref.skip_binder().args;
183-
if let (Some(target_ty), Some(source_ty)) =
184-
(trait_args.types().nth(0), trait_args.types().nth(1))
185-
{
186-
self.from_trait_impls
187-
.borrow_mut()
188-
.insert(def_id, (source_ty, target_ty));
189-
}
171+
if let Some(impl_def_id) = self.tcx.impl_of_method(def_id)
172+
&& let Some(trait_ref) = self.tcx.impl_trait_ref(impl_def_id)
173+
{
174+
let trait_def_id = trait_ref.skip_binder().def_id;
175+
176+
// Check if this is the From trait.
177+
let trait_path = self.tcx.def_path_str(trait_def_id);
178+
if matches!(
179+
trait_path.as_str(),
180+
"core::convert::From" | "std::convert::From"
181+
) {
182+
// Extract the source and target types from the trait substitutions
183+
let trait_args = trait_ref.skip_binder().args;
184+
if let (Some(target_ty), Some(source_ty)) =
185+
(trait_args.types().nth(0), trait_args.types().nth(1))
186+
{
187+
self.from_trait_impls
188+
.borrow_mut()
189+
.insert(def_id, (source_ty, target_ty));
190190
}
191191
}
192192
}
@@ -251,12 +251,12 @@ impl<'tcx> CodegenCx<'tcx> {
251251
_ => return None,
252252
})
253253
});
254-
if let Some(spec) = spec {
255-
if let Some((ty,)) = instance.args.types().collect_tuple() {
256-
self.fmt_rt_arg_new_fn_ids_to_ty_and_spec
257-
.borrow_mut()
258-
.insert(fn_id, (ty, spec));
259-
}
254+
if let Some(spec) = spec
255+
&& let Some((ty,)) = instance.args.types().collect_tuple()
256+
{
257+
self.fmt_rt_arg_new_fn_ids_to_ty_and_spec
258+
.borrow_mut()
259+
.insert(fn_id, (ty, spec));
260260
}
261261
}
262262

0 commit comments

Comments
 (0)