Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply Clippy lints #516

Merged
merged 5 commits into from Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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

features:
name: Feature check
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"
41 changes: 2 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,15 @@ impl ToKey for str {
}

/// A key in a structured key-value pair.
#[derive(Clone)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
hellow554 marked this conversation as resolved.
Show resolved Hide resolved
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 +48,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 @@ -421,7 +421,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 @@ -448,86 +448,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 @@ -637,7 +569,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 @@ -653,88 +585,18 @@ pub enum LevelFilter {
Trace,
}

// Deriving generates terrible impls of these traits
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This statement is not right. I created a godbolt for it. There's no real difference between derived impls and custom impls (anymore): https://rust.godbolt.org/z/7K1vjG6KE


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 @@ -1142,6 +1004,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 @@ -1265,6 +1133,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 @@ -1307,10 +1181,10 @@ where
}

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

Expand Down Expand Up @@ -1355,7 +1229,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 @@ -1546,7 +1420,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)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functional/API change, but personally I'm ok with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clippy suggested it and I thought it made sense.

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