*: appease clippy

While I'm here convert a String to a PathBuf in an error to avoid lossy
conversions.

See https://rust-lang.github.io/rust-clippy/master/index.html#io_other_error.
reviewable/pr1190/r1
Tamir Duberstein 2 weeks ago
parent f49a761c27
commit 122c49fca4

@ -1,6 +1,6 @@
use std::{ use std::{
io::{self, Write}, io::{self, Write},
process::{Command, Stdio}, process::{Command, Output, Stdio},
}; };
pub fn format(code: &str) -> Result<String, io::Error> { pub fn format(code: &str) -> Result<String, io::Error> {
@ -11,15 +11,16 @@ pub fn format(code: &str) -> Result<String, io::Error> {
let stdin = child.stdin.as_mut().unwrap(); let stdin = child.stdin.as_mut().unwrap();
stdin.write_all(code.as_bytes())?; stdin.write_all(code.as_bytes())?;
let output = child.wait_with_output()?; let Output {
if !output.status.success() { status,
return Err(io::Error::new( stdout,
io::ErrorKind::Other, stderr,
format!( } = child.wait_with_output()?;
"rustfmt failed with exit code: {}", if !status.success() {
output.status.code().unwrap() let stderr = String::from_utf8(stderr).unwrap();
), return Err(io::Error::other(format!(
)); "rustfmt failed: {status:?}\n{stderr}"
)));
} }
Ok(String::from_utf8(output.stdout).unwrap()) Ok(String::from_utf8(stdout).unwrap())
} }

@ -436,10 +436,7 @@ impl MMap {
// about a null pointer, we check it anyway. // about a null pointer, we check it anyway.
MapError::SyscallError(SyscallError { MapError::SyscallError(SyscallError {
call: "mmap", call: "mmap",
io_error: io::Error::new( io_error: io::Error::other("mmap returned null pointer"),
io::ErrorKind::Other,
"mmap returned null pointer",
),
}), }),
)?, )?,
len, len,

@ -325,12 +325,7 @@ fn read_sys_fs_perf_type(pmu: &str) -> Result<u32, (PathBuf, io::Error)> {
.join("type"); .join("type");
fs::read_to_string(&file) fs::read_to_string(&file)
.and_then(|perf_ty| { .and_then(|perf_ty| perf_ty.trim().parse::<u32>().map_err(io::Error::other))
perf_ty
.trim()
.parse::<u32>()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
})
.map_err(|e| (file, e)) .map_err(|e| (file, e))
} }
@ -344,11 +339,9 @@ fn read_sys_fs_perf_ret_probe(pmu: &str) -> Result<u32, (PathBuf, io::Error)> {
let mut parts = data.trim().splitn(2, ':').skip(1); let mut parts = data.trim().splitn(2, ':').skip(1);
let config = parts let config = parts
.next() .next()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "invalid format"))?; .ok_or_else(|| io::Error::other("invalid format"))?;
config config.parse::<u32>().map_err(io::Error::other)
.parse::<u32>()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}) })
.map_err(|e| (file, e)) .map_err(|e| (file, e))
} }

