Skip to content

Commit f4f3385

Browse files
committed
various: fix clippy warnings
- Adds proper documentation comments with `# Errors` sections for all functions returning `Result` - `cast_precision_loss` on `u64` -> `f64` for disk sizes is acceptable since disk sizes won't exceed f64's precision limit in practice. Thus, we can suppress those. - `cast_sign_loss` and `cast_possible_truncation` on the percentage calculation is safe since percentages are always 0-100. Once again, it's safe to suppress. Signed-off-by: NotAShelf <[email protected]> Change-Id: Id4dd7ebc9674407d2be4f38ff4de24bc6a6a6964
1 parent 2ad765e commit f4f3385

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/release.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ pub fn get_system_info(utsname: &UtsName) -> String {
2121
result
2222
}
2323

24+
/// Gets the pretty name of the OS from `/etc/os-release`.
25+
///
26+
/// # Errors
27+
///
28+
/// Returns an error if `/etc/os-release` cannot be read.
2429
#[cfg_attr(feature = "hotpath", hotpath::measure)]
2530
pub fn get_os_pretty_name() -> Result<String, io::Error> {
2631
// We use a stack-allocated buffer here, which seems to perform MUCH better

src/system.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ pub fn get_shell() -> String {
4949
.to_owned()
5050
}
5151

52+
/// Gets the root disk usage information.
53+
///
54+
/// # Errors
55+
///
56+
/// Returns an error if the filesystem information cannot be retrieved.
5257
#[cfg_attr(feature = "hotpath", hotpath::measure)]
58+
#[allow(clippy::cast_precision_loss)]
5359
pub fn get_root_disk_usage() -> Result<String, io::Error> {
5460
let vfs = statvfs("/")?;
5561
let block_size = vfs.block_size() as u64;
@@ -75,6 +81,11 @@ pub fn get_root_disk_usage() -> Result<String, io::Error> {
7581
Ok(result)
7682
}
7783

84+
/// Gets the system memory usage information.
85+
///
86+
/// # Errors
87+
///
88+
/// Returns an error if `/proc/meminfo` cannot be read.
7889
#[cfg_attr(feature = "hotpath", hotpath::measure)]
7990
pub fn get_memory_usage() -> Result<String, io::Error> {
8091
#[cfg_attr(feature = "hotpath", hotpath::measure)]
@@ -109,6 +120,7 @@ pub fn get_memory_usage() -> Result<String, io::Error> {
109120
}
110121

111122
let (used_memory, total_memory) = parse_memory_info()?;
123+
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
112124
let percentage_used = (used_memory / total_memory * 100.0).round() as u64;
113125

114126
let mut result = String::with_capacity(64);

src/uptime.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
use std::{fmt::Write, io, mem::MaybeUninit};
22

3+
/// Gets the current system uptime.
4+
///
5+
/// # Errors
6+
///
7+
/// Returns an error if the system uptime cannot be retrieved.
38
#[cfg_attr(feature = "hotpath", hotpath::measure)]
49
pub fn get_current() -> Result<String, io::Error> {
510
let uptime_seconds = {
611
let mut info = MaybeUninit::uninit();
712
if unsafe { libc::sysinfo(info.as_mut_ptr()) } != 0 {
813
return Err(io::Error::last_os_error());
914
}
15+
#[allow(clippy::cast_sign_loss)]
1016
unsafe { info.assume_init().uptime as u64 }
1117
};
1218

0 commit comments

Comments
 (0)