From 69bcb077440a4467e0b9e32b51f069d01a2d6405 Mon Sep 17 00:00:00 2001 From: utam0k Date: Tue, 7 Sep 2021 23:10:39 +0900 Subject: [PATCH] fix cargo clippy warning in cgroups. --- cgroups/Cargo.lock | 4 +-- cgroups/src/v1/manager.rs | 2 +- cgroups/src/v2/devices/bpf.rs | 3 +- cgroups/src/v2/devices/controller.rs | 2 +- cgroups/src/v2/devices/emulator.rs | 2 +- cgroups/src/v2/devices/program.rs | 5 +-- cgroups/src/v2/manager.rs | 7 ++-- cgroups/src/v2/unified.rs | 53 ++++++++++++++++------------ 8 files changed, 43 insertions(+), 35 deletions(-) diff --git a/cgroups/Cargo.lock b/cgroups/Cargo.lock index 3174005d68..68b066505e 100644 --- a/cgroups/Cargo.lock +++ b/cgroups/Cargo.lock @@ -464,9 +464,9 @@ dependencies = [ [[package]] name = "procfs" -version = "0.9.1" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8809e0c18450a2db0f236d2a44ec0b4c1412d0eb936233579f0990faa5d5cd" +checksum = "95e344cafeaeefe487300c361654bcfc85db3ac53619eeccced29f5ea18c4c70" dependencies = [ "bitflags", "byteorder", diff --git a/cgroups/src/v1/manager.rs b/cgroups/src/v1/manager.rs index 0288862be0..546e6beae9 100644 --- a/cgroups/src/v1/manager.rs +++ b/cgroups/src/v1/manager.rs @@ -51,7 +51,7 @@ impl Manager { let p = if cgroup_path.to_string_lossy().into_owned().is_empty() { mount_point.join_safely(Path::new(&cgroup.pathname))? } else if cgroup_path.is_absolute() { - mount_point.join_safely(&cgroup_path)? + mount_point.join_safely(cgroup_path)? } else { mount_point.join(cgroup_path) }; diff --git a/cgroups/src/v2/devices/bpf.rs b/cgroups/src/v2/devices/bpf.rs index af3228fc71..4b3259cb6f 100644 --- a/cgroups/src/v2/devices/bpf.rs +++ b/cgroups/src/v2/devices/bpf.rs @@ -1,5 +1,6 @@ use anyhow::{bail, Result}; use std::os::unix::io::RawFd; +use std::ptr; // FIXME: add tests @@ -14,7 +15,7 @@ pub fn prog_load(license: &str, insns: &[u8]) -> Result { insns_cnt as u64, license as *const _ as *const i8, 0, - 0 as *mut i8, + ptr::null_mut::(), 0, ) }; diff --git a/cgroups/src/v2/devices/controller.rs b/cgroups/src/v2/devices/controller.rs index 9b969206b2..6dfdd9c8b4 100644 --- a/cgroups/src/v2/devices/controller.rs +++ b/cgroups/src/v2/devices/controller.rs @@ -11,7 +11,7 @@ use oci_spec::{LinuxDeviceCgroup, LinuxResources}; use crate::common::{default_allow_devices, default_devices}; use crate::v2::controller::Controller; -const LICENSE: &'static str = &"Apache"; +const LICENSE: &str = "Apache"; pub struct Devices {} diff --git a/cgroups/src/v2/devices/emulator.rs b/cgroups/src/v2/devices/emulator.rs index ef34c9ed13..5b6aefaa95 100644 --- a/cgroups/src/v2/devices/emulator.rs +++ b/cgroups/src/v2/devices/emulator.rs @@ -38,7 +38,7 @@ impl Emulator { pub fn add_rule(&mut self, rule: &oci_spec::LinuxDeviceCgroup) -> Result<()> { // special case, switch to blacklist or whitelist and clear all existing rules // NOTE: we ignore other fields when type='a', this is same as cgroup v1 and runc - if rule.typ.clone().unwrap_or_default() == oci_spec::LinuxDeviceType::A { + if rule.typ.unwrap_or_default() == oci_spec::LinuxDeviceType::A { self.default_allow = rule.allow; self.rules.clear(); return Ok(()); diff --git a/cgroups/src/v2/devices/program.rs b/cgroups/src/v2/devices/program.rs index 548eb903d9..e30be5d614 100644 --- a/cgroups/src/v2/devices/program.rs +++ b/cgroups/src/v2/devices/program.rs @@ -10,7 +10,7 @@ pub struct Program { } impl Program { - pub fn from_rules(rules: &Vec, default_allow: bool) -> Result { + pub fn from_rules(rules: &[LinuxDeviceCgroup], default_allow: bool) -> Result { let mut prog = Program { prog: BpfCode::new(), }; @@ -90,7 +90,7 @@ impl Program { } fn add_rule(&mut self, rule: &LinuxDeviceCgroup) -> Result<()> { - let dev_type = bpf_dev_type(rule.typ.clone().unwrap_or_default())?; + let dev_type = bpf_dev_type(rule.typ.unwrap_or_default())?; let access = bpf_access(rule.access.clone().unwrap_or_default())?; let has_access = access != (libbpf_sys::BPF_DEVCG_ACC_READ @@ -245,6 +245,7 @@ fn bpf_cgroup_dev_ctx( Ok(mem) } +#[cfg(test)] mod tests { use super::*; diff --git a/cgroups/src/v2/manager.rs b/cgroups/src/v2/manager.rs index 3c38f95015..b20588c3ba 100644 --- a/cgroups/src/v2/manager.rs +++ b/cgroups/src/v2/manager.rs @@ -139,13 +139,12 @@ impl CgroupManager for Manager { Devices::apply(linux_resources, &self.cgroup_path)?; for pseudoctlr in PSEUDO_CONTROLLER_TYPES { - match pseudoctlr { - PseudoControllerType::Unified => Unified::apply( + if let PseudoControllerType::Unified = pseudoctlr { + Unified::apply( linux_resources, &self.cgroup_path, self.get_available_controllers()?, - )?, - _ => {} + )? } } diff --git a/cgroups/src/v2/unified.rs b/cgroups/src/v2/unified.rs index 5de817cab1..cdb6481461 100644 --- a/cgroups/src/v2/unified.rs +++ b/cgroups/src/v2/unified.rs @@ -47,9 +47,7 @@ impl Unified { #[cfg(test)] mod tests { use std::array::IntoIter; - use std::collections::HashMap; use std::fs; - use std::iter::FromIterator; use crate::test::{create_temp_dir, set_fixture}; use crate::v2::controller_type::ControllerType; @@ -64,13 +62,16 @@ mod tests { let cpu_weight_path = set_fixture(&tmp, "cpu.weight", "").unwrap(); let resources = LinuxResources { - unified: Some(HashMap::<_, _>::from_iter(IntoIter::new([ - ( - "hugetlb.1GB.limit_in_bytes".to_owned(), - "72348034".to_owned(), - ), - ("cpu.weight".to_owned(), "5000".to_owned()), - ]))), + unified: Some( + IntoIter::new([ + ( + "hugetlb.1GB.limit_in_bytes".to_owned(), + "72348034".to_owned(), + ), + ("cpu.weight".to_owned(), "5000".to_owned()), + ]) + .collect(), + ), ..Default::default() }; @@ -91,13 +92,16 @@ mod tests { create_temp_dir("test_set_unified_failed_to_write_subsystem_not_enabled").unwrap(); let resources = LinuxResources { - unified: Some(HashMap::<_, _>::from_iter(IntoIter::new([ - ( - "hugetlb.1GB.limit_in_bytes".to_owned(), - "72348034".to_owned(), - ), - ("cpu.weight".to_owned(), "5000".to_owned()), - ]))), + unified: Some( + IntoIter::new([ + ( + "hugetlb.1GB.limit_in_bytes".to_owned(), + "72348034".to_owned(), + ), + ("cpu.weight".to_owned(), "5000".to_owned()), + ]) + .collect(), + ), ..Default::default() }; @@ -114,13 +118,16 @@ mod tests { let tmp = create_temp_dir("test_set_unified_failed_to_write_subsystem_enabled").unwrap(); let resources = LinuxResources { - unified: Some(HashMap::<_, _>::from_iter(IntoIter::new([ - ( - "hugetlb.1GB.limit_in_bytes".to_owned(), - "72348034".to_owned(), - ), - ("cpu.weight".to_owned(), "5000".to_owned()), - ]))), + unified: Some( + IntoIter::new([ + ( + "hugetlb.1GB.limit_in_bytes".to_owned(), + "72348034".to_owned(), + ), + ("cpu.weight".to_owned(), "5000".to_owned()), + ]) + .collect(), + ), ..Default::default() };