@ -1,5 +1,9 @@
//! Tracepoint programs. //! Tracepoint programs.
use std::{fs, io, os::fd::AsFd as _, path::Path}; use std::{
fs, io,
os::fd::AsFd as _,
path::{Path, PathBuf},
};
use aya_obj::generated::{bpf_link_type, bpf_prog_type::BPF_PROG_TYPE_TRACEPOINT}; use aya_obj::generated::{bpf_link_type, bpf_prog_type::BPF_PROG_TYPE_TRACEPOINT};
use thiserror::Error; use thiserror::Error;
@ -21,7 +25,7 @@ pub enum TracePointError {
#[error("`{filename}`")] #[error("`{filename}`")]
FileError { FileError {
/// The file name /// The file name
filename: String, filename: PathBuf,
/// The [`io::Error`] returned from the file operation /// The [`io::Error`] returned from the file operation
#[source] #[source]
io_error: io::Error, io_error: io::Error,
@ -122,19 +126,21 @@ pub(crate) fn read_sys_fs_trace_point_id(
category: &str, category: &str,
name: &Path, name: &Path,
) -> Result<u32, TracePointError> { ) -> Result<u32, TracePointError> {
let file = tracefs.join("events").join(category).join(name).join("id"); let filename = tracefs.join("events").join(category).join(name).join("id");
let id = fs::read_to_string(&file).map_err(|io_error| TracePointError::FileError { let id = match fs::read_to_string(&filename) {
filename: file.display().to_string(), Ok(id) => id,
io_error, Err(io_error) => return Err(TracePointError::FileError { filename, io_error }),
})?; };
let id = id let id = match id.trim().parse::<u32>() {
.trim() Ok(id) => id,
.parse::<u32>() Err(error) => {
.map_err(|error| TracePointError::FileError { return Err(TracePointError::FileError {
filename: file.display().to_string(), filename,
io_error: io::Error::new(io::ErrorKind::Other, error), io_error: io::Error::other(error),
})?; })
}
};
Ok(id) Ok(id)
} }

@ -55,7 +55,8 @@ pub(crate) fn find_tracefs_path() -> Result<&'static Path, ProgramError> {
TRACE_FS TRACE_FS
.as_deref() .as_deref()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "tracefs not found").into()) .ok_or_else(|| io::Error::other("tracefs not found"))
.map_err(Into::into)
} }
/// The time at which the system is booted. /// The time at which the system is booted.

