From 9548f1dd1bbe7cb564da9d0c372b7dcff42afb35 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Fri, 7 Dec 2018 12:25:25 +0100 Subject: [PATCH] More clippy pedantism! (#70) --- Cargo.toml | 1 - README.md | 12 ++++++++++++ RELEASING.md | 4 ++++ src/cache.rs | 7 ++++--- src/config.rs | 23 ++++++++++++----------- src/error.rs | 3 ++- src/main.rs | 40 +++++----------------------------------- src/tokenizer.rs | 8 ++++---- src/types.rs | 4 ++-- 9 files changed, 45 insertions(+), 57 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 00f2fa2..d4deb05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,6 @@ tempdir = "^0.3" utime = "0.2.0" [features] -dev = ["clippy"] logging = ["env_logger"] [profile.release] diff --git a/README.md b/README.md index af9b690..2eee710 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,18 @@ To get bash autocompletion, simply rename the file `bash_tealdeer` to `tldr` and copy it to `/usr/share/bash-completion/completions/tldr`. +## Development + +To run tests: + + $ cargo test + +To run lints: + + $ rustup component add clippy + $ cargo clean && cargo clippy + + ## License Licensed under either of diff --git a/RELEASING.md b/RELEASING.md index a5016b0..aed6a9b 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -1,5 +1,9 @@ # Releasing +Run linting: + + $ cargo clean && cargo clippy + Set variables: $ export VERSION=X.Y.Z diff --git a/src/cache.rs b/src/cache.rs index 9d08457..d08615f 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -24,13 +24,13 @@ pub struct Cache { } impl Cache { - pub fn new(url: S, os: OsType) -> Cache + pub fn new(url: S, os: OsType) -> Self where S: Into, { - Cache { + Self { url: url.into(), - os: os, + os, } } @@ -132,6 +132,7 @@ impl Cache { } /// Return the platform directory. + #[allow(clippy::match_same_arms)] fn get_platform_dir(&self) -> Option<&'static str> { match self.os { OsType::Linux => Some("linux"), diff --git a/src/config.rs b/src/config.rs index 443d87f..787de94 100644 --- a/src/config.rs +++ b/src/config.rs @@ -35,7 +35,7 @@ pub enum RawColor { } impl From for Color { - fn from(raw_color: RawColor) -> Color { + fn from(raw_color: RawColor) -> Self { match raw_color { RawColor::Black => Color::Black, RawColor::Red => Color::Red, @@ -60,8 +60,8 @@ struct RawStyle { } impl Default for RawStyle { - fn default() -> RawStyle { - RawStyle { + fn default() -> Self { + Self { foreground: None, background: None, underline: false, @@ -71,8 +71,8 @@ impl Default for RawStyle { } // impl RawStyle impl From for Style { - fn from(raw_style: RawStyle) -> Style { - let mut style = Style::default(); + fn from(raw_style: RawStyle) -> Self { + let mut style = Self::default(); if let Some(foreground) = raw_style.foreground { style = style.fg(Color::from(foreground)); @@ -114,8 +114,8 @@ struct RawConfig { } impl RawConfig { - fn new() -> RawConfig { - let mut raw_config = RawConfig::default(); + fn new() -> Self { + let mut raw_config = Self::default(); // Set default config raw_config.style.example_text.foreground = Some(RawColor::Green); @@ -143,8 +143,8 @@ pub struct Config { } impl From for Config { - fn from(raw_config: RawConfig) -> Config { - Config { + fn from(raw_config: RawConfig) -> Self { + Self { style: StyleConfig { command_name: raw_config.style.command_name.into(), description: raw_config.style.description.into(), @@ -156,12 +156,13 @@ impl From for Config { } } +#[allow(clippy::needless_pass_by_value)] fn map_io_err_to_config_err(e: IoError) -> TealdeerError { ConfigError(format!("Io Error: {}", e)) } impl Config { - pub fn load() -> Result { + pub fn load() -> Result { debug!("Loading config"); // Determine path @@ -182,7 +183,7 @@ impl Config { RawConfig::new() }; - Ok(Config::from(raw_config)) + Ok(Self::from(raw_config)) } } // impl Config diff --git a/src/error.rs b/src/error.rs index 96a5f4e..60e0756 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,6 +2,7 @@ use std::fmt; use reqwest::Error as ReqwestError; #[derive(Debug)] +#[allow(clippy::pub_enum_variant_names)] pub enum TealdeerError { CacheError(String), ConfigError(String), @@ -9,7 +10,7 @@ pub enum TealdeerError { } impl From for TealdeerError { - fn from(err: ReqwestError) -> TealdeerError { + fn from(err: ReqwestError) -> Self { TealdeerError::UpdateError(format!("HTTP error: {}", err.to_string())) } } diff --git a/src/main.rs b/src/main.rs index dc9976c..06c3f72 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,40 +8,10 @@ // option. All files in the project carrying such notice may not be // copied, modified, or distributed except according to those terms. -#![deny( - missing_docs, - missing_debug_implementations, - unsafe_code, - unused_import_braces, - unused_qualifications -)] -#![warn( - trivial_casts, - trivial_numeric_casts, - missing_copy_implementations, - unused_extern_crates, - unused_results -)] -#![cfg_attr(feature = "dev", feature(plugin))] -#![cfg_attr(feature = "dev", plugin(clippy))] -#![cfg_attr( - feature = "dev", - warn( - cast_possible_truncation, - cast_possible_wrap, - cast_precision_loss, - cast_sign_loss, - mut_mut, - non_ascii_literal, - option_unwrap_used, - result_unwrap_used, - shadow_reuse, - shadow_same, - unicode_not_nfc, - wrong_self_convention, - wrong_pub_self_convention - ) -)] +#![deny(clippy::all)] +#![warn(clippy::pedantic)] +#![allow(clippy::similar_names)] +#![allow(clippy::stutter)] #[cfg(feature = "logging")] extern crate env_logger; @@ -105,7 +75,7 @@ To render a local file (for testing): $ tldr --render /path/to/file.md "; const ARCHIVE_URL: &str = "https://github.com/tldr-pages/tldr/archive/master.tar.gz"; -const MAX_CACHE_AGE: i64 = 2592000; // 30 days +const MAX_CACHE_AGE: i64 = 2_592_000; // 30 days #[derive(Debug, Deserialize)] struct Args { diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 846c9f1..7c33740 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -15,7 +15,7 @@ pub enum TldrFormat { V2, } -/// A tokenizer is initialized with a BufReader instance that contains the +/// A tokenizer is initialized with a `BufReader` instance that contains the /// entire Tldr page. It then returns tokens as `Option`. #[derive(Debug)] pub struct Tokenizer { @@ -33,9 +33,9 @@ impl Tokenizer where R: BufRead, { - pub fn new(reader: R) -> Tokenizer { - Tokenizer { - reader: reader, + pub fn new(reader: R) -> Self { + Self { + reader, first_line: true, current_line: String::new(), format: TldrFormat::Undecided, diff --git a/src/types.rs b/src/types.rs index a7d3f35..2e2c3b9 100644 --- a/src/types.rs +++ b/src/types.rs @@ -37,7 +37,7 @@ pub enum LineType { impl<'a> From<&'a str> for LineType { /// Convert a string slice to a LineType. Newlines and trailing whitespace are trimmed. - fn from(line: &'a str) -> LineType { + fn from(line: &'a str) -> Self { let trimmed: &str = line.trim_right(); let mut chars = trimmed.chars(); match chars.next() { @@ -65,7 +65,7 @@ impl<'a> From<&'a str> for LineType { impl LineType { /// Support for old format. /// TODO: Remove once old format has been phased out! - pub fn from_v1(line: &str) -> LineType { + pub fn from_v1(line: &str) -> Self { let trimmed = line.trim(); let mut chars = trimmed.chars(); match chars.next() {