Skip to content

Commit a1e0255

Browse files
Fix new clippies on linux (#89)
* Fix new clippy errors related to using variables in formats statements on Linux
1 parent 5b88f88 commit a1e0255

File tree

6 files changed

+28
-29
lines changed

6 files changed

+28
-29
lines changed

.github/workflows/rust.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ env:
1414

1515
jobs:
1616
build:
17-
1817
runs-on: ${{ matrix.os }}
1918
strategy:
2019
matrix:

src/dmesg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::libproc::libproc::kmesg_buffer;
1313

1414
fn main() {
1515
match kmesg_buffer::kmsgbuf() {
16-
Ok(message) => print!("{}", message),
17-
Err(e) => eprintln!("{}", e)
16+
Ok(message) => print!("{message}"),
17+
Err(e) => eprintln!("{e}")
1818
}
1919
}

src/libproc/helpers.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::io::{BufRead, BufReader};
99
pub fn get_errno_with_message(return_code: i32) -> String {
1010
let e = errno();
1111
let code = e.0;
12-
format!("return code = {}, errno = {}, message = '{}'", return_code, code, e)
12+
format!("return code = {return_code}, errno = {code}, message = '{e}'")
1313
}
1414

1515
/// Helper function that depending on the `ret` value:
@@ -25,7 +25,7 @@ pub fn check_errno(ret: i32, buf: &mut Vec<u8>) -> Result<String, String> {
2525

2626
match String::from_utf8(buf.to_vec()) {
2727
Ok(return_value) => Ok(return_value),
28-
Err(e) => Err(format!("Invalid UTF-8 sequence: {}", e))
28+
Err(e) => Err(format!("Invalid UTF-8 sequence: {e}"))
2929
}
3030
}
3131
}
@@ -35,10 +35,10 @@ pub fn check_errno(ret: i32, buf: &mut Vec<u8>) -> Result<String, String> {
3535
/// This will be more useful when implementing more linux functions
3636
pub fn procfile_field(filename: &str, field_name: &str) -> Result<String, String> {
3737
const SEPARATOR: &str = ":";
38-
let line_header = format!("{}{}", field_name, SEPARATOR);
38+
let line_header = format!("{field_name}{SEPARATOR}");
3939

4040
// Open the file in read-only mode (ignoring errors).
41-
let file = File::open(filename).map_err(|_| format!("Could not open /proc file '{}'", filename))?;
41+
let file = File::open(filename).map_err(|_| format!("Could not open /proc file '{filename}'"))?;
4242
let reader = BufReader::new(file);
4343

4444
// Read the file line by line using the lines() iterator from std::io::BufRead.
@@ -50,7 +50,7 @@ pub fn procfile_field(filename: &str, field_name: &str) -> Result<String, String
5050
}
5151
}
5252

53-
Err(format!("Could not find the field named '{}' in the /proc FS file name '{}'", field_name, filename))
53+
Err(format!("Could not find the field named '{field_name}' in the /proc FS file name '{filename}'"))
5454
}
5555