@ -212,10 +212,9 @@ pub(crate) unsafe fn netlink_qdisc_attach(
None => { None => {
// if sock.recv() succeeds we should never get here unless there's a // if sock.recv() succeeds we should never get here unless there's a
// bug in the kernel // bug in the kernel
return Err(NetlinkError(NetlinkErrorInternal::IoError(io::Error::new( return Err(NetlinkError(NetlinkErrorInternal::IoError(
io::ErrorKind::Other, io::Error::other("no RTM_NEWTFILTER reply received, this is a bug."),
"no RTM_NEWTFILTER reply received, this is a bug.", )));
))));
} }
}; };
@ -495,28 +494,24 @@ struct NetlinkMessage {
impl NetlinkMessage { impl NetlinkMessage {
fn read(buf: &[u8]) -> Result<Self, io::Error> { fn read(buf: &[u8]) -> Result<Self, io::Error> {
if mem::size_of::<nlmsghdr>() > buf.len() { if mem::size_of::<nlmsghdr>() > buf.len() {
return Err(io::Error::new( return Err(io::Error::other("buffer smaller than nlmsghdr"));
io::ErrorKind::Other,
"buffer smaller than nlmsghdr",
));
} }
// Safety: nlmsghdr is POD so read is safe // Safety: nlmsghdr is POD so read is safe
let header = unsafe { ptr::read_unaligned(buf.as_ptr() as *const nlmsghdr) }; let header = unsafe { ptr::read_unaligned(buf.as_ptr() as *const nlmsghdr) };
let msg_len = header.nlmsg_len as usize; let msg_len = header.nlmsg_len as usize;
if msg_len < mem::size_of::<nlmsghdr>() || msg_len > buf.len() { if msg_len < mem::size_of::<nlmsghdr>() || msg_len > buf.len() {
return Err(io::Error::new(io::ErrorKind::Other, "invalid nlmsg_len")); return Err(io::Error::other("invalid nlmsg_len"));
} }
let data_offset = align_to(mem::size_of::<nlmsghdr>(), NLMSG_ALIGNTO as usize); let data_offset = align_to(mem::size_of::<nlmsghdr>(), NLMSG_ALIGNTO as usize);
if data_offset >= buf.len() { if data_offset >= buf.len() {
return Err(io::Error::new(io::ErrorKind::Other, "need more data")); return Err(io::Error::other("need more data"));
} }
let (rest, error) = if header.nlmsg_type == NLMSG_ERROR as u16 { let (rest, error) = if header.nlmsg_type == NLMSG_ERROR as u16 {
if data_offset + mem::size_of::<nlmsgerr>() > buf.len() { if data_offset + mem::size_of::<nlmsgerr>() > buf.len() {
return Err(io::Error::new( return Err(io::Error::other(
io::ErrorKind::Other,
"NLMSG_ERROR but not enough space for nlmsgerr", "NLMSG_ERROR but not enough space for nlmsgerr",
)); ));
} }
@ -625,7 +620,7 @@ fn write_attr_header(buf: &mut [u8], offset: usize, attr: nlattr) -> Result<usiz
fn write_bytes(buf: &mut [u8], offset: usize, value: &[u8]) -> Result<usize, io::Error> { fn write_bytes(buf: &mut [u8], offset: usize, value: &[u8]) -> Result<usize, io::Error> {
let align_len = align_to(value.len(), NLA_ALIGNTO as usize); let align_len = align_to(value.len(), NLA_ALIGNTO as usize);
if offset + align_len > buf.len() { if offset + align_len > buf.len() {
return Err(io::Error::new(io::ErrorKind::Other, "no space left")); return Err(io::Error::other("no space left"));
} }
buf[offset..offset + value.len()].copy_from_slice(value); buf[offset..offset + value.len()].copy_from_slice(value);
@ -706,8 +701,8 @@ pub(crate) enum NlAttrError {
} }
impl From<NlAttrError> for io::Error { impl From<NlAttrError> for io::Error {
fn from(e: NlAttrError) -> Self { fn from(err: NlAttrError) -> Self {
Self::new(io::ErrorKind::Other, e) Self::other(err)
} }
} }

@ -6416,7 +6416,7 @@ pub fn aya::programs::tp_btf::BtfTracePointLinkId::from(t: T) -> T
pub mod aya::programs::trace_point pub mod aya::programs::trace_point
pub enum aya::programs::trace_point::TracePointError pub enum aya::programs::trace_point::TracePointError
pub aya::programs::trace_point::TracePointError::FileError pub aya::programs::trace_point::TracePointError::FileError
pub aya::programs::trace_point::TracePointError::FileError::filename: alloc::string::String pub aya::programs::trace_point::TracePointError::FileError::filename: std::path::PathBuf
pub aya::programs::trace_point::TracePointError::FileError::io_error: std::io::error::Error pub aya::programs::trace_point::TracePointError::FileError::io_error: std::io::error::Error
impl core::convert::From<aya::programs::trace_point::TracePointError> for aya::programs::ProgramError impl core::convert::From<aya::programs::trace_point::TracePointError> for aya::programs::ProgramError
pub fn aya::programs::ProgramError::from(source: aya::programs::trace_point::TracePointError) -> Self pub fn aya::programs::ProgramError::from(source: aya::programs::trace_point::TracePointError) -> Self
@ -7887,7 +7887,7 @@ impl<T> core::convert::From<T> for aya::programs::tc::TcError
pub fn aya::programs::tc::TcError::from(t: T) -> T pub fn aya::programs::tc::TcError::from(t: T) -> T
pub enum aya::programs::TracePointError pub enum aya::programs::TracePointError
pub aya::programs::TracePointError::FileError pub aya::programs::TracePointError::FileError
pub aya::programs::TracePointError::FileError::filename: alloc::string::String pub aya::programs::TracePointError::FileError::filename: std::path::PathBuf
pub aya::programs::TracePointError::FileError::io_error: std::io::error::Error pub aya::programs::TracePointError::FileError::io_error: std::io::error::Error
impl core::convert::From<aya::programs::trace_point::TracePointError> for aya::programs::ProgramError impl core::convert::From<aya::programs::trace_point::TracePointError> for aya::programs::ProgramError
pub fn aya::programs::ProgramError::from(source: aya::programs::trace_point::TracePointError) -> Self pub fn aya::programs::ProgramError::from(source: aya::programs::trace_point::TracePointError) -> Self

Loading…
Cancel
Save