Skip to content

Commit

Permalink
Merge pull request #516 from hellow554/clippy_lints
Browse files Browse the repository at this point in the history
Apply Clippy lints
  • Loading branch information
KodrAus committed Jun 2, 2022
2 parents 65cbb90 + d9f0939 commit ca62c39
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 196 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/main.yml
Expand Up @@ -60,6 +60,18 @@ jobs:
rustup component add rustfmt
- run: cargo fmt -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Install Rust
run: |
rustup update stable --no-self-update
rustup default stable
rustup component add clippy
- run: cargo clippy --verbose

doc:
name: Check Documentation
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions build.rs
Expand Up @@ -22,7 +22,7 @@ fn main() {
}

fn target_has_atomic_cas(target: &str) -> bool {
match &target[..] {
match target {
"thumbv6m-none-eabi"
| "msp430-none-elf"
| "riscv32i-unknown-none-elf"
Expand All @@ -32,7 +32,7 @@ fn target_has_atomic_cas(target: &str) -> bool {
}

fn target_has_atomics(target: &str) -> bool {
match &target[..] {
match target {
"thumbv4t-none-eabi"
| "msp430-none-elf"
| "riscv32i-unknown-none-elf"
Expand Down
1 change: 1 addition & 0 deletions clippy.toml
@@ -0,0 +1 @@
msrv = "1.31.0"
43 changes: 4 additions & 39 deletions src/kv/key.rs
@@ -1,9 +1,7 @@
//! Structured keys.

use std::borrow::Borrow;
use std::cmp;
use std::fmt;
use std::hash;

/// A type that can be converted into a [`Key`](struct.Key.html).
pub trait ToKey {
Expand Down Expand Up @@ -33,15 +31,17 @@ impl ToKey for str {
}

/// A key in a structured key-value pair.
#[derive(Clone)]
// These impls must only be based on the as_str() representation of the key
// If a new field (such as an optional index) is added to the key they must not affect comparison
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Key<'k> {
key: &'k str,
}

impl<'k> Key<'k> {
/// Get a key from a borrowed string.
pub fn from_str(key: &'k str) -> Self {
Key { key: key }
Key { key }
}

/// Get a borrowed string from this key.
Expand All @@ -50,47 +50,12 @@ impl<'k> Key<'k> {
}
}

impl<'k> fmt::Debug for Key<'k> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.key.fmt(f)
}
}

impl<'k> fmt::Display for Key<'k> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.key.fmt(f)
}
}

impl<'k> hash::Hash for Key<'k> {
fn hash<H>(&self, state: &mut H)
where
H: hash::Hasher,
{
self.as_str().hash(state)
}
}

impl<'k, 'ko> PartialEq<Key<'ko>> for Key<'k> {
fn eq(&self, other: &Key<'ko>) -> bool {
self.as_str().eq(other.as_str())
}
}

impl<'k> Eq for Key<'k> {}

impl<'k, 'ko> PartialOrd<Key<'ko>> for Key<'k> {
fn partial_cmp(&self, other: &Key<'ko>) -> Option<cmp::Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}

impl<'k> Ord for Key<'k> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.as_str().cmp(other.as_str())
}
}

impl<'k> AsRef<str> for Key<'k> {
fn as_ref(&self) -> &str {
self.as_str()
Expand Down
162 changes: 18 additions & 144 deletions src/lib.rs
Expand Up @@ -422,7 +422,7 @@ static LEVEL_PARSE_ERROR: &str =
/// [`log!`](macro.log.html), and comparing a `Level` directly to a
/// [`LevelFilter`](enum.LevelFilter.html).
#[repr(usize)]
#[derive(Copy, Eq, Debug, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub enum Level {
/// The "error" level.
///
Expand All @@ -449,86 +449,18 @@ pub enum Level {
Trace,
}

impl Clone for Level {
#[inline]
fn clone(&self) -> Level {
*self
}
}

impl PartialEq for Level {
#[inline]
fn eq(&self, other: &Level) -> bool {
*self as usize == *other as usize
}
}

impl PartialEq<LevelFilter> for Level {
#[inline]
fn eq(&self, other: &LevelFilter) -> bool {
*self as usize == *other as usize
}
}

impl PartialOrd for Level {
#[inline]
fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}

#[inline]
fn lt(&self, other: &Level) -> bool {
(*self as usize) < *other as usize
}

#[inline]
fn le(&self, other: &Level) -> bool {
*self as usize <= *other as usize
}

#[inline]
fn gt(&self, other: &Level) -> bool {
*self as usize > *other as usize
}

#[inline]
fn ge(&self, other: &Level) -> bool {
*self as usize >= *other as usize
}
}

impl PartialOrd<LevelFilter> for Level {
#[inline]
fn partial_cmp(&self, other: &LevelFilter) -> Option<cmp::Ordering> {
Some((*self as usize).cmp(&(*other as usize)))
}

#[inline]
fn lt(&self, other: &LevelFilter) -> bool {
(*self as usize) < *other as usize
}

#[inline]
fn le(&self, other: &LevelFilter) -> bool {
*self as usize <= *other as usize
}

#[inline]
fn gt(&self, other: &LevelFilter) -> bool {
*self as usize > *other as usize
}

#[inline]
fn ge(&self, other: &LevelFilter) -> bool {
*self as usize >= *other as usize
}
}

impl Ord for Level {
#[inline]
fn cmp(&self, other: &Level) -> cmp::Ordering {
(*self as usize).cmp(&(*other as usize))
}
}

fn ok_or<T, E>(t: Option<T>, e: E) -> Result<T, E> {
Expand Down Expand Up @@ -638,7 +570,7 @@ impl Level {
/// [`max_level()`]: fn.max_level.html
/// [`set_max_level`]: fn.set_max_level.html
#[repr(usize)]
#[derive(Copy, Eq, Debug, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub enum LevelFilter {
/// A level lower than all log levels.
Off,
Expand All @@ -654,88 +586,18 @@ pub enum LevelFilter {
Trace,
}

// Deriving generates terrible impls of these traits

impl Clone for LevelFilter {
#[inline]
fn clone(&self) -> LevelFilter {
*self
}
}

impl PartialEq for LevelFilter {
#[inline]
fn eq(&self, other: &LevelFilter) -> bool {
*self as usize == *other as usize
}
}

impl PartialEq<Level> for LevelFilter {
#[inline]
fn eq(&self, other: &Level) -> bool {
other.eq(self)
}
}

impl PartialOrd for LevelFilter {
#[inline]
fn partial_cmp(&self, other: &LevelFilter) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}

#[inline]
fn lt(&self, other: &LevelFilter) -> bool {
(*self as usize) < *other as usize
}

#[inline]
fn le(&self, other: &LevelFilter) -> bool {
*self as usize <= *other as usize
}

#[inline]
fn gt(&self, other: &LevelFilter) -> bool {
*self as usize > *other as usize
}

#[inline]
fn ge(&self, other: &LevelFilter) -> bool {
*self as usize >= *other as usize
}
}

impl PartialOrd<Level> for LevelFilter {
#[inline]
fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
Some((*self as usize).cmp(&(*other as usize)))
}

#[inline]
fn lt(&self, other: &Level) -> bool {
(*self as usize) < *other as usize
}

#[inline]
fn le(&self, other: &Level) -> bool {
*self as usize <= *other as usize
}

#[inline]
fn gt(&self, other: &Level) -> bool {
*self as usize > *other as usize
}

#[inline]
fn ge(&self, other: &Level) -> bool {
*self as usize >= *other as usize
}
}

impl Ord for LevelFilter {
#[inline]
fn cmp(&self, other: &LevelFilter) -> cmp::Ordering {
(*self as usize).cmp(&(*other as usize))
}
}

impl FromStr for LevelFilter {
Expand Down Expand Up @@ -1143,6 +1005,12 @@ impl<'a> RecordBuilder<'a> {
}
}

impl<'a> Default for RecordBuilder<'a> {
fn default() -> Self {
Self::new()
}
}

/// Metadata about a log message.
///
/// # Use
Expand Down Expand Up @@ -1266,6 +1134,12 @@ impl<'a> MetadataBuilder<'a> {
}
}

impl<'a> Default for MetadataBuilder<'a> {
fn default() -> Self {
Self::new()
}
}

/// A trait encapsulating the operations required of a logger.
pub trait Log: Sync + Send {
/// Determines if a log message with the specified metadata would be
Expand Down Expand Up @@ -1316,10 +1190,10 @@ where
}

fn log(&self, record: &Record) {
(**self).log(record)
(**self).log(record);
}
fn flush(&self) {
(**self).flush()
(**self).flush();
}
}

Expand Down Expand Up @@ -1364,7 +1238,7 @@ where
/// Note that `Trace` is the maximum level, because it provides the maximum amount of detail in the emitted logs.
#[inline]
pub fn set_max_level(level: LevelFilter) {
MAX_LOG_LEVEL_FILTER.store(level as usize, Ordering::Relaxed)
MAX_LOG_LEVEL_FILTER.store(level as usize, Ordering::Relaxed);
}

/// Returns the current maximum log level.
Expand Down Expand Up @@ -1555,7 +1429,7 @@ impl error::Error for SetLoggerError {}
///
/// [`from_str`]: https://doc.rust-lang.org/std/str/trait.FromStr.html#tymethod.from_str
#[allow(missing_copy_implementations)]
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub struct ParseLevelError(());

impl fmt::Display for ParseLevelError {
Expand Down
10 changes: 5 additions & 5 deletions tests/filters.rs
Expand Up @@ -69,15 +69,15 @@ fn main() {
fn test(a: &State, filter: LevelFilter) {
log::set_max_level(filter);
error!("");
last(&a, t(Level::Error, filter));
last(a, t(Level::Error, filter));
warn!("");
last(&a, t(Level::Warn, filter));
last(a, t(Level::Warn, filter));
info!("");
last(&a, t(Level::Info, filter));
last(a, t(Level::Info, filter));
debug!("");
last(&a, t(Level::Debug, filter));
last(a, t(Level::Debug, filter));
trace!("");
last(&a, t(Level::Trace, filter));
last(a, t(Level::Trace, filter));

fn t(lvl: Level, filter: LevelFilter) -> Option<Level> {
if lvl <= filter {
Expand Down

0 comments on commit ca62c39

Please sign in to comment.