5656
#[cfg(target_os = "linux")]
@@ -59,14 +59,14 @@ pub fn procfile_field(filename: &str, field_name: &str) -> Result<String, String
5959
pub fn parse_memory_string(line: &str) -> Result<u64, String> {
6060
let parts: Vec<&str> = line.trim().split(' ').collect();
6161
if parts.is_empty() {
62-
return Err(format!("Could not parse Memory String: {}", line))
62+
return Err(format!("Could not parse Memory String: {line}"))
6363
}
6464
let multiplier: u64 = if parts.len() == 2 {
6565
match parts[1] {
6666
"MB" => 1024 * 1024,
6767
"kB" => 1024,
6868
"B" => 1,
69-
_ => return Err(format!("Could not parse units of Memory String: {}", line))
69+
_ => return Err(format!("Could not parse units of Memory String: {line}"))
7070
}
7171
} else {
7272
1

src/libproc/pid_rusage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ pub fn pidrusage<T: PIDRUsage>(pid : i32) -> Result<T, String> {
406406
/// ```
407407
pub fn pidrusage<T: PIDRUsage>(pid : i32) -> Result<T, String> {
408408
let mut pidrusage = T::default();
409-
let vm_size = procfile_field(&format!("/proc/{}/status", pid), "VmSize")?;
409+
let vm_size = procfile_field(&format!("/proc/{pid}/status"), "VmSize")?;
410410
pidrusage.set_memory_used(parse_memory_string(&vm_size)?);
411411

412412
Ok(pidrusage)

src/libproc/proc_pid.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ pub fn listpids(proc_types: ProcType) -> Result<Vec<u32>, String> {
202202
ProcType::ProcAllPIDS => {
203203
let mut pids = Vec::<u32>::new();
204204

205-
let proc_dir = fs::read_dir("/proc").map_err(|e| format!("Could not read '/proc': {}", e))?;
205+
let proc_dir = fs::read_dir("/proc").map_err(|e| format!("Could not read '/proc': {e}"))?;
206206

207207
for entry in proc_dir {
208208
let path = entry.map_err(|_| "Couldn't get /proc/ filename")?.path();
@@ -289,7 +289,7 @@ pub fn listpidspath(proc_types: ProcType, path: &str) -> Result<Vec<u32>, String
289289
pub fn pidinfo<T: PIDInfo>(pid: i32, arg: u64) -> Result<T, String> {
290290
let flavor = T::flavor() as i32;
291291
let buffer_size = mem::size_of::<T>() as i32;
292-
let mut pidinfo = unsafe { std::mem::zeroed() };
292+
let mut pidinfo = unsafe { mem::zeroed() };
293293
let buffer_ptr = &mut pidinfo as *mut _ as *mut c_void;
294294
let ret: i32;
295295

@@ -405,7 +405,7 @@ pub fn pidpath(pid: i32) -> Result<String, String> {
405405
/// ```
406406
#[cfg(target_os = "linux")]
407407
pub fn pidpath(pid: i32) -> Result<String, String> {
408-
let exe_path = CString::new(format!("/proc/{}/exe", pid))
408+
let exe_path = CString::new(format!("/proc/{pid}/exe"))
409409
.map_err(|_| "Could not create CString")?;
410410
let mut buf: Vec<u8> = Vec::with_capacity(PATH_MAX as usize - 1);
411411
let buffer_ptr = buf.as_mut_ptr() as *mut c_char;
@@ -499,7 +499,7 @@ pub fn name(pid: i32) -> Result<String, String> {
499499

500500
match String::from_utf8(namebuf) {
501501
Ok(name) => Ok(name),
502-
Err(e) => Err(format!("Invalid UTF-8 sequence: {}", e))
502+
Err(e) => Err(format!("Invalid UTF-8 sequence: {e}"))
503503
}
504504
}
505505
}
@@ -508,7 +508,7 @@ pub fn name(pid: i32) -> Result<String, String> {
508508
/// Get the name of a Process using it's Pid
509509
#[cfg(target_os = "linux")]
510510
pub fn name(pid: i32) -> Result<String, String> {
511-
helpers::procfile_field(&format!("/proc/{}/status", pid), "Name")
511+
helpers::procfile_field(&format!("/proc/{pid}/status"), "Name")
512512
}
513513

514514
/// Get information on all running processes.
@@ -600,7 +600,7 @@ pub fn pidcwd(_pid: pid_t) -> Result<PathBuf, String> {
600600
/// }
601601
/// ```
602602
pub fn pidcwd(pid: pid_t) -> Result<PathBuf, String> {
603-
fs::read_link(format!("/proc/{}/cwd", pid)).map_err(|e| {
603+
fs::read_link(format!("/proc/{pid}/cwd")).map_err(|e| {
604604
e.to_string()
605605
})
606606
}

src/procinfo.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,40 +46,40 @@ fn getpid() -> i32 {
4646

4747
fn procinfo(pid: i32) {
4848
match proc_pid::libversion() {
49-
Ok((major, minor)) => println!("Libversion: {}.{}", major, minor),
50-
Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap()
49+
Ok((major, minor)) => println!("Libversion: {major}.{minor}"),
50+
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
5151
}
5252

53-
println!("Pid: {}", pid);
53+
println!("Pid: {pid}");
5454

5555
match proc_pid::pidpath(pid) {
56-
Ok(path) => println!("Path: {}", path),
57-
Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap()
56+
Ok(path) => println!("Path: {path}"),
57+
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
5858
}
5959

6060
match pidrusage::<RUsageInfoV0>(pid) {
6161
Ok(resource_usage) => println!("Memory Used: {} Bytes", resource_usage.ri_resident_size),
62-
Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap()
62+
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
6363
}
6464

6565
match proc_pid::name(pid) {
66-
Ok(name) => println!("Name: {}", name),
67-
Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap()
66+
Ok(name) => println!("Name: {name}"),
67+
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
6868
}
6969

7070
match proc_pid::regionfilename(pid, 0) {
71-
Ok(regionfilename) => println!("Region Filename (at address 0): {}", regionfilename),
72-
Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap()
71+
Ok(regionfilename) => println!("Region Filename (at address 0): {regionfilename}"),
72+
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
7373
}
7474

7575
match proc_pid::listpids(proc_pid::ProcType::ProcAllPIDS) {
7676
Ok(pids) => {
7777
println!("There are currently {} processes active", pids.len());
7878
for pid in pids {
79-
println!("{}", pid);
79+
println!("{pid}");
8080
}
8181
},
82-
Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap()
82+
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
8383
}
8484
}
8585

0 commit comments

Comments
 (0)