From c6487dad5e8a45a94202250f928cf97fb9400870 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Fri, 27 Dec 2024 08:04:54 -0500 Subject: [PATCH 1/2] Simplify `parse_param` --- aya-log-parser/src/lib.rs | 46 +++++++++++++++------------------------ 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/aya-log-parser/src/lib.rs b/aya-log-parser/src/lib.rs index 1bdf2abed..82217a780 100644 --- a/aya-log-parser/src/lib.rs +++ b/aya-log-parser/src/lib.rs @@ -55,39 +55,27 @@ fn push_literal(frag: &mut Vec, unescaped_literal: &str) -> Result<(), Ok(()) } -/// Parses the display hint (e.g. the `ipv4` in `{:ipv4}`). -fn parse_display_hint(s: &str) -> Result { - Ok(match s { - "p" | "x" => DisplayHint::LowerHex, - "X" => DisplayHint::UpperHex, - "i" => DisplayHint::Ip, - "mac" => DisplayHint::LowerMac, - "MAC" => DisplayHint::UpperMac, - _ => return Err(format!("unknown display hint: {s:?}")), - }) -} - /// Parse `Param` from the given `&str` which can specify an optional format /// like `:x` or `:ipv4` (without curly braces, which are parsed by the `parse` /// function). -fn parse_param(mut input: &str) -> Result { - const HINT_PREFIX: &str = ":"; - - // Then, optional hint - let mut hint = DisplayHint::Default; - - if input.starts_with(HINT_PREFIX) { - // skip the prefix - input = &input[HINT_PREFIX.len()..]; - if input.is_empty() { - return Err("malformed format string (missing display hint after ':')".into()); +fn parse_param(input: &str) -> Result { + let hint = match input.strip_prefix(":") { + Some(input) => match input { + "" => return Err("malformed format string (missing display hint after ':')".into()), + "p" | "x" => DisplayHint::LowerHex, + "X" => DisplayHint::UpperHex, + "i" => DisplayHint::Ip, + "mac" => DisplayHint::LowerMac, + "MAC" => DisplayHint::UpperMac, + input => return Err(format!("unknown display hint: {input:?}")), + }, + None => { + if !input.is_empty() { + return Err(format!("unexpected content {input:?} in format string")); + } + DisplayHint::Default } - - hint = parse_display_hint(input)?; - } else if !input.is_empty() { - return Err(format!("unexpected content {input:?} in format string")); - } - + }; Ok(Parameter { hint }) } From 9ec2d312c879301fdad86122126fcf76f156a4c7 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Fri, 27 Dec 2024 05:50:55 -0500 Subject: [PATCH 2/2] Narrow clippy allowances --- aya-log-parser/src/lib.rs | 6 +++--- aya/src/sys/mod.rs | 12 +++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/aya-log-parser/src/lib.rs b/aya-log-parser/src/lib.rs index 82217a780..1e760333b 100644 --- a/aya-log-parser/src/lib.rs +++ b/aya-log-parser/src/lib.rs @@ -1,6 +1,3 @@ -// We implement our own formatter here and we pass literal strings on purpose. -#![allow(clippy::literal_string_with_formatting_args)] - use std::str; use aya_log_common::DisplayHint; @@ -133,6 +130,9 @@ pub fn parse(format_string: &str) -> Result, String> { mod test { use super::*; + // TODO(https://github.com/rust-lang/rust-clippy/issues/13885): narrow this to just the specific + // strings when that doesn't trip the lint. + #[allow(clippy::literal_string_with_formatting_args)] #[test] fn test_parse() { assert_eq!( diff --git a/aya/src/sys/mod.rs b/aya/src/sys/mod.rs index f7b2cc25e..86f197abc 100644 --- a/aya/src/sys/mod.rs +++ b/aya/src/sys/mod.rs @@ -93,10 +93,6 @@ fn syscall(call: Syscall<'_>) -> SysResult { #[cfg(test)] return TEST_SYSCALL.with(|test_impl| unsafe { test_impl.borrow()(call) }); - // The type of integer taken by `ioctl` is different in glibc (i64) and - // musl (i32). musl builds would complain about useless conversion. - // `libc::ioctl` returns i32 on x86_64 while `libc::syscall` returns i64. - #[allow(clippy::useless_conversion)] #[cfg_attr(test, allow(unreachable_code))] { let ret = unsafe { @@ -112,7 +108,13 @@ fn syscall(call: Syscall<'_>) -> SysResult { flags, } => libc::syscall(SYS_perf_event_open, &attr, pid, cpu, group, flags), Syscall::PerfEventIoctl { fd, request, arg } => { - let ret = libc::ioctl(fd.as_raw_fd(), request.try_into().unwrap(), arg); + // The type of integer taken by `ioctl` is different in glibc (i64) and + // musl (i32). musl builds would complain about useless conversion. + #[allow(clippy::useless_conversion)] + let request = request.try_into().unwrap(); + let ret = libc::ioctl(fd.as_raw_fd(), request, arg); + // `libc::ioctl` returns i32 on x86_64 while `libc::syscall` returns i64. + #[allow(clippy::useless_conversion)] ret.into() } }