Skip to content

Commit bbadfbf

Browse files
committed
chore(clippy): update code with new lints
With a new Rust version, new Clippy entered our repository. This commit aligns our codebase with guidance provided by new lints. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 41bda16 commit bbadfbf

36 files changed

+103
-125
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ resolver = "2"
99

1010
[workspace.lints.rust]
1111
missing_debug_implementations = "warn"
12+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(kani)'] }
1213

1314
[workspace.lints.clippy]
1415
ptr_as_ptr = "warn"

src/firecracker/src/api_server/request/actions.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ struct ActionBody {
3030

3131
pub(crate) fn parse_put_actions(body: &Body) -> Result<ParsedRequest, RequestError> {
3232
METRICS.put_api_requests.actions_count.inc();
33-
let action_body = serde_json::from_slice::<ActionBody>(body.raw()).map_err(|err| {
33+
let action_body = serde_json::from_slice::<ActionBody>(body.raw()).inspect_err(|_| {
3434
METRICS.put_api_requests.actions_fails.inc();
35-
err
3635
})?;
3736

3837
match action_body.action_type {

src/firecracker/src/api_server/request/boot_source.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ use super::Body;
1111
pub(crate) fn parse_put_boot_source(body: &Body) -> Result<ParsedRequest, RequestError> {
1212
METRICS.put_api_requests.boot_source_count.inc();
1313
Ok(ParsedRequest::new_sync(VmmAction::ConfigureBootSource(
14-
serde_json::from_slice::<BootSourceConfig>(body.raw()).map_err(|err| {
14+
serde_json::from_slice::<BootSourceConfig>(body.raw()).inspect_err(|_| {
1515
METRICS.put_api_requests.boot_source_fails.inc();
16-
err
1716
})?,
1817
)))
1918
}

src/firecracker/src/api_server/request/drive.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ pub(crate) fn parse_put_drive(
2020
return Err(RequestError::EmptyID);
2121
};
2222

23-
let device_cfg = serde_json::from_slice::<BlockDeviceConfig>(body.raw()).map_err(|err| {
23+
let device_cfg = serde_json::from_slice::<BlockDeviceConfig>(body.raw()).inspect_err(|_| {
2424
METRICS.put_api_requests.drive_fails.inc();
25-
err
2625
})?;
2726

2827
if id != device_cfg.drive_id {
@@ -51,9 +50,8 @@ pub(crate) fn parse_patch_drive(
5150
};
5251

5352
let block_device_update_cfg: BlockDeviceUpdateConfig =
54-
serde_json::from_slice::<BlockDeviceUpdateConfig>(body.raw()).map_err(|err| {
53+
serde_json::from_slice::<BlockDeviceUpdateConfig>(body.raw()).inspect_err(|_| {
5554
METRICS.patch_api_requests.drive_fails.inc();
56-
err
5755
})?;
5856

5957
if id != block_device_update_cfg.drive_id {

src/firecracker/src/api_server/request/logger.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ use super::Body;
1010
pub(crate) fn parse_put_logger(body: &Body) -> Result<ParsedRequest, RequestError> {
1111
METRICS.put_api_requests.logger_count.inc();
1212
let res = serde_json::from_slice::<vmm::logger::LoggerConfig>(body.raw());
13-
let config = res.map_err(|err| {
13+
let config = res.inspect_err(|_| {
1414
METRICS.put_api_requests.logger_fails.inc();
15-
err
1615
})?;
1716
Ok(ParsedRequest::new_sync(VmmAction::ConfigureLogger(config)))
1817
}

src/firecracker/src/api_server/request/machine_configuration.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ pub(crate) fn parse_get_machine_config() -> Result<ParsedRequest, RequestError>
1515

1616
pub(crate) fn parse_put_machine_config(body: &Body) -> Result<ParsedRequest, RequestError> {
1717
METRICS.put_api_requests.machine_cfg_count.inc();
18-
let config = serde_json::from_slice::<MachineConfig>(body.raw()).map_err(|err| {
18+
let config = serde_json::from_slice::<MachineConfig>(body.raw()).inspect_err(|_| {
1919
METRICS.put_api_requests.machine_cfg_fails.inc();
20-
err
2120
})?;
2221

2322
// Check for the presence of deprecated `cpu_template` field.
@@ -44,9 +43,8 @@ pub(crate) fn parse_put_machine_config(body: &Body) -> Result<ParsedRequest, Req
4443
pub(crate) fn parse_patch_machine_config(body: &Body) -> Result<ParsedRequest, RequestError> {
4544
METRICS.patch_api_requests.machine_cfg_count.inc();
4645
let config_update =
47-
serde_json::from_slice::<MachineConfigUpdate>(body.raw()).map_err(|err| {
46+
serde_json::from_slice::<MachineConfigUpdate>(body.raw()).inspect_err(|_| {
4847
METRICS.patch_api_requests.machine_cfg_fails.inc();
49-
err
5048
})?;
5149

5250
if config_update.is_empty() {

src/firecracker/src/api_server/request/metrics.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ use super::Body;
1111
pub(crate) fn parse_put_metrics(body: &Body) -> Result<ParsedRequest, RequestError> {
1212
METRICS.put_api_requests.metrics_count.inc();
1313
Ok(ParsedRequest::new_sync(VmmAction::ConfigureMetrics(
14-
serde_json::from_slice::<MetricsConfig>(body.raw()).map_err(|err| {
14+
serde_json::from_slice::<MetricsConfig>(body.raw()).inspect_err(|_| {
1515
METRICS.put_api_requests.metrics_fails.inc();
16-
err
1716
})?,
1817
)))
1918
}

src/firecracker/src/api_server/request/mmds.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ pub(crate) fn parse_get_mmds() -> Result<ParsedRequest, RequestError> {
1616
}
1717

1818
fn parse_put_mmds_config(body: &Body) -> Result<ParsedRequest, RequestError> {
19-
let config: MmdsConfig = serde_json::from_slice(body.raw()).map_err(|err| {
19+
let config: MmdsConfig = serde_json::from_slice(body.raw()).inspect_err(|_| {
2020
METRICS.put_api_requests.mmds_fails.inc();
21-
err
2221
})?;
2322
// Construct the `ParsedRequest` object.
2423
let version = config.version;
@@ -42,9 +41,8 @@ pub(crate) fn parse_put_mmds(
4241
METRICS.put_api_requests.mmds_count.inc();
4342
match path_second_token {
4443
None => Ok(ParsedRequest::new_sync(VmmAction::PutMMDS(
45-
serde_json::from_slice(body.raw()).map_err(|err| {
44+
serde_json::from_slice(body.raw()).inspect_err(|_| {
4645
METRICS.put_api_requests.mmds_fails.inc();
47-
err
4846
})?,
4947
))),
5048
Some("config") => parse_put_mmds_config(body),
@@ -61,9 +59,8 @@ pub(crate) fn parse_put_mmds(
6159
pub(crate) fn parse_patch_mmds(body: &Body) -> Result<ParsedRequest, RequestError> {
6260
METRICS.patch_api_requests.mmds_count.inc();
6361
Ok(ParsedRequest::new_sync(VmmAction::PatchMMDS(
64-
serde_json::from_slice(body.raw()).map_err(|err| {
62+
serde_json::from_slice(body.raw()).inspect_err(|_| {
6563
METRICS.patch_api_requests.mmds_fails.inc();
66-
err
6764
})?,
6865
)))
6966
}

src/firecracker/src/api_server/request/net.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ pub(crate) fn parse_put_net(
2020
return Err(RequestError::EmptyID);
2121
};
2222

23-
let netif = serde_json::from_slice::<NetworkInterfaceConfig>(body.raw()).map_err(|err| {
23+
let netif = serde_json::from_slice::<NetworkInterfaceConfig>(body.raw()).inspect_err(|_| {
2424
METRICS.put_api_requests.network_fails.inc();
25-
err
2625
})?;
2726
if id != netif.iface_id.as_str() {
2827
METRICS.put_api_requests.network_fails.inc();
@@ -53,9 +52,8 @@ pub(crate) fn parse_patch_net(
5352
};
5453

5554
let netif =
56-
serde_json::from_slice::<NetworkInterfaceUpdateConfig>(body.raw()).map_err(|err| {
55+
serde_json::from_slice::<NetworkInterfaceUpdateConfig>(body.raw()).inspect_err(|_| {
5756
METRICS.patch_api_requests.network_fails.inc();
58-
err
5957
})?;
6058
if id != netif.iface_id {
6159
METRICS.patch_api_requests.network_count.inc();

src/firecracker/src/api_server/request/vsock.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ use super::Body;
1010

1111
pub(crate) fn parse_put_vsock(body: &Body) -> Result<ParsedRequest, RequestError> {
1212
METRICS.put_api_requests.vsock_count.inc();
13-
let vsock_cfg = serde_json::from_slice::<VsockDeviceConfig>(body.raw()).map_err(|err| {
13+
let vsock_cfg = serde_json::from_slice::<VsockDeviceConfig>(body.raw()).inspect_err(|_| {
1414
METRICS.put_api_requests.vsock_fails.inc();
15-
err
1615
})?;
1716

1817
// Check for the presence of deprecated `vsock_id` field.

src/vmm/src/arch/aarch64/cache_info.rs

+30-31
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ pub(crate) enum CacheInfoError {
2323
MissingOptionalAttr(String, CacheEntry),
2424
}
2525

26-
struct CacheEngine {
27-
store: Box<dyn CacheStore>,
26+
struct CacheEngine<T: CacheStore = HostCacheStore> {
27+
store: T,
2828
}
2929

3030
trait CacheStore: std::fmt::Debug {
3131
fn get_by_key(&self, index: u8, file_name: &str) -> Result<String, CacheInfoError>;
3232
}
3333

3434
#[derive(Debug)]
35-
pub(crate) struct CacheEntry {
35+
pub struct CacheEntry {
3636
// Cache Level: 1, 2, 3..
3737
pub level: u8,
3838
// Type of cache: Unified, Data, Instruction.
@@ -45,17 +45,16 @@ pub(crate) struct CacheEntry {
4545
}
4646

4747
#[derive(Debug)]
48-
struct HostCacheStore {
48+
pub struct HostCacheStore {
4949
cache_dir: PathBuf,
5050
}
5151

52-
#[cfg(not(test))]
53-
impl Default for CacheEngine {
52+
impl Default for CacheEngine<HostCacheStore> {
5453
fn default() -> Self {
5554
CacheEngine {
56-
store: Box::new(HostCacheStore {
55+
store: HostCacheStore {
5756
cache_dir: PathBuf::from("/sys/devices/system/cpu/cpu0/cache"),
58-
}),
57+
},
5958
}
6059
}
6160
}
@@ -72,7 +71,7 @@ impl CacheStore for HostCacheStore {
7271
}
7372

7473
impl CacheEntry {
75-
fn from_index(index: u8, store: &dyn CacheStore) -> Result<CacheEntry, CacheInfoError> {
74+
fn from_index(index: u8, store: &impl CacheStore) -> Result<CacheEntry, CacheInfoError> {
7675
let mut err_str = String::new();
7776
let mut cache: CacheEntry = CacheEntry::default();
7877

@@ -287,10 +286,10 @@ pub(crate) fn read_cache_config(
287286
// Also without this mechanism we would be logging the warnings for each level which pollutes
288287
// a lot the logs.
289288
let mut logged_missing_attr = false;
290-
let engine = CacheEngine::default();
289+
let engine = CacheEngine::<HostCacheStore>::default();
291290

292291
for index in 0..=MAX_CACHE_LEVEL {
293-
match CacheEntry::from_index(index, engine.store.as_ref()) {
292+
match CacheEntry::from_index(index, &engine.store) {
294293
Ok(cache) => {
295294
append_cache_level(cache_l1, cache_non_l1, cache);
296295
}
@@ -326,22 +325,22 @@ mod tests {
326325
dummy_fs: HashMap<String, String>,
327326
}
328327

329-
impl Default for CacheEngine {
328+
impl Default for CacheEngine<MockCacheStore> {
330329
fn default() -> Self {
331330
CacheEngine {
332-
store: Box::new(MockCacheStore {
331+
store: MockCacheStore {
333332
dummy_fs: create_default_store(),
334-
}),
333+
},
335334
}
336335
}
337336
}
338337

339-
impl CacheEngine {
338+
impl CacheEngine<MockCacheStore> {
340339
fn new(map: &HashMap<String, String>) -> Self {
341340
CacheEngine {
342-
store: Box::new(MockCacheStore {
341+
store: MockCacheStore {
343342
dummy_fs: map.clone(),
344-
}),
343+
},
345344
}
346345
}
347346
}
@@ -425,12 +424,12 @@ mod tests {
425424
let mut map1 = default_map.clone();
426425
map1.remove("index0/type");
427426
let engine = CacheEngine::new(&map1);
428-
let res = CacheEntry::from_index(0, engine.store.as_ref());
427+
let res = CacheEntry::from_index(0, &engine.store);
429428
// We did create the level file but we still do not have the type file.
430429
assert!(matches!(res.unwrap_err(), CacheInfoError::MissingCacheType));
431430

432431
let engine = CacheEngine::new(&default_map);
433-
let res = CacheEntry::from_index(0, engine.store.as_ref());
432+
let res = CacheEntry::from_index(0, &engine.store);
434433
assert_eq!(
435434
format!("{}", res.unwrap_err()),
436435
"shared cpu map, coherency line size, size, number of sets",
@@ -440,15 +439,15 @@ mod tests {
440439
let mut map2 = default_map.clone();
441440
map2.insert("index0/level".to_string(), "d".to_string());
442441
let engine = CacheEngine::new(&map2);
443-
let res = CacheEntry::from_index(0, engine.store.as_ref());
442+
let res = CacheEntry::from_index(0, &engine.store);
444443
assert_eq!(
445444
format!("{}", res.unwrap_err()),
446445
"Invalid cache configuration found for level: invalid digit found in string"
447446
);
448447

449448
default_map.insert("index0/type".to_string(), "Instructionn".to_string());
450449
let engine = CacheEngine::new(&default_map);
451-
let res = CacheEntry::from_index(0, engine.store.as_ref());
450+
let res = CacheEntry::from_index(0, &engine.store);
452451
assert_eq!(
453452
format!("{}", res.unwrap_err()),
454453
"Invalid cache configuration found for type: Instructionn"
@@ -464,7 +463,7 @@ mod tests {
464463
"00000000,00000001".to_string(),
465464
);
466465
let engine = CacheEngine::new(&default_map);
467-
let res = CacheEntry::from_index(0, engine.store.as_ref());
466+
let res = CacheEntry::from_index(0, &engine.store);
468467
assert_eq!(
469468
format!("{}", res.unwrap_err()),
470469
"coherency line size, size, number of sets"
@@ -475,15 +474,15 @@ mod tests {
475474
"00000000,0000000G".to_string(),
476475
);
477476
let engine = CacheEngine::new(&default_map);
478-
let res = CacheEntry::from_index(0, engine.store.as_ref());
477+
let res = CacheEntry::from_index(0, &engine.store);
479478
assert_eq!(
480479
format!("{}", res.unwrap_err()),
481480
"Invalid cache configuration found for shared_cpu_map: invalid digit found in string"
482481
);
483482

484483
default_map.insert("index0/shared_cpu_map".to_string(), "00000000".to_string());
485484
let engine = CacheEngine::new(&default_map);
486-
let res = CacheEntry::from_index(0, engine.store.as_ref());
485+
let res = CacheEntry::from_index(0, &engine.store);
487486
assert_eq!(
488487
format!("{}", res.unwrap_err()),
489488
"Invalid cache configuration found for shared_cpu_map: 00000000"
@@ -496,7 +495,7 @@ mod tests {
496495

497496
default_map.insert("index0/coherency_line_size".to_string(), "64".to_string());
498497
let engine = CacheEngine::new(&default_map);
499-
let res = CacheEntry::from_index(0, engine.store.as_ref());
498+
let res = CacheEntry::from_index(0, &engine.store);
500499
assert_eq!(
501500
"shared cpu map, size, number of sets",
502501
format!("{}", res.unwrap_err())
@@ -507,7 +506,7 @@ mod tests {
507506
"Instruction".to_string(),
508507
);
509508
let engine = CacheEngine::new(&default_map);
510-
let res = CacheEntry::from_index(0, engine.store.as_ref());
509+
let res = CacheEntry::from_index(0, &engine.store);
511510
assert_eq!(
512511
format!("{}", res.unwrap_err()),
513512
"Invalid cache configuration found for coherency_line_size: invalid digit found in \
@@ -521,23 +520,23 @@ mod tests {
521520

522521
default_map.insert("index0/size".to_string(), "64K".to_string());
523522
let engine = CacheEngine::new(&default_map);
524-
let res = CacheEntry::from_index(0, engine.store.as_ref());
523+
let res = CacheEntry::from_index(0, &engine.store);
525524
assert_eq!(
526525
format!("{}", res.unwrap_err()),
527526
"shared cpu map, coherency line size, number of sets",
528527
);
529528

530529
default_map.insert("index0/size".to_string(), "64".to_string());
531530
let engine = CacheEngine::new(&default_map);
532-
let res = CacheEntry::from_index(0, engine.store.as_ref());
531+
let res = CacheEntry::from_index(0, &engine.store);
533532
assert_eq!(
534533
format!("{}", res.unwrap_err()),
535534
"Invalid cache configuration found for size: 64"
536535
);
537536

538537
default_map.insert("index0/size".to_string(), "64Z".to_string());
539538
let engine = CacheEngine::new(&default_map);
540-
let res = CacheEntry::from_index(0, engine.store.as_ref());
539+
let res = CacheEntry::from_index(0, &engine.store);
541540
assert_eq!(
542541
format!("{}", res.unwrap_err()),
543542
"Invalid cache configuration found for size: 64Z"
@@ -550,15 +549,15 @@ mod tests {
550549

551550
default_map.insert("index0/number_of_sets".to_string(), "64".to_string());
552551
let engine = CacheEngine::new(&default_map);
553-
let res = CacheEntry::from_index(0, engine.store.as_ref());
552+
let res = CacheEntry::from_index(0, &engine.store);
554553
assert_eq!(
555554
"shared cpu map, coherency line size, size",
556555
format!("{}", res.unwrap_err())
557556
);
558557

559558
default_map.insert("index0/number_of_sets".to_string(), "64K".to_string());
560559
let engine = CacheEngine::new(&default_map);
561-
let res = CacheEntry::from_index(0, engine.store.as_ref());
560+
let res = CacheEntry::from_index(0, &engine.store);
562561
assert_eq!(
563562
format!("{}", res.unwrap_err()),
564563
"Invalid cache configuration found for number_of_sets: invalid digit found in string"

src/vmm/src/devices/legacy/serial.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ mod tests {
386386
),
387387
input: None::<std::io::Stdin>,
388388
};
389-
serial.serial.raw_input(&[b'a', b'b', b'c']).unwrap();
389+
serial.serial.raw_input(b"abc").unwrap();
390390

391391
let invalid_reads_before = metrics.missed_read_count.count();
392392
let mut v = [0x00; 2];

src/vmm/src/devices/virtio/balloon/metrics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//!
3232
//! The system implements 1 type of metrics:
3333
//! * Shared Incremental Metrics (SharedIncMetrics) - dedicated for the metrics which need a counter
34-
//! (i.e the number of times an API request failed). These metrics are reset upon flush.
34+
//! (i.e the number of times an API request failed). These metrics are reset upon flush.
3535
3636
use serde::ser::SerializeMap;
3737
use serde::{Serialize, Serializer};

0 commit comments

Comments
 (0)