*: 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::{
io::{self, Write},
process::{Command, Stdio},
process::{Command, Output, Stdio},
};
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();
stdin.write_all(code.as_bytes())?;
let output = child.wait_with_output()?;
if !output.status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"rustfmt failed with exit code: {}",
output.status.code().unwrap()
),
));
let Output {
status,
stdout,
stderr,
} = child.wait_with_output()?;
if !status.success() {
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.
MapError::SyscallError(SyscallError {
call: "mmap",
io_error: io::Error::new(
io::ErrorKind::Other,
"mmap returned null pointer",
),
io_error: io::Error::other("mmap returned null pointer"),
}),
)?,
len,

@ -325,12 +325,7 @@ fn read_sys_fs_perf_type(pmu: &str) -> Result<u32, (PathBuf, io::Error)> {
.join("type");
fs::read_to_string(&file)
.and_then(|perf_ty| {
perf_ty
.trim()
.parse::<u32>()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
})
.and_then(|perf_ty| perf_ty.trim().parse::<u32>().map_err(io::Error::other))
.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 config = parts
.next()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "invalid format"))?;
.ok_or_else(|| io::Error::other("invalid format"))?;
config
.parse::<u32>()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
config.parse::<u32>().map_err(io::Error::other)
})
.map_err(|e| (file, e))
}

@ -1,5 +1,9 @@
//! 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 thiserror::Error;
@ -21,7 +25,7 @@ pub enum TracePointError {
#[error("`{filename}`")]
FileError {
/// The file name
filename: String,
filename: PathBuf,
/// The [`io::Error`] returned from the file operation
#[source]
io_error: io::Error,
@ -122,19 +126,21 @@ pub(crate) fn read_sys_fs_trace_point_id(
category: &str,
name: &Path,
) -> 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 {
filename: file.display().to_string(),
io_error,
})?;
let id = id
.trim()
.parse::<u32>()
.map_err(|error| TracePointError::FileError {
filename: file.display().to_string(),
io_error: io::Error::new(io::ErrorKind::Other, error),
})?;
let id = match fs::read_to_string(&filename) {
Ok(id) => id,
Err(io_error) => return Err(TracePointError::FileError { filename, io_error }),
};
let id = match id.trim().parse::<u32>() {
Ok(id) => id,
Err(error) => {
return Err(TracePointError::FileError {
filename,
io_error: io::Error::other(error),
})
}
};
Ok(id)
}

@ -55,7 +55,8 @@ pub(crate) fn find_tracefs_path() -> Result<&'static Path, ProgramError> {
TRACE_FS
.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.

@ -212,10 +212,9 @@ pub(crate) unsafe fn netlink_qdisc_attach(
None => {
// if sock.recv() succeeds we should never get here unless there's a
// bug in the kernel
return Err(NetlinkError(NetlinkErrorInternal::IoError(io::Error::new(
io::ErrorKind::Other,
"no RTM_NEWTFILTER reply received, this is a bug.",
))));
return Err(NetlinkError(NetlinkErrorInternal::IoError(
io::Error::other("no RTM_NEWTFILTER reply received, this is a bug."),
)));
}
};
@ -495,28 +494,24 @@ struct NetlinkMessage {
impl NetlinkMessage {
fn read(buf: &[u8]) -> Result<Self, io::Error> {
if mem::size_of::<nlmsghdr>() > buf.len() {
return Err(io::Error::new(
io::ErrorKind::Other,
"buffer smaller than nlmsghdr",
));
return Err(io::Error::other("buffer smaller than nlmsghdr"));
}
// Safety: nlmsghdr is POD so read is safe
let header = unsafe { ptr::read_unaligned(buf.as_ptr() as *const nlmsghdr) };
let msg_len = header.nlmsg_len as usize;
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);
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 {
if data_offset + mem::size_of::<nlmsgerr>() > buf.len() {
return Err(io::Error::new(
io::ErrorKind::Other,
return Err(io::Error::other(
"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> {
let align_len = align_to(value.len(), NLA_ALIGNTO as usize);
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);
@ -706,8 +701,8 @@ pub(crate) enum NlAttrError {
}
impl From<NlAttrError> for io::Error {
fn from(e: NlAttrError) -> Self {
Self::new(io::ErrorKind::Other, e)
fn from(err: NlAttrError) -> Self {
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 enum aya::programs::trace_point::TracePointError
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
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
@ -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 enum aya::programs::TracePointError
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
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

Loading…
Cancel
Save