From 8289448c3e3d3d8dcc7f90b841f7fc81a6fa5abd Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sat, 1 Feb 2025 15:53:31 -0700 Subject: [PATCH] x509-cert: enable and fix clippy `core`/`alloc`/`std` lints Enables the following clippy lints: - `clippy::alloc_instead_of_core` - `clippy::std_instead_of_alloc` - `clippy::std_instead_of_core` These check that imports are coming from the correct package in the standard library, enabling more features to be used in `no_std` environments. --- x509-cert/src/builder.rs | 3 +-- x509-cert/src/ext/pkix/name/general.rs | 13 ++++++------- x509-cert/src/lib.rs | 3 +++ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/x509-cert/src/builder.rs b/x509-cert/src/builder.rs index 809e1a538..1949ce25b 100644 --- a/x509-cert/src/builder.rs +++ b/x509-cert/src/builder.rs @@ -66,8 +66,7 @@ pub enum Error { MissingAttributes, } -#[cfg(feature = "std")] -impl std::error::Error for Error {} +impl core::error::Error for Error {} impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/x509-cert/src/ext/pkix/name/general.rs b/x509-cert/src/ext/pkix/name/general.rs index 821599d1d..0c2f057a9 100644 --- a/x509-cert/src/ext/pkix/name/general.rs +++ b/x509-cert/src/ext/pkix/name/general.rs @@ -62,19 +62,18 @@ pub enum GeneralName { RegisteredId(ObjectIdentifier), } -#[cfg(feature = "std")] -impl From for GeneralName { - fn from(ip: std::net::IpAddr) -> Self { +impl From for GeneralName { + fn from(ip: core::net::IpAddr) -> Self { // Safety: this is unfailable here, OctetString will issue an error if you go // over 256MiB, here the buffer is at most 16 bytes (ipv6). The two `expect`s // below are safe. let buf = match ip { - std::net::IpAddr::V4(v) => { + core::net::IpAddr::V4(v) => { let value = v.octets(); OctetString::new(&value[..]) .expect("OctetString is not expected to fail with a 4 bytes long buffer") } - std::net::IpAddr::V6(v) => { + core::net::IpAddr::V6(v) => { let value = v.octets(); OctetString::new(&value[..]) .expect("OctetString is not expected to fail with a 16 bytes long buffer") @@ -85,7 +84,7 @@ impl From for GeneralName { } } -#[cfg(all(feature = "std", test))] +#[cfg(test)] #[allow(clippy::unwrap_used)] mod tests { use super::*; @@ -93,7 +92,7 @@ mod tests { #[test] fn test_convert() { - use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; + use core::net::{IpAddr, Ipv4Addr, Ipv6Addr}; let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)); let localhost_v6 = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); diff --git a/x509-cert/src/lib.rs b/x509-cert/src/lib.rs index 6af915625..5488f482e 100644 --- a/x509-cert/src/lib.rs +++ b/x509-cert/src/lib.rs @@ -7,7 +7,10 @@ )] #![forbid(unsafe_code)] #![warn( + clippy::alloc_instead_of_core, clippy::mod_module_files, + clippy::std_instead_of_alloc, + clippy::std_instead_of_core, clippy::unwrap_used, missing_docs, rust_2018_idioms,