From cf5d2518633e4c2f14d6160ae3531ab2f15e462c Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Wed, 5 Jun 2024 14:17:00 -0700 Subject: [PATCH] Fix clippy::unnecessary_get_then_check warnings Avoid .get()ting something just to check if it's present; use .contains_key() instead (or .entry() in the case where we want to insert an item if the key is not present). BUG=b:344974550 TEST=tools/clippy Change-Id: I78ee9e5a7ad9ffd693d9a50a347663444e1516a2 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5609074 Reviewed-by: Alexandre Courbot Commit-Queue: Daniel Verkamp --- devices/src/pci/pcie/pcie_switch.rs | 2 +- devices/src/vfio.rs | 2 +- resources/src/system_allocator.rs | 47 ++++++++++++++--------------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/devices/src/pci/pcie/pcie_switch.rs b/devices/src/pci/pcie/pcie_switch.rs index 177a385d89..13892692f4 100644 --- a/devices/src/pci/pcie/pcie_switch.rs +++ b/devices/src/pci/pcie/pcie_switch.rs @@ -221,7 +221,7 @@ impl HotPlugBus for PcieDownstreamPort { if !self.pcie_port.hotplug_implemented() { bail!("hotplug not implemented."); } - if self.downstream_devices.get(&addr).is_none() { + if !self.downstream_devices.contains_key(&addr) { bail!("no downstream devices."); } if !self.pcie_port.is_hotplug_ready() { diff --git a/devices/src/vfio.rs b/devices/src/vfio.rs index b0ddd47a6f..5f21e53907 100644 --- a/devices/src/vfio.rs +++ b/devices/src/vfio.rs @@ -361,7 +361,7 @@ impl VfioContainer { } fn is_group_set(&self, group_id: u32) -> bool { - self.groups.get(&group_id).is_some() + self.groups.contains_key(&group_id) } fn check_extension(&self, val: IommuType) -> bool { diff --git a/resources/src/system_allocator.rs b/resources/src/system_allocator.rs index 4e11a98f51..ac3729f542 100644 --- a/resources/src/system_allocator.rs +++ b/resources/src/system_allocator.rs @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +use std::collections::btree_map; use std::collections::BTreeMap; use base::pagesize; @@ -270,36 +271,34 @@ impl SystemAllocator { } fn get_pci_allocator_mut(&mut self, bus: u8) -> Option<&mut AddressAllocator> { - // pci root is 00:00.0, Bus 0 next device is 00:01.0 with mandatory function - // number zero. - if self.pci_allocator.get(&bus).is_none() { - let base = if bus == 0 { 8 } else { 0 }; + match self.pci_allocator.entry(bus) { + btree_map::Entry::Occupied(entry) => Some(entry.into_mut()), + btree_map::Entry::Vacant(entry) => { + // pci root is 00:00.0, Bus 0 next device is 00:01.0 with mandatory function number + // zero. + let base = if bus == 0 { 8 } else { 0 }; - // Each bus supports up to 32 (devices) x 8 (functions). - // Prefer allocating at device granularity (preferred_align = 8), but fall back to - // allocating individual functions (min_align = 1) when we run out of devices. - match AddressAllocator::new( - AddressRange { - start: base, - end: (32 * 8) - 1, - }, - Some(1), - Some(8), - ) { - Ok(v) => self.pci_allocator.insert(bus, v), - Err(_) => return None, - }; + // Each bus supports up to 32 (devices) x 8 (functions). + // Prefer allocating at device granularity (preferred_align = 8), but fall back to + // allocating individual functions (min_align = 1) when we run out of devices. + let pci_alloc = AddressAllocator::new( + AddressRange { + start: base, + end: (32 * 8) - 1, + }, + Some(1), + Some(8), + ) + .ok()?; + + Some(entry.insert(pci_alloc)) + } } - self.pci_allocator.get_mut(&bus) } // Check whether devices exist or not on the specified bus pub fn pci_bus_empty(&self, bus: u8) -> bool { - if self.pci_allocator.get(&bus).is_none() { - true - } else { - false - } + !self.pci_allocator.contains_key(&bus) } /// Allocate PCI slot location.