Browse Source

*: avoid `_`

This can silently discard information, so we shouldn't do it.
Tamir Duberstein 1 month ago
parent
commit
bdd8ae2d0b

+ 105 - 37
aya-log/src/lib.rs

@@ -468,12 +468,12 @@ fn log_buf(mut buf: &[u8], logger: &dyn Log) -> Result<(), ()> {
     let mut line = None;
     let mut num_args = None;
 
-    for _ in 0..LOG_FIELDS {
+    for () in std::iter::repeat_n((), LOG_FIELDS) {
         let (RecordFieldWrapper(tag), value, rest) = try_read(buf)?;
 
         match tag {
             RecordField::Target => {
-                target = Some(str::from_utf8(value).map_err(|_| ())?);
+                target = Some(str::from_utf8(value).map_err(|std::str::Utf8Error { .. }| ())?);
             }
             RecordField::Level => {
                 level = Some({
@@ -488,16 +488,24 @@ fn log_buf(mut buf: &[u8], logger: &dyn Log) -> Result<(), ()> {
                 })
             }
             RecordField::Module => {
-                module = Some(str::from_utf8(value).map_err(|_| ())?);
+                module = Some(str::from_utf8(value).map_err(|std::str::Utf8Error { .. }| ())?);
             }
             RecordField::File => {
-                file = Some(str::from_utf8(value).map_err(|_| ())?);
+                file = Some(str::from_utf8(value).map_err(|std::str::Utf8Error { .. }| ())?);
             }
             RecordField::Line => {
-                line = Some(u32::from_ne_bytes(value.try_into().map_err(|_| ())?));
+                line = Some(u32::from_ne_bytes(
+                    value
+                        .try_into()
+                        .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                ));
             }
             RecordField::NumArgs => {
-                num_args = Some(usize::from_ne_bytes(value.try_into().map_err(|_| ())?));
+                num_args = Some(usize::from_ne_bytes(
+                    value
+                        .try_into()
+                        .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                ));
             }
         }
 
@@ -506,7 +514,7 @@ fn log_buf(mut buf: &[u8], logger: &dyn Log) -> Result<(), ()> {
 
     let mut full_log_msg = String::new();
     let mut last_hint: Option<DisplayHintWrapper> = None;
-    for _ in 0..num_args.ok_or(())? {
+    for () in std::iter::repeat_n((), num_args.ok_or(())?) {
         let (ArgumentWrapper(tag), value, rest) = try_read(buf)?;
 
         match tag {
@@ -515,100 +523,160 @@ fn log_buf(mut buf: &[u8], logger: &dyn Log) -> Result<(), ()> {
             }
             Argument::I8 => {
                 full_log_msg.push_str(
-                    &i8::from_ne_bytes(value.try_into().map_err(|_| ())?)
-                        .format(last_hint.take())?,
+                    &i8::from_ne_bytes(
+                        value
+                            .try_into()
+                            .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                    )
+                    .format(last_hint.take())?,
                 );
             }
             Argument::I16 => {
                 full_log_msg.push_str(
-                    &i16::from_ne_bytes(value.try_into().map_err(|_| ())?)
-                        .format(last_hint.take())?,
+                    &i16::from_ne_bytes(
+                        value
+                            .try_into()
+                            .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                    )
+                    .format(last_hint.take())?,
                 );
             }
             Argument::I32 => {
                 full_log_msg.push_str(
-                    &i32::from_ne_bytes(value.try_into().map_err(|_| ())?)
-                        .format(last_hint.take())?,
+                    &i32::from_ne_bytes(
+                        value
+                            .try_into()
+                            .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                    )
+                    .format(last_hint.take())?,
                 );
             }
             Argument::I64 => {
                 full_log_msg.push_str(
-                    &i64::from_ne_bytes(value.try_into().map_err(|_| ())?)
-                        .format(last_hint.take())?,
+                    &i64::from_ne_bytes(
+                        value
+                            .try_into()
+                            .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                    )
+                    .format(last_hint.take())?,
                 );
             }
             Argument::Isize => {
                 full_log_msg.push_str(
-                    &isize::from_ne_bytes(value.try_into().map_err(|_| ())?)
-                        .format(last_hint.take())?,
+                    &isize::from_ne_bytes(
+                        value
+                            .try_into()
+                            .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                    )
+                    .format(last_hint.take())?,
                 );
             }
             Argument::U8 => {
                 full_log_msg.push_str(
-                    &u8::from_ne_bytes(value.try_into().map_err(|_| ())?)
-                        .format(last_hint.take())?,
+                    &u8::from_ne_bytes(
+                        value
+                            .try_into()
+                            .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                    )
+                    .format(last_hint.take())?,
                 );
             }
             Argument::U16 => {
                 full_log_msg.push_str(
-                    &u16::from_ne_bytes(value.try_into().map_err(|_| ())?)
-                        .format(last_hint.take())?,
+                    &u16::from_ne_bytes(
+                        value
+                            .try_into()
+                            .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                    )
+                    .format(last_hint.take())?,
                 );
             }
             Argument::U32 => {
                 full_log_msg.push_str(
-                    &u32::from_ne_bytes(value.try_into().map_err(|_| ())?)
-                        .format(last_hint.take())?,
+                    &u32::from_ne_bytes(
+                        value
+                            .try_into()
+                            .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                    )
+                    .format(last_hint.take())?,
                 );
             }
             Argument::U64 => {
                 full_log_msg.push_str(
-                    &u64::from_ne_bytes(value.try_into().map_err(|_| ())?)
-                        .format(last_hint.take())?,
+                    &u64::from_ne_bytes(
+                        value
+                            .try_into()
+                            .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                    )
+                    .format(last_hint.take())?,
                 );
             }
             Argument::Usize => {
                 full_log_msg.push_str(
-                    &usize::from_ne_bytes(value.try_into().map_err(|_| ())?)
-                        .format(last_hint.take())?,
+                    &usize::from_ne_bytes(
+                        value
+                            .try_into()
+                            .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                    )
+                    .format(last_hint.take())?,
                 );
             }
             Argument::F32 => {
                 full_log_msg.push_str(
-                    &f32::from_ne_bytes(value.try_into().map_err(|_| ())?)
-                        .format(last_hint.take())?,
+                    &f32::from_ne_bytes(
+                        value
+                            .try_into()
+                            .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                    )
+                    .format(last_hint.take())?,
                 );
             }
             Argument::F64 => {
                 full_log_msg.push_str(
-                    &f64::from_ne_bytes(value.try_into().map_err(|_| ())?)
-                        .format(last_hint.take())?,
+                    &f64::from_ne_bytes(
+                        value
+                            .try_into()
+                            .map_err(|std::array::TryFromSliceError { .. }| ())?,
+                    )
+                    .format(last_hint.take())?,
                 );
             }
             Argument::Ipv4Addr => {
-                let value: [u8; 4] = value.try_into().map_err(|_| ())?;
+                let value: [u8; 4] = value
+                    .try_into()
+                    .map_err(|std::array::TryFromSliceError { .. }| ())?;
                 let value = Ipv4Addr::from(value);
                 full_log_msg.push_str(&value.format(last_hint.take())?)
             }
             Argument::Ipv6Addr => {
-                let value: [u8; 16] = value.try_into().map_err(|_| ())?;
+                let value: [u8; 16] = value
+                    .try_into()
+                    .map_err(|std::array::TryFromSliceError { .. }| ())?;
                 let value = Ipv6Addr::from(value);
                 full_log_msg.push_str(&value.format(last_hint.take())?)
             }
             Argument::ArrU8Len4 => {
-                let value: [u8; 4] = value.try_into().map_err(|_| ())?;
+                let value: [u8; 4] = value
+                    .try_into()
+                    .map_err(|std::array::TryFromSliceError { .. }| ())?;
                 full_log_msg.push_str(&value.format(last_hint.take())?);
             }
             Argument::ArrU8Len6 => {
-                let value: [u8; 6] = value.try_into().map_err(|_| ())?;
+                let value: [u8; 6] = value
+                    .try_into()
+                    .map_err(|std::array::TryFromSliceError { .. }| ())?;
                 full_log_msg.push_str(&value.format(last_hint.take())?);
             }
             Argument::ArrU8Len16 => {
-                let value: [u8; 16] = value.try_into().map_err(|_| ())?;
+                let value: [u8; 16] = value
+                    .try_into()
+                    .map_err(|std::array::TryFromSliceError { .. }| ())?;
                 full_log_msg.push_str(&value.format(last_hint.take())?);
             }
             Argument::ArrU16Len8 => {
-                let data: [u8; 16] = value.try_into().map_err(|_| ())?;
+                let data: [u8; 16] = value
+                    .try_into()
+                    .map_err(|std::array::TryFromSliceError { .. }| ())?;
                 let mut value: [u16; 8] = Default::default();
                 for (i, s) in data.chunks_exact(2).enumerate() {
                     value[i] = ((s[1] as u16) << 8) | s[0] as u16;

+ 3 - 3
aya-obj/src/btf/btf.rs

@@ -23,7 +23,7 @@ use crate::{
     Object,
 };
 
-pub(crate) const MAX_RESOLVE_DEPTH: u8 = 32;
+pub(crate) const MAX_RESOLVE_DEPTH: usize = 32;
 pub(crate) const MAX_SPEC_LEN: usize = 64;
 
 /// The error type returned when `BTF` operations fail.
@@ -434,7 +434,7 @@ impl Btf {
     pub(crate) fn type_size(&self, root_type_id: u32) -> Result<usize, BtfError> {
         let mut type_id = root_type_id;
         let mut n_elems = 1;
-        for _ in 0..MAX_RESOLVE_DEPTH {
+        for () in core::iter::repeat_n((), MAX_RESOLVE_DEPTH) {
             let ty = self.types.type_by_id(type_id)?;
             let size = match ty {
                 BtfType::Array(Array { array, .. }) => {
@@ -1054,7 +1054,7 @@ impl BtfTypes {
 
     pub(crate) fn resolve_type(&self, root_type_id: u32) -> Result<u32, BtfError> {
         let mut type_id = root_type_id;
-        for _ in 0..MAX_RESOLVE_DEPTH {
+        for () in core::iter::repeat_n((), MAX_RESOLVE_DEPTH) {
             let ty = self.type_by_id(type_id)?;
 
             use BtfType::*;

+ 1 - 1
aya-obj/src/btf/relocation.rs

@@ -246,7 +246,7 @@ impl Object {
                 target_btf,
                 &mut candidates_cache,
             ) {
-                Ok(_) => {}
+                Ok(()) => {}
                 Err(error) => {
                     return Err(BtfRelocationError {
                         section: section_name.to_string(),

+ 2 - 2
aya-obj/src/btf/types.rs

@@ -1451,7 +1451,7 @@ pub(crate) fn types_are_compatible(
         return Ok(false);
     }
 
-    for _ in 0..MAX_RESOLVE_DEPTH {
+    for () in core::iter::repeat_n((), MAX_RESOLVE_DEPTH) {
         local_id = local_btf.resolve_type(local_id)?;
         target_id = target_btf.resolve_type(target_id)?;
         let local_ty = local_btf.type_by_id(local_id)?;
@@ -1520,7 +1520,7 @@ pub(crate) fn fields_are_compatible(
     target_btf: &Btf,
     mut target_id: u32,
 ) -> Result<bool, BtfError> {
-    for _ in 0..MAX_RESOLVE_DEPTH {
+    for () in core::iter::repeat_n((), MAX_RESOLVE_DEPTH) {
         local_id = local_btf.resolve_type(local_id)?;
         target_id = target_btf.resolve_type(target_id)?;
         let local_ty = local_btf.type_by_id(local_id)?;

+ 2 - 1
aya/src/maps/mod.rs

@@ -558,7 +558,8 @@ impl MapData {
         name: &str,
         btf_fd: Option<BorrowedFd<'_>>,
     ) -> Result<Self, MapError> {
-        let c_name = CString::new(name).map_err(|_| MapError::InvalidName { name: name.into() })?;
+        let c_name = CString::new(name)
+            .map_err(|std::ffi::NulError { .. }| MapError::InvalidName { name: name.into() })?;
 
         // BPF_MAP_TYPE_PERF_EVENT_ARRAY's max_entries should not exceed the number of
         // CPUs.

+ 1 - 2
aya/src/maps/perf/async_perf_event_array.rs

@@ -63,8 +63,7 @@ use crate::maps::{
 ///
 ///     // process each perf buffer in a separate task
 ///     task::spawn(async move {
-///         let mut buffers = (0..10)
-///             .map(|_| BytesMut::with_capacity(1024))
+///         let mut buffers = std::iter::repeat_n(BytesMut::with_capacity(1024), 10)
 ///             .collect::<Vec<_>>();
 ///
 ///         loop {

+ 1 - 3
aya/src/maps/perf/perf_buffer.rs

@@ -473,9 +473,7 @@ mod tests {
         fake_mmap(&mut mmapped_buf);
         let mut buf = PerfBuffer::open(1, PAGE_SIZE, 1).unwrap();
 
-        let mut out_bufs = (0..3)
-            .map(|_| BytesMut::with_capacity(4))
-            .collect::<Vec<_>>();
+        let mut out_bufs = std::iter::repeat_n(BytesMut::with_capacity(4), 3).collect::<Vec<_>>();
 
         let events = buf.read_events(&mut out_bufs).unwrap();
         assert_eq!(events, Events { lost: 0, read: 2 });

+ 1 - 1
aya/src/maps/stack_trace.rs

@@ -202,5 +202,5 @@ fn sysctl<T: FromStr>(key: &str) -> Result<T, io::Error> {
     let val = fs::read_to_string(Path::new("/proc/sys").join(key))?;
     val.trim()
         .parse::<T>()
-        .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, val))
+        .map_err(|_: T::Err| io::Error::new(io::ErrorKind::InvalidData, val))
 }

+ 4 - 3
aya/src/programs/mod.rs

@@ -667,8 +667,9 @@ fn load_program<T: Link>(
         if name.len() > 15 {
             name.truncate(15);
         }
-        let prog_name = CString::new(name.clone())
-            .map_err(|_| ProgramError::InvalidName { name: name.clone() })?;
+        let prog_name = CString::new(name.clone()).map_err(|std::ffi::NulError { .. }| {
+            ProgramError::InvalidName { name: name.clone() }
+        })?;
         Some(prog_name)
     } else {
         None
@@ -730,7 +731,7 @@ pub(crate) fn query(
             &mut prog_cnt,
             &mut revision,
         ) {
-            Ok(_) => {
+            Ok(()) => {
                 prog_ids.resize(prog_cnt as usize, 0);
                 return Ok((revision, prog_ids));
             }

+ 2 - 2
aya/src/programs/uprobe.rs

@@ -344,7 +344,7 @@ impl LdSoCache {
         // Check for new format
         let mut buf = [0u8; LD_SO_CACHE_HEADER_NEW.len()];
         cursor.read_exact(&mut buf)?;
-        let header = std::str::from_utf8(&buf).map_err(|_| {
+        let header = std::str::from_utf8(&buf).map_err(|std::str::Utf8Error { .. }| {
             io::Error::new(io::ErrorKind::InvalidData, "invalid ld.so.cache header")
         })?;
 
@@ -355,7 +355,7 @@ impl LdSoCache {
             cursor.set_position(0);
             let mut buf = [0u8; LD_SO_CACHE_HEADER_OLD.len()];
             cursor.read_exact(&mut buf)?;
-            let header = std::str::from_utf8(&buf).map_err(|_| {
+            let header = std::str::from_utf8(&buf).map_err(|std::str::Utf8Error { .. }| {
                 io::Error::new(io::ErrorKind::InvalidData, "invalid ld.so.cache header")
             })?;
 

+ 3 - 3
aya/src/sys/bpf.rs

@@ -220,7 +220,7 @@ fn lookup<K: Pod, V: Pod>(
     u.flags = flags;
 
     match unit_sys_bpf(cmd, &mut attr) {
-        Ok(_) => Ok(Some(unsafe { value.assume_init() })),
+        Ok(()) => Ok(Some(unsafe { value.assume_init() })),
         Err(io_error) if io_error.raw_os_error() == Some(ENOENT) => Ok(None),
         Err(e) => Err(e),
     }
@@ -272,7 +272,7 @@ pub(crate) fn bpf_map_lookup_elem_ptr<K: Pod, V>(
     u.flags = flags;
 
     match unit_sys_bpf(bpf_cmd::BPF_MAP_LOOKUP_ELEM, &mut attr) {
-        Ok(_) => Ok(Some(())),
+        Ok(()) => Ok(Some(())),
         Err(io_error) if io_error.raw_os_error() == Some(ENOENT) => Ok(None),
         Err(e) => Err(e),
     }
@@ -364,7 +364,7 @@ pub(crate) fn bpf_map_get_next_key<K: Pod>(
     u.__bindgen_anon_1.next_key = &mut next_key as *mut _ as u64;
 
     match unit_sys_bpf(bpf_cmd::BPF_MAP_GET_NEXT_KEY, &mut attr) {
-        Ok(_) => Ok(Some(unsafe { next_key.assume_init() })),
+        Ok(()) => Ok(Some(unsafe { next_key.assume_init() })),
         Err(io_error) if io_error.raw_os_error() == Some(ENOENT) => Ok(None),
         Err(e) => Err(e),
     }

+ 1 - 1
aya/src/sys/perf_event.rs

@@ -127,7 +127,7 @@ fn perf_event_sys(
         flags,
     })?;
 
-    let fd = fd.try_into().map_err(|_| {
+    let fd = fd.try_into().map_err(|std::num::TryFromIntError { .. }| {
         (
             fd,
             io::Error::new(

+ 1 - 1
ebpf/aya-ebpf/src/programs/sk_buff.rs

@@ -419,7 +419,7 @@ impl SkBuffContext {
     /// fn try_cgroup_skb(ctx: SkBuffContext) -> Result<i32, i32> {
     ///     let len = ETH_HLEN + IP_HLEN + UDP_HLEN;
     ///     match ctx.pull_data(len as u32) {
-    ///         Ok(_) => return Ok(0),
+    ///         Ok(()) => return Ok(0),
     ///         Err(ret) => return Err(ret as i32),
     ///     }
     /// }

+ 1 - 1
ebpf/aya-ebpf/src/programs/tc.rs

@@ -176,7 +176,7 @@ impl TcContext {
     /// fn try_classifier(ctx: TcContext) -> Result<i32, i32> {
     ///     let len = ETH_HLEN + IP_HLEN + UDP_HLEN;
     ///     match ctx.pull_data(len as u32) {
-    ///         Ok(_) => return Ok(0),
+    ///         Ok(()) => return Ok(0),
     ///         Err(ret) => return Err(ret as i32),
     ///     }
     /// }

+ 1 - 1
test/integration-test/src/tests/info.rs

@@ -143,7 +143,7 @@ fn test_loaded_at() {
     // timestamp to continuously jump around, so we add some retries. If the test is ever correct,
     // we know that the value returned by loaded_at() was reasonable relative to SystemTime::now().
     let mut failures = Vec::new();
-    for _ in 0..5 {
+    for () in std::iter::repeat_n((), 5) {
         let t1 = SystemTime::now();
         prog.load().unwrap();