misc: clippy: add map_unwrap_or

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-11-18 12:28:43 +01:00
committed by Rob Bradford
parent dda89d7027
commit d2b19bb969
8 changed files with 22 additions and 26 deletions

View File

@@ -172,6 +172,7 @@ suspicious = "deny"
# Individual Lints # Individual Lints
assertions_on_result_states = "deny" assertions_on_result_states = "deny"
manual_string_new = "deny" manual_string_new = "deny"
map_unwrap_or = "deny"
semicolon_if_nothing_returned = "deny" semicolon_if_nothing_returned = "deny"
undocumented_unsafe_blocks = "deny" undocumented_unsafe_blocks = "deny"
uninlined_format_args = "deny" uninlined_format_args = "deny"

View File

@@ -648,8 +648,10 @@ impl PvmemcontrolBusDevice {
.find_connection(conn) .find_connection(conn)
.ok_or(Error::InvalidConnection(conn.command)) .ok_or(Error::InvalidConnection(conn.command))
}) })
.map(|gpa| self.handle_pvmemcontrol_request(gpa)) .map_or_else(
.unwrap_or_else(|err| warn!("{err:?}")); |err| warn!("{err:?}"),
|gpa| self.handle_pvmemcontrol_request(gpa),
);
} }
} }
} }

View File

@@ -1161,7 +1161,7 @@ fn main() {
if let Some(api_client::Error::ServerResponse(status_code, body)) = if let Some(api_client::Error::ServerResponse(status_code, body)) =
error.downcast_ref::<api_client::Error>() error.downcast_ref::<api_client::Error>()
{ {
let body = body.as_ref().map(|body| body.as_str()).unwrap_or(""); let body = body.as_ref().map_or("", |body| body.as_str());
// Retrieve the list of error messages back. // Retrieve the list of error messages back.
let lines: Vec<&str> = match serde_json::from_str(body) { let lines: Vec<&str> = match serde_json::from_str(body) {

View File

@@ -735,9 +735,7 @@ impl Block {
(disk_nsectors, avail_features, 0, config, false) (disk_nsectors, avail_features, 0, config, false)
}; };
let serial = serial let serial = serial.map_or_else(|| build_serial(&disk_path), Vec::from);
.map(Vec::from)
.unwrap_or_else(|| build_serial(&disk_path));
Ok(Block { Ok(Block {
common: VirtioCommon { common: VirtioCommon {
@@ -773,21 +771,20 @@ impl Block {
// TODO In future, we could add a `lock_granularity=` configuration to the CLI. // TODO In future, we could add a `lock_granularity=` configuration to the CLI.
// For now, we stick to QEMU behavior. // For now, we stick to QEMU behavior.
fn lock_granularity(&mut self) -> LockGranularity { fn lock_granularity(&mut self) -> LockGranularity {
let fallback = LockGranularity::WholeFile; self.disk_image.size().map_or_else(
self.disk_image
.size()
.map(|size| LockGranularity::ByteRange(0, size))
// use a safe fallback // use a safe fallback
.unwrap_or_else(|e| { |e| {
log::warn!( let fallback = LockGranularity::WholeFile;
warn!(
"Can't get disk size for id={},path={}, falling back to {:?}: error: {e}", "Can't get disk size for id={},path={}, falling back to {:?}: error: {e}",
self.id, self.id,
self.disk_path.display(), self.disk_path.display(),
fallback fallback
); );
fallback fallback
}) },
|size| LockGranularity::ByteRange(0, size),
)
} }
/// Tries to set an advisory lock for the corresponding disk image. /// Tries to set an advisory lock for the corresponding disk image.

View File

@@ -1562,8 +1562,7 @@ impl BalloonConfig {
let size = parser let size = parser
.convert::<ByteSized>("size") .convert::<ByteSized>("size")
.map_err(Error::ParseBalloon)? .map_err(Error::ParseBalloon)?
.map(|v| v.0) .map_or(0, |v| v.0);
.unwrap_or(0);
let deflate_on_oom = parser let deflate_on_oom = parser
.convert::<Toggle>("deflate_on_oom") .convert::<Toggle>("deflate_on_oom")
@@ -2482,7 +2481,7 @@ impl VmConfig {
#[cfg(feature = "tdx")] #[cfg(feature = "tdx")]
{ {
let tdx_enabled = self.platform.as_ref().map(|p| p.tdx).unwrap_or(false); let tdx_enabled = self.platform.as_ref().is_some_and(|p| p.tdx);
// At this point we know payload isn't None. // At this point we know payload isn't None.
if tdx_enabled && self.payload.as_ref().unwrap().firmware.is_none() { if tdx_enabled && self.payload.as_ref().unwrap().firmware.is_none() {
return Err(ValidationError::TdxFirmwareMissing); return Err(ValidationError::TdxFirmwareMissing);
@@ -3110,12 +3109,12 @@ impl VmConfig {
#[cfg(feature = "tdx")] #[cfg(feature = "tdx")]
pub fn is_tdx_enabled(&self) -> bool { pub fn is_tdx_enabled(&self) -> bool {
self.platform.as_ref().map(|p| p.tdx).unwrap_or(false) self.platform.as_ref().is_some_and(|p| p.tdx)
} }
#[cfg(feature = "sev_snp")] #[cfg(feature = "sev_snp")]
pub fn is_sev_snp_enabled(&self) -> bool { pub fn is_sev_snp_enabled(&self) -> bool {
self.platform.as_ref().map(|p| p.sev_snp).unwrap_or(false) self.platform.as_ref().is_some_and(|p| p.sev_snp)
} }
} }

View File

@@ -2113,8 +2113,7 @@ impl DeviceManager {
.debug_console .debug_console
.clone() .clone()
.iobase .iobase
.map(|port| port as u64) .map_or(debug_console::DEFAULT_PORT, |port| port as u64);
.unwrap_or(debug_console::DEFAULT_PORT);
self.bus_devices self.bus_devices
.push(Arc::clone(&debug_console) as Arc<dyn BusDeviceSync>); .push(Arc::clone(&debug_console) as Arc<dyn BusDeviceSync>);

View File

@@ -104,7 +104,7 @@ unsafe fn close_unused_fds(keep_fds: &mut [RawFd]) {
// The next fd is the one at i, because the indexes in the // The next fd is the one at i, because the indexes in the
// iterator are offset by one due to the initial 0. // iterator are offset by one due to the initial 0.
let next_keep_fd = keep_fds.get(i); let next_keep_fd = keep_fds.get(i);
let last = next_keep_fd.map(|fd| fd - 1).unwrap_or(RawFd::MAX); let last = next_keep_fd.map_or(RawFd::MAX, |fd| fd - 1);
if first > last { if first > last {
continue; continue;

View File

@@ -763,8 +763,7 @@ impl Vm {
.unwrap() .unwrap()
.payload .payload
.as_ref() .as_ref()
.map(|p| p.fw_cfg_config.is_some()) .is_some_and(|p| p.fw_cfg_config.is_some());
.unwrap_or(false);
if fw_cfg_config { if fw_cfg_config {
device_manager device_manager
.lock() .lock()
@@ -2338,8 +2337,7 @@ impl Vm {
.unwrap() .unwrap()
.payload .payload
.as_ref() .as_ref()
.map(|p| p.fw_cfg_config.is_some()) .is_some_and(|p| p.fw_cfg_config.is_some());
.unwrap_or(false);
if fw_cfg_enabled { if fw_cfg_enabled {
let fw_cfg_config = self let fw_cfg_config = self
.config .config