Skip to content

Commit

Permalink
Merge pull request #870 from GuillaumeGomez/clippy-lints
Browse files Browse the repository at this point in the history
Fix new clippy lints
  • Loading branch information
GuillaumeGomez committed Oct 25, 2022
2 parents ab563bf + d3daee6 commit 52ff70e
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 43 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/CI.yml
Expand Up @@ -5,6 +5,11 @@ on:

name: CI

# If a new push on the PR is done, cancel the build
concurrency:
group: ${{ github.head_ref }}
cancel-in-progress: true

jobs:
rustfmt:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -34,7 +39,7 @@ jobs:
toolchain: nightly
override: true
components: clippy
- run: cargo clippy -- -D warnings
- run: cargo clippy --all-targets -- -D warnings

check:
name: Check ${{ matrix.toolchain }} / ${{ matrix.triple.target }}
Expand Down
17 changes: 9 additions & 8 deletions examples/simple.rs
Expand Up @@ -2,6 +2,7 @@

#![crate_type = "bin"]
#![allow(unused_must_use, non_upper_case_globals)]
#![allow(clippy::manual_range_contains)]

extern crate sysinfo;

Expand Down Expand Up @@ -152,7 +153,7 @@ fn interpret_input(input: &str, sys: &mut System) -> bool {
let mut nb = 1i32;

for sig in signals {
writeln!(&mut io::stdout(), "{:2}:{:?}", nb, sig);
writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
nb += 1;
}
}
Expand All @@ -172,7 +173,7 @@ fn interpret_input(input: &str, sys: &mut System) -> bool {
sys.global_cpu_info().cpu_usage()
);
for proc_ in sys.cpus() {
writeln!(&mut io::stdout(), "{:?}", proc_);
writeln!(&mut io::stdout(), "{proc_:?}");
}
}
"memory" => {
Expand Down Expand Up @@ -244,19 +245,19 @@ fn interpret_input(input: &str, sys: &mut System) -> bool {
} else if let Ok(pid) = Pid::from_str(tmp[1]) {
match sys.process(pid) {
Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
None => writeln!(&mut io::stdout(), "pid \"{:?}\" not found", pid),
None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
};
} else {
let proc_name = tmp[1];
for proc_ in sys.processes_by_name(proc_name) {
writeln!(&mut io::stdout(), "==== {} ====", proc_.name());
writeln!(&mut io::stdout(), "{:?}", proc_);
writeln!(&mut io::stdout(), "{proc_:?}");
}
}
}
"temperature" => {
for component in sys.components() {
writeln!(&mut io::stdout(), "{:?}", component);
writeln!(&mut io::stdout(), "{component:?}");
}
}
"network" => {
Expand Down Expand Up @@ -303,7 +304,7 @@ fn interpret_input(input: &str, sys: &mut System) -> bool {
if let Some(res) =
p.kill_with(*signals.get(signal as usize - 1).unwrap())
{
writeln!(&mut io::stdout(), "kill: {}", res,);
writeln!(&mut io::stdout(), "kill: {res}");
} else {
writeln!(
&mut io::stdout(),
Expand All @@ -320,7 +321,7 @@ fn interpret_input(input: &str, sys: &mut System) -> bool {
}
"disks" => {
for disk in sys.disks() {
writeln!(&mut io::stdout(), "{:?}", disk);
writeln!(&mut io::stdout(), "{disk:?}");
}
}
"users" => {
Expand Down Expand Up @@ -362,7 +363,7 @@ fn interpret_input(input: &str, sys: &mut System) -> bool {
.next()
{
if sys.refresh_process(pid) {
writeln!(&mut io::stdout(), "Process `{}` updated successfully", pid);
writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
} else {
writeln!(
&mut io::stdout(),
Expand Down
4 changes: 2 additions & 2 deletions src/apple/cpu.rs
Expand Up @@ -317,14 +317,14 @@ mod test {
let cpus = sys.cpus();
assert!(!cpus.is_empty(), "no CPU found");
if let Some(line) = stdout.lines().find(|l| l.contains("machdep.cpu.vendor")) {
let sysctl_value = line.split(":").skip(1).next().unwrap();
let sysctl_value = line.split(':').nth(1).unwrap();
assert_eq!(cpus[0].vendor_id(), sysctl_value.trim());
}
if let Some(line) = stdout
.lines()
.find(|l| l.contains("machdep.cpu.brand_string"))
{
let sysctl_value = line.split(":").skip(1).next().unwrap();
let sysctl_value = line.split(':').nth(1).unwrap();
assert_eq!(cpus[0].brand(), sysctl_value.trim());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/debug.rs
Expand Up @@ -105,7 +105,7 @@ impl fmt::Debug for Networks {
f,
"Networks {{ {} }}",
self.iter()
.map(|x| format!("{:?}", x))
.map(|x| format!("{x:?}"))
.collect::<Vec<_>>()
.join(", ")
)
Expand Down
4 changes: 1 addition & 3 deletions src/freebsd/component.rs
Expand Up @@ -55,9 +55,7 @@ pub unsafe fn get_components(nb_cpus: usize) -> Vec<Component> {
let mut components = Vec::with_capacity(nb_cpus);

for core in 0..nb_cpus {
let id = format!("dev.cpu.{}.temperature\0", core)
.as_bytes()
.to_vec();
let id = format!("dev.cpu.{core}.temperature\0").as_bytes().to_vec();
if let Some(temperature) = refresh_component(&id) {
components.push(Component {
id,
Expand Down
7 changes: 2 additions & 5 deletions src/freebsd/system.rs
Expand Up @@ -126,11 +126,8 @@ impl SystemExt for System {
frequency = get_frequency_for_cpu(pos);
}
}
self.cpus.push(Cpu::new(
format!("cpu {}", pos),
vendor_id.clone(),
frequency,
));
self.cpus
.push(Cpu::new(format!("cpu {pos}"), vendor_id.clone(), frequency));
}
self.global_cpu.vendor_id = vendor_id;
self.got_cpu_frequency = refresh_kind.frequency();
Expand Down
2 changes: 1 addition & 1 deletion src/freebsd/utils.rs
Expand Up @@ -287,7 +287,7 @@ pub(crate) unsafe fn get_frequency_for_cpu(cpu_nb: c_int) -> u64 {

// The information can be missing if it's running inside a VM.
if !get_sys_value_by_name(
format!("dev.cpu.{}.freq\0", cpu_nb).as_bytes(),
format!("dev.cpu.{cpu_nb}.freq\0").as_bytes(),
&mut frequency,
) {
frequency = 0;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -7,6 +7,7 @@
#![allow(clippy::upper_case_acronyms)]
#![allow(clippy::non_send_fields_in_send_ty)]
#![allow(renamed_and_removed_lints)]
#![allow(clippy::assertions_on_constants)]
#![allow(unknown_lints)]

#[macro_use]
Expand Down
8 changes: 4 additions & 4 deletions src/linux/component.rs
Expand Up @@ -284,11 +284,11 @@ impl Component {
let has_label = !label.is_empty();
match (has_label, device_model) {
(true, Some(device_model)) => {
format!("{} {} {} {}{}", name, label, device_model, class, id)
format!("{name} {label} {device_model} {class}{id}")
}
(true, None) => format!("{} {}", name, label),
(false, Some(device_model)) => format!("{} {}", name, device_model),
(false, None) => format!("{} {}{}", name, class, id),
(true, None) => format!("{name} {label}"),
(false, Some(device_model)) => format!("{name} {device_model}"),
(false, None) => format!("{name} {class}{id}"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/linux/network.rs
Expand Up @@ -297,7 +297,7 @@ mod test {
let itf1_dir = sys_net_dir.path().join("itf1");
let itf2_dir = sys_net_dir.path().join("itf2");
fs::create_dir(&itf1_dir).expect("failed to create subdirectory");
fs::create_dir(&itf2_dir).expect("failed to create subdirectory");
fs::create_dir(itf2_dir).expect("failed to create subdirectory");

let mut interfaces = HashMap::new();

Expand Down
4 changes: 2 additions & 2 deletions src/traits.rs
Expand Up @@ -415,7 +415,7 @@ pub trait ProcessExt: Debug {

/// Wait for process termination.
///
///```no_run
/// ```no_run
/// use sysinfo::{Pid, ProcessExt, System, SystemExt};
///
/// let mut s = System::new_all();
Expand All @@ -425,7 +425,7 @@ pub trait ProcessExt: Debug {
/// process.wait();
/// eprintln!("Pid 1337 exited");
/// }
///````
/// ```
fn wait(&self);
}

Expand Down
8 changes: 4 additions & 4 deletions src/windows/system.rs
Expand Up @@ -130,10 +130,10 @@ impl SystemExt for System {
);
for (pos, proc_) in self.cpus.iter_mut(refresh_kind).enumerate() {
add_english_counter(
format!(r"\Processor({})\% Processor Time", pos),
format!(r"\Processor({pos})\% Processor Time"),
query,
get_key_used(proc_),
format!("{}_0", pos),
format!("{pos}_0"),
);
}
}
Expand Down Expand Up @@ -494,7 +494,7 @@ impl SystemExt for System {
.unwrap_or_default(),
)
};
Some(format!("{} ({})", major, build_number))
Some(format!("{major} ({build_number})"))
}

fn distribution_id(&self) -> String {
Expand Down Expand Up @@ -550,7 +550,7 @@ pub(crate) fn get_process_name(process: &SYSTEM_PROCESS_INFORMATION, process_id:
match process_id.0 {
0 => "Idle".to_owned(),
4 => "System".to_owned(),
_ => format!("<no name> Process {}", process_id),
_ => format!("<no name> Process {process_id}"),
}
} else {
unsafe {
Expand Down
4 changes: 2 additions & 2 deletions tests/code_checkers/docs.rs
Expand Up @@ -45,9 +45,9 @@ fn check_md_doc_path(p: &Path, md_line: &str, ty_line: &str) -> bool {
return false;
}
}
show_error(p, &format!("Cannot extract type name from `{}`", ty_line));
show_error(p, &format!("Cannot extract type name from `{ty_line}`"));
} else {
show_error(p, &format!("Cannot extract md name from `{}`", md_line));
show_error(p, &format!("Cannot extract md name from `{md_line}`"));
}
false
}
Expand Down
2 changes: 1 addition & 1 deletion tests/code_checkers/headers.rs
Expand Up @@ -19,7 +19,7 @@ pub fn check_license_header(content: &str, p: &Path) -> TestResult {
Some(s) => {
show_error(
p,
&format!("Expected empty line after license header, found `{}`", s),
&format!("Expected empty line after license header, found `{s}`"),
);
TestResult {
nb_tests: 1,
Expand Down
1 change: 1 addition & 0 deletions tests/code_checkers/mod.rs
Expand Up @@ -8,6 +8,7 @@ mod utils;
use std::path::Path;
use utils::TestResult;

#[allow(clippy::type_complexity)]
const CHECKS: &[(fn(&str, &Path) -> TestResult, &[&str])] = &[
(headers::check_license_header, &["src", "tests", "examples"]),
(signals::check_signals, &["src"]),
Expand Down
2 changes: 1 addition & 1 deletion tests/code_checkers/utils.rs
Expand Up @@ -45,5 +45,5 @@ fn read_file<P: AsRef<Path>>(p: P) -> String {
}

pub fn show_error(p: &Path, err: &str) {
eprintln!("=> [{}]: {}", p.display(), err);
eprintln!("=> [{}]: {err}", p.display());
}
16 changes: 9 additions & 7 deletions tests/process.rs
Expand Up @@ -38,7 +38,7 @@ fn test_cwd() {
.unwrap()
};

let pid = Pid::from_u32(p.id() as u32);
let pid = Pid::from_u32(p.id() as _);
std::thread::sleep(std::time::Duration::from_secs(1));
let mut s = sysinfo::System::new();
s.refresh_processes();
Expand Down Expand Up @@ -81,7 +81,7 @@ fn test_cmd() {
s.refresh_processes();
p.kill().expect("Unable to kill process.");
assert!(!s.processes().is_empty());
if let Some(process) = s.process(Pid::from_u32(p.id() as u32)) {
if let Some(process) = s.process(Pid::from_u32(p.id() as _)) {
if cfg!(target_os = "windows") {
// Sometimes, we get the full path instead for some reasons... So just in case,
// we check for the command independently that from the arguments.
Expand Down Expand Up @@ -120,7 +120,7 @@ fn test_environ() {
.unwrap()
};

let pid = Pid::from_u32(p.id() as u32);
let pid = Pid::from_u32(p.id() as _);
std::thread::sleep(std::time::Duration::from_secs(1));
let mut s = sysinfo::System::new();
s.refresh_processes();
Expand Down Expand Up @@ -224,6 +224,8 @@ fn cpu_usage_is_not_nan() {
return;
}

// We need `collect` otherwise we can't have mutable access to `system`.
#[allow(clippy::needless_collect)]
let first_pids = system
.processes()
.iter()
Expand Down Expand Up @@ -265,7 +267,7 @@ fn test_process_times() {
.unwrap()
};

let pid = Pid::from_u32(p.id() as u32);
let pid = Pid::from_u32(p.id() as _);
std::thread::sleep(std::time::Duration::from_secs(1));
let mut s = sysinfo::System::new();
s.refresh_processes();
Expand Down Expand Up @@ -313,7 +315,7 @@ fn test_refresh_processes() {
.unwrap()
};

let pid = Pid::from_u32(p.id() as u32);
let pid = Pid::from_u32(p.id() as _);
std::thread::sleep(std::time::Duration::from_secs(1));

// Checks that the process is listed as it should.
Expand Down Expand Up @@ -357,7 +359,7 @@ fn test_refresh_process() {
.unwrap()
};

let pid = Pid::from_u32(p.id() as u32);
let pid = Pid::from_u32(p.id() as _);
std::thread::sleep(std::time::Duration::from_secs(1));

// Checks that the process is listed as it should.
Expand Down Expand Up @@ -401,7 +403,7 @@ fn test_wait_child() {
};

let before = std::time::Instant::now();
let pid = Pid::from_u32(p.id() as u32);
let pid = Pid::from_u32(p.id() as _);

let mut s = sysinfo::System::new();
s.refresh_process(pid);
Expand Down

0 comments on commit 52ff70e

Please sign in to comment.