build: treewide: clippy: collapse nested ifs, use let chains

This bumps the MSRV to 1.88 (also, Rust edition 2024 is mandatory).

Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
On-behalf-of: SAP philipp.schuster@sap.com
This commit is contained in:
Philipp Schuster
2025-08-15 09:00:01 +02:00
committed by Bo Chen
parent f73a6c8d8e
commit c995b72384
40 changed files with 574 additions and 608 deletions

View File

@@ -1248,10 +1248,11 @@ impl DiskConfig {
return Err(ValidationError::InvalidPciSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() {
if iommu_segments.contains(&self.pci_segment) && !self.iommu {
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref()
&& iommu_segments.contains(&self.pci_segment)
&& !self.iommu
{
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
}
@@ -1260,13 +1261,13 @@ impl DiskConfig {
}
// Check Block device serial length
if let Some(ref serial) = self.serial {
if serial.len() > VIRTIO_BLK_ID_BYTES as usize {
return Err(ValidationError::InvalidSerialLength(
serial.len(),
VIRTIO_BLK_ID_BYTES as usize,
));
}
if let Some(ref serial) = self.serial
&& serial.len() > VIRTIO_BLK_ID_BYTES as usize
{
return Err(ValidationError::InvalidSerialLength(
serial.len(),
VIRTIO_BLK_ID_BYTES as usize,
));
}
Ok(())
@@ -1496,17 +1497,18 @@ impl NetConfig {
return Err(ValidationError::InvalidPciSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() {
if iommu_segments.contains(&self.pci_segment) && !self.iommu {
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref()
&& iommu_segments.contains(&self.pci_segment)
&& !self.iommu
{
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
}
if let Some(mtu) = self.mtu {
if mtu < virtio_devices::net::MIN_MTU {
return Err(ValidationError::InvalidMtu(mtu));
}
if let Some(mtu) = self.mtu
&& mtu < virtio_devices::net::MIN_MTU
{
return Err(ValidationError::InvalidMtu(mtu));
}
if !self.offload_csum && (self.offload_tso || self.offload_ufo) {
@@ -1633,12 +1635,12 @@ impl FsConfig {
return Err(ValidationError::InvalidPciSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() {
if iommu_segments.contains(&self.pci_segment) {
return Err(ValidationError::IommuNotSupportedOnSegment(
self.pci_segment,
));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref()
&& iommu_segments.contains(&self.pci_segment)
{
return Err(ValidationError::IommuNotSupportedOnSegment(
self.pci_segment,
));
}
}
@@ -1795,10 +1797,11 @@ impl PmemConfig {
return Err(ValidationError::InvalidPciSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() {
if iommu_segments.contains(&self.pci_segment) && !self.iommu {
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref()
&& iommu_segments.contains(&self.pci_segment)
&& !self.iommu
{
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
}
@@ -1895,17 +1898,18 @@ impl DebugConsoleConfig {
return Err(Error::ParseConsoleInvalidModeGiven);
}
if parser.is_set("iobase") {
if let Some(iobase_opt) = parser.get("iobase") {
if !iobase_opt.starts_with("0x") {
return Err(Error::Validation(ValidationError::InvalidIoPortHex(
iobase_opt,
)));
}
iobase = Some(u16::from_str_radix(&iobase_opt[2..], 16).map_err(|_| {
if parser.is_set("iobase")
&& let Some(iobase_opt) = parser.get("iobase")
{
if !iobase_opt.starts_with("0x") {
return Err(Error::Validation(ValidationError::InvalidIoPortHex(
iobase_opt,
)));
}
iobase =
Some(u16::from_str_radix(&iobase_opt[2..], 16).map_err(|_| {
Error::Validation(ValidationError::InvalidIoPortHex(iobase_opt))
})?);
}
}
Ok(Self { file, mode, iobase })
@@ -1957,10 +1961,11 @@ impl DeviceConfig {
return Err(ValidationError::InvalidPciSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() {
if iommu_segments.contains(&self.pci_segment) && !self.iommu {
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref()
&& iommu_segments.contains(&self.pci_segment)
&& !self.iommu
{
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
}
@@ -2000,12 +2005,12 @@ impl UserDeviceConfig {
return Err(ValidationError::InvalidPciSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() {
if iommu_segments.contains(&self.pci_segment) {
return Err(ValidationError::IommuNotSupportedOnSegment(
self.pci_segment,
));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref()
&& iommu_segments.contains(&self.pci_segment)
{
return Err(ValidationError::IommuNotSupportedOnSegment(
self.pci_segment,
));
}
}
@@ -2062,10 +2067,11 @@ impl VdpaConfig {
return Err(ValidationError::InvalidPciSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() {
if iommu_segments.contains(&self.pci_segment) && !self.iommu {
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref()
&& iommu_segments.contains(&self.pci_segment)
&& !self.iommu
{
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
}
@@ -2121,10 +2127,11 @@ impl VsockConfig {
return Err(ValidationError::InvalidPciSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() {
if iommu_segments.contains(&self.pci_segment) && !self.iommu {
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref()
&& iommu_segments.contains(&self.pci_segment)
&& !self.iommu
{
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
}
@@ -2493,10 +2500,10 @@ impl VmConfig {
{
let host_data_opt = &self.payload.as_ref().unwrap().host_data;
if let Some(host_data) = host_data_opt {
if host_data.len() != 64 {
return Err(ValidationError::InvalidHostData);
}
if let Some(host_data) = host_data_opt
&& host_data.len() != 64
{
return Err(ValidationError::InvalidHostData);
}
}
// The 'conflict' check is introduced in commit 24438e0390d3
@@ -2675,10 +2682,10 @@ impl VmConfig {
}
}
if let Some(vsock) = &self.vsock {
if [!0, 0, 1, 2].contains(&vsock.cid) {
return Err(ValidationError::VsockSpecialCid(vsock.cid));
}
if let Some(vsock) = &self.vsock
&& [!0, 0, 1, 2].contains(&vsock.cid)
{
return Err(ValidationError::VsockSpecialCid(vsock.cid));
}
if let Some(balloon) = &self.balloon {
@@ -3080,11 +3087,11 @@ impl VmConfig {
}
// Remove if vsock device
if let Some(vsock) = self.vsock.as_ref() {
if vsock.id.as_ref().map(|id| id.as_ref()) == Some(id) {
self.vsock = None;
removed = true;
}
if let Some(vsock) = self.vsock.as_ref()
&& vsock.id.as_ref().map(|id| id.as_ref()) == Some(id)
{
self.vsock = None;
removed = true;
}
removed

View File

@@ -601,10 +601,10 @@ impl BusDevice for CpuManager {
state.removing = false;
}
// Trigger removal of vCPU
if data[0] & (1 << CPU_EJECT_FLAG) == 1 << CPU_EJECT_FLAG {
if let Err(e) = self.remove_vcpu(self.selected_cpu as u32) {
error!("Error removing vCPU: {:?}", e);
}
if data[0] & (1 << CPU_EJECT_FLAG) == 1 << CPU_EJECT_FLAG
&& let Err(e) = self.remove_vcpu(self.selected_cpu as u32)
{
error!("Error removing vCPU: {:?}", e);
}
} else {
warn!("Out of range vCPU id: {}", self.selected_cpu);
@@ -1059,14 +1059,13 @@ impl CpuManager {
}
// Apply seccomp filter for vcpu thread.
if !vcpu_seccomp_filter.is_empty() {
if let Err(e) =
if !vcpu_seccomp_filter.is_empty() && let Err(e) =
apply_filter(&vcpu_seccomp_filter).map_err(Error::ApplySeccompFilter)
{
error!("Error applying seccomp filter: {:?}", e);
return;
}
}
extern "C" fn handle_signal(_: i32, _: *mut siginfo_t, _: *mut c_void) {}
// This uses an async signal safe handler to kill the vcpu handles.
register_signal_handler(SIGRTMIN(), handle_signal)

View File

@@ -772,12 +772,13 @@ impl DeviceRelocation for AddressManager {
if let Some(node) = self.device_tree.lock().unwrap().get_mut(&id) {
let mut resource_updated = false;
for resource in node.resources.iter_mut() {
if let Resource::PciBar { base, type_, .. } = resource {
if PciBarRegionType::from(*type_) == region_type && *base == old_base {
*base = new_base;
resource_updated = true;
break;
}
if let Resource::PciBar { base, type_, .. } = resource
&& PciBarRegionType::from(*type_) == region_type
&& *base == old_base
{
*base = new_base;
resource_updated = true;
break;
}
}
@@ -814,43 +815,41 @@ impl DeviceRelocation for AddressManager {
} else {
let virtio_dev = virtio_pci_dev.virtio_device();
let mut virtio_dev = virtio_dev.lock().unwrap();
if let Some(mut shm_regions) = virtio_dev.get_shm_regions() {
if shm_regions.addr.raw_value() == old_base {
let mem_region = self.vm.make_user_memory_region(
shm_regions.mem_slot,
old_base,
shm_regions.len,
shm_regions.host_addr,
false,
false,
);
if let Some(mut shm_regions) = virtio_dev.get_shm_regions()
&& shm_regions.addr.raw_value() == old_base
{
let mem_region = self.vm.make_user_memory_region(
shm_regions.mem_slot,
old_base,
shm_regions.len,
shm_regions.host_addr,
false,
false,
);
self.vm.remove_user_memory_region(mem_region).map_err(|e| {
io::Error::other(format!("failed to remove user memory region: {e:?}"))
})?;
self.vm.remove_user_memory_region(mem_region).map_err(|e| {
io::Error::other(format!("failed to remove user memory region: {e:?}"))
})?;
// Create new mapping by inserting new region to KVM.
let mem_region = self.vm.make_user_memory_region(
shm_regions.mem_slot,
new_base,
shm_regions.len,
shm_regions.host_addr,
false,
false,
);
// Create new mapping by inserting new region to KVM.
let mem_region = self.vm.make_user_memory_region(
shm_regions.mem_slot,
new_base,
shm_regions.len,
shm_regions.host_addr,
false,
false,
);
self.vm.create_user_memory_region(mem_region).map_err(|e| {
io::Error::other(format!("failed to create user memory regions: {e:?}"))
})?;
self.vm.create_user_memory_region(mem_region).map_err(|e| {
io::Error::other(format!("failed to create user memory regions: {e:?}"))
})?;
// Update shared memory regions to reflect the new mapping.
shm_regions.addr = GuestAddress(new_base);
virtio_dev.set_shm_regions(shm_regions).map_err(|e| {
io::Error::other(format!(
"failed to update shared memory regions: {e:?}"
))
})?;
}
// Update shared memory regions to reflect the new mapping.
shm_regions.addr = GuestAddress(new_base);
virtio_dev.set_shm_regions(shm_regions).map_err(|e| {
io::Error::other(format!("failed to update shared memory regions: {e:?}"))
})?;
}
}
}
@@ -1655,14 +1654,14 @@ impl DeviceManager {
iommu_attached_devices.append(&mut vfio_user_iommu_device_ids);
// Add all devices from forced iommu segments
if let Some(platform_config) = self.config.lock().unwrap().platform.as_ref() {
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref() {
for segment in iommu_segments {
for device in 0..32 {
let bdf = PciBdf::new(*segment, 0, device, 0);
if !iommu_attached_devices.contains(&bdf) {
iommu_attached_devices.push(bdf);
}
if let Some(platform_config) = self.config.lock().unwrap().platform.as_ref()
&& let Some(iommu_segments) = platform_config.iommu_segments.as_ref()
{
for segment in iommu_segments {
for device in 0..32 {
let bdf = PciBdf::new(*segment, 0, device, 0);
if !iommu_attached_devices.contains(&bdf) {
iommu_attached_devices.push(bdf);
}
}
}
@@ -4350,14 +4349,14 @@ impl DeviceManager {
.add_memory_region(new_region)
.map_err(DeviceManagerError::UpdateMemoryForVirtioDevice)?;
if let Some(dma_handler) = &handle.dma_handler {
if !handle.iommu {
let gpa = new_region.start_addr().0;
let size = new_region.len();
dma_handler
.map(gpa, gpa, size)
.map_err(DeviceManagerError::VirtioDmaMap)?;
}
if let Some(dma_handler) = &handle.dma_handler
&& !handle.iommu
{
let gpa = new_region.start_addr().0;
let size = new_region.len();
dma_handler
.map(gpa, gpa, size)
.map_err(DeviceManagerError::VirtioDmaMap)?;
}
}
@@ -4576,10 +4575,10 @@ impl DeviceManager {
};
let mut iommu_attached = false;
if let Some((_, iommu_attached_devices)) = &self.iommu_attached_devices {
if iommu_attached_devices.contains(&pci_device_bdf) {
iommu_attached = true;
}
if let Some((_, iommu_attached_devices)) = &self.iommu_attached_devices
&& iommu_attached_devices.contains(&pci_device_bdf)
{
iommu_attached = true;
}
let (pci_device, bus_device, virtio_device, remove_dma_handler) = match pci_device_handle {
@@ -4610,16 +4609,16 @@ impl DeviceManager {
.map_err(|e| DeviceManagerError::UnRegisterIoevent(e.into()))?;
}
if let Some(dma_handler) = dev.dma_handler() {
if !iommu_attached {
for (_, zone) in self.memory_manager.lock().unwrap().memory_zones().iter() {
for region in zone.regions() {
let iova = region.start_addr().0;
let size = region.len();
dma_handler
.unmap(iova, size)
.map_err(DeviceManagerError::VirtioDmaUnmap)?;
}
if let Some(dma_handler) = dev.dma_handler()
&& !iommu_attached
{
for (_, zone) in self.memory_manager.lock().unwrap().memory_zones().iter() {
for region in zone.regions() {
let iova = region.start_addr().0;
let size = region.len();
dma_handler
.unmap(iova, size)
.map_err(DeviceManagerError::VirtioDmaUnmap)?;
}
}
}

View File

@@ -428,11 +428,11 @@ pub fn load_igvm(
let gpas_grouped = gpas
.iter()
.fold(Vec::<Vec<GpaPages>>::new(), |mut acc, gpa| {
if let Some(last_vec) = acc.last_mut() {
if last_vec[0].page_type == gpa.page_type {
last_vec.push(*gpa);
return acc;
}
if let Some(last_vec) = acc.last_mut()
&& last_vec[0].page_type == gpa.page_type
{
last_vec.push(*gpa);
return acc;
}
acc.push(vec![*gpa]);
acc

View File

@@ -729,15 +729,14 @@ impl Vmm {
thread::Builder::new()
.name("vmm_signal_handler".to_string())
.spawn(move || {
if !signal_handler_seccomp_filter.is_empty() {
if let Err(e) = apply_filter(&signal_handler_seccomp_filter)
if !signal_handler_seccomp_filter.is_empty() && let Err(e) = apply_filter(&signal_handler_seccomp_filter)
.map_err(Error::ApplySeccompFilter)
{
error!("Error applying seccomp filter: {:?}", e);
exit_evt.write(1).ok();
return;
}
}
if landlock_enable{
match Landlock::new() {
Ok(landlock) => {
@@ -1834,10 +1833,10 @@ impl RequestHandler for Vmm {
if let Some(desired_ram) = desired_ram {
config.memory.size = desired_ram;
}
if let Some(desired_balloon) = desired_balloon {
if let Some(balloon_config) = &mut config.balloon {
balloon_config.size = desired_balloon;
}
if let Some(desired_balloon) = desired_balloon
&& let Some(balloon_config) = &mut config.balloon
{
balloon_config.size = desired_balloon;
}
Ok(())
}
@@ -2306,16 +2305,16 @@ impl RequestHandler for Vmm {
error!("Migration failed: {:?}", migration_err);
// Stop logging dirty pages only for non-local migrations
if !send_data_migration.local {
if let Err(e) = vm.stop_dirty_log() {
return e;
}
if !send_data_migration.local
&& let Err(e) = vm.stop_dirty_log()
{
return e;
}
if vm.get_state().unwrap() == VmState::Paused {
if let Err(e) = vm.resume() {
return e;
}
if vm.get_state().unwrap() == VmState::Paused
&& let Err(e) = vm.resume()
{
return e;
}
migration_err

View File

@@ -1959,23 +1959,21 @@ impl MemoryManager {
}
for region in memory_zone.regions() {
if snapshot {
if let Some(file_offset) = region.file_offset() {
if (region.flags() & libc::MAP_SHARED == libc::MAP_SHARED)
&& Self::is_hardlink(file_offset.file())
{
// In this very specific case, we know the memory
// region is backed by a file on the host filesystem
// that can be accessed by the user, and additionally
// the mapping is shared, which means that modifications
// to the content are written to the actual file.
// When meeting these conditions, we can skip the
// copy of the memory content for this specific region,
// as we can assume the user will have it saved through
// the backing file already.
continue;
}
}
if snapshot
&& let Some(file_offset) = region.file_offset()
&& (region.flags() & libc::MAP_SHARED == libc::MAP_SHARED)
&& Self::is_hardlink(file_offset.file())
{
// In this very specific case, we know the memory
// region is backed by a file on the host filesystem
// that can be accessed by the user, and additionally
// the mapping is shared, which means that modifications
// to the content are written to the actual file.
// When meeting these conditions, we can skip the
// copy of the memory content for this specific region,
// as we can assume the user will have it saved through
// the backing file already.
continue;
}
table.push(MemoryRange {

View File

@@ -432,12 +432,12 @@ impl Drop for SerialManager {
if let Some(handle) = self.handle.take() {
handle.join().ok();
}
if let ConsoleOutput::Socket(_) = self.in_file {
if let Some(socket_path) = self.socket_path.as_ref() {
std::fs::remove_file(socket_path.as_os_str())
.map_err(Error::RemoveUnixSocket)
.ok();
}
if let ConsoleOutput::Socket(_) = self.in_file
&& let Some(socket_path) = self.socket_path.as_ref()
{
std::fs::remove_file(socket_path.as_os_str())
.map_err(Error::RemoveUnixSocket)
.ok();
}
}
}

View File

@@ -3123,12 +3123,12 @@ impl GuestDebuggable for Vm {
#[cfg(feature = "tdx")]
{
if let Some(ref platform) = self.config.lock().unwrap().platform {
if platform.tdx {
return Err(GuestDebuggableError::Coredump(anyhow!(
"Coredump not possible with TDX VM"
)));
}
if let Some(ref platform) = self.config.lock().unwrap().platform
&& platform.tdx
{
return Err(GuestDebuggableError::Coredump(anyhow!(
"Coredump not possible with TDX VM"
)));
}
}