various clippy lint fixes

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-07-16 13:44:42 +02:00
parent 43a25b7713
commit def1d54aa6
10 changed files with 79 additions and 23 deletions

View File

@ -200,7 +200,7 @@ impl Schema {
}
pub fn add_default_property(&mut self, key: &str, value: syn::Expr) {
if !self.find_schema_property(key).is_some() {
if self.find_schema_property(key).is_none() {
self.properties.push((Ident::new(key, Span::call_site()), value));
}
}

View File

@ -212,10 +212,8 @@ fn handle_function_signature(
// try to infer the type in the schema if it is not specified explicitly:
let is_option = util::infer_type(schema, &*pat_type.ty)?;
let has_default = schema.find_schema_property("default").is_some();
if !is_option && *optional {
if !has_default {
bail!(pat_type => "optional types need a default or be an Option<T>");
}
if !is_option && *optional && !has_default {
bail!(pat_type => "optional types need a default or be an Option<T>");
}
if has_default && !*optional {
bail!(pat_type => "non-optional parameter cannot have a default");

View File

@ -94,23 +94,21 @@ fn handle_newtype_struct(
// create "linked" schemas. We cannot do this purely via the macro.
let mut schema: Schema = attribs.try_into()?;
match schema.item {
SchemaItem::Inferred(_span) => {
// The schema has no `type` and we failed to guess it. Infer it from the contained
// field!
if let SchemaItem::Inferred(_span) = schema.item {
// The schema has no `type` and we failed to guess it. Infer it from the contained field!
let fields = match &stru.fields {
syn::Fields::Unnamed(fields) => &fields.unnamed,
_ => panic!("handle_unit_struct on non-unit struct"), // `handle_struct()` verified this!
};
// this is also part of `handle_struct()`'s verification!
assert_eq!(fields.len(), 1, "handle_unit_struct needs a struct with exactly 1 field");
let fields = match &stru.fields {
syn::Fields::Unnamed(fields) => &fields.unnamed,
// Now infer the type information:
util::infer_type(&mut schema, &fields[0].ty)?;
}
_ => (),
};
// `handle_struct()` verified this!
_ => panic!("handle_unit_struct on non-unit struct"),
};
// this is also part of `handle_struct()`'s verification!
assert_eq!(fields.len(), 1, "handle_unit_struct needs a struct with exactly 1 field");
// Now infer the type information:
util::infer_type(&mut schema, &fields[0].ty)?;
}
get_struct_description(&mut schema, &stru)?;

View File

@ -105,6 +105,11 @@ pub trait ReadExt {
/// # }
/// ```
///
/// # Safety
///
/// This should only used for types with a defined storage representation, usually
/// `#[repr(C)]`, otherwise the results may be inconsistent.
///
/// [`Endian`]: https://docs.rs/endian_trait/0.6/endian_trait/trait.Endian.html
unsafe fn read_host_value<T: Endian>(&mut self) -> io::Result<T>;
@ -137,6 +142,11 @@ pub trait ReadExt {
/// # }
/// ```
///
/// # Safety
///
/// This should only used for types with a defined storage representation, usually
/// `#[repr(C)]`, otherwise the results may be inconsistent.
///
/// [`Endian`]: https://docs.rs/endian_trait/0.6/endian_trait/trait.Endian.html
unsafe fn read_le_value<T: Endian>(&mut self) -> io::Result<T>;
@ -169,6 +179,11 @@ pub trait ReadExt {
/// # }
/// ```
///
/// # Safety
///
/// This should only used for types with a defined storage representation, usually
/// `#[repr(C)]`, otherwise the results may be inconsistent.
///
/// [`Endian`]: https://docs.rs/endian_trait/0.6/endian_trait/trait.Endian.html
unsafe fn read_be_value<T: Endian>(&mut self) -> io::Result<T>;
@ -201,6 +216,11 @@ pub trait ReadExt {
/// # }
/// # code(&input[..]).unwrap();
/// ```
///
/// # Safety
///
/// This should only used for types with a defined storage representation, usually
/// `#[repr(C)]`, otherwise the results may be inconsistent.
unsafe fn read_host_value_boxed<T>(&mut self) -> io::Result<Box<T>>;
}

View File

@ -70,6 +70,11 @@ pub trait WriteExt {
/// # }
/// ```
///
/// # Safety
///
/// This should only used for types with a defined storage representation, usually
/// `#[repr(C)]`, otherwise the results may be inconsistent.
///
/// [`Endian`]: https://docs.rs/endian_trait/0.6/endian_trait/trait.Endian.html
unsafe fn write_host_value<T: Endian>(&mut self, value: T) -> io::Result<()>;
@ -108,6 +113,11 @@ pub trait WriteExt {
/// # }
/// ```
///
/// # Safety
///
/// This should only used for types with a defined storage representation, usually
/// `#[repr(C)]`, otherwise the results may be inconsistent.
///
/// [`Endian`]: https://docs.rs/endian_trait/0.6/endian_trait/trait.Endian.html
unsafe fn write_le_value<T: Endian>(&mut self, value: T) -> io::Result<()>;
@ -146,6 +156,11 @@ pub trait WriteExt {
/// # }
/// ```
///
/// # Safety
///
/// This should only used for types with a defined storage representation, usually
/// `#[repr(C)]`, otherwise the results may be inconsistent.
///
/// [`Endian`]: https://docs.rs/endian_trait/0.6/endian_trait/trait.Endian.html
unsafe fn write_be_value<T: Endian>(&mut self, value: T) -> io::Result<()>;
}

View File

@ -17,6 +17,11 @@ unsafe impl<T> Send for Mmap<T> where T: Send {}
unsafe impl<T> Sync for Mmap<T> where T: Sync {}
impl<T> Mmap<T> {
/// Map a file into memory.
///
/// # Safety
///
/// `fd` must refer to a valid file descriptor.
pub unsafe fn map_fd(
fd: RawFd,
ofs: u64,
@ -25,6 +30,8 @@ impl<T> Mmap<T> {
flags: mman::MapFlags,
) -> io::Result<Self> {
let byte_len = count * mem::size_of::<T>();
// libc::size_t vs usize
#[allow(clippy::useless_conversion)]
let data = mman::mmap(
ptr::null_mut(),
libc::size_t::try_from(byte_len).map_err(io_err_other)?,

View File

@ -119,7 +119,7 @@ pub mod string_as_base64 {
use base64;
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S>(data: &String, serializer: S) -> Result<S::Ok, S::Error>
pub fn serialize<S>(data: &str, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{

View File

@ -50,7 +50,7 @@ pub fn localtime(epoch: i64) -> Result<libc::tm, Error> {
let mut result = new_libc_tm();
unsafe {
if libc::localtime_r(&epoch, &mut result) == std::ptr::null_mut() {
if libc::localtime_r(&epoch, &mut result).is_null() {
bail!("libc::localtime failed for '{}'", epoch);
}
}
@ -64,7 +64,7 @@ pub fn gmtime(epoch: i64) -> Result<libc::tm, Error> {
let mut result = new_libc_tm();
unsafe {
if libc::gmtime_r(&epoch, &mut result) == std::ptr::null_mut() {
if libc::gmtime_r(&epoch, &mut result).is_null() {
bail!("libc::gmtime failed for '{}'", epoch);
}
}

View File

@ -44,6 +44,12 @@ pub use byte_vec::*;
/// v.set_len(len);
/// }
/// ```
///
/// # Safety
///
/// It's generally not unsafe to use this method, but the contents are uninitialized, and since
/// this does not return a `MaybeUninit` type to track the initialization state, this is simply
/// marked as unsafe for good measure.
#[inline]
pub unsafe fn uninitialized(len: usize) -> Vec<u8> {
let mut out = Vec::with_capacity(len);

View File

@ -64,6 +64,12 @@ pub trait ByteVecExt {
/// file.append_to_vec(&mut buffer, 1024)?;
/// ```
///
/// # Safety
///
/// When increasing the size, the new contents are uninitialized and have nothing to do with
/// the previously contained content. Since we cannot track this state through the type system,
/// this method is marked as an unsafe API for good measure.
///
/// [`ReadExt`]: crate::io::ReadExt
unsafe fn grow_uninitialized(&mut self, more: usize) -> &mut [u8];
@ -77,6 +83,12 @@ pub trait ByteVecExt {
/// }
/// }
/// ```
///
/// # Safety
///
/// When increasing the size, the new contents are uninitialized and have nothing to do with
/// the previously contained content. Since we cannot track this state through the type system,
/// this method is marked as an unsafe API for good measure.
unsafe fn resize_uninitialized(&mut self, total: usize);
}