From a5154325a0b3d058a2edfce775e66186124896c8 Mon Sep 17 00:00:00 2001 From: kennytm Date: Mon, 30 Jul 2018 17:31:40 +0800 Subject: [PATCH] Fix clippy warnings. --- rustfmt.toml | 1 + src/bits.rs | 6 +++--- src/canvas.rs | 9 ++------- src/lib.rs | 19 +++++++++---------- src/optimize.rs | 4 ++-- src/types.rs | 34 +++++++++++++++++----------------- 6 files changed, 34 insertions(+), 39 deletions(-) diff --git a/rustfmt.toml b/rustfmt.toml index c775577..acd2021 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,2 +1,3 @@ max_width = 120 use_small_heuristics = "Max" +use_field_init_shorthand = true diff --git a/src/bits.rs b/src/bits.rs index 78fa506..df2e0c0 100644 --- a/src/bits.rs +++ b/src/bits.rs @@ -22,7 +22,7 @@ pub struct Bits { impl Bits { /// Constructs a new, empty bits structure. pub fn new(version: Version) -> Self { - Self { data: Vec::new(), bit_offset: 0, version: version } + Self { data: Vec::new(), bit_offset: 0, version } } /// Pushes an N-bit big-endian integer to the end of the bits. @@ -255,7 +255,7 @@ impl Bits { self.push_number(2, 0b10); self.push_number(14, eci_designator.as_u16()); } - 16384...999999 => { + 16384...999_999 => { self.push_number(3, 0b110); self.push_number(5, (eci_designator >> 16).as_u16()); self.push_number(16, (eci_designator & 0xffff).as_u16()); @@ -688,7 +688,7 @@ impl Bits { } if self.len() < data_length { - const PADDING_BYTES: &'static [u8] = &[0b11101100, 0b00010001]; + const PADDING_BYTES: &[u8] = &[0b1110_1100, 0b0001_0001]; self.bit_offset = 0; let data_bytes_length = data_length / 8; diff --git a/src/canvas.rs b/src/canvas.rs index f7a34f5..c66075e 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -93,12 +93,7 @@ impl Canvas { /// Constructs a new canvas big enough for a QR code of the given version. pub fn new(version: Version, ec_level: EcLevel) -> Self { let width = version.width(); - Self { - width: width, - version: version, - ec_level: ec_level, - modules: vec![Module::Empty; (width * width).as_usize()], - } + Self { width, version, ec_level, modules: vec![Module::Empty; (width * width).as_usize()] } } /// Converts the canvas into a human-readable string. @@ -1149,7 +1144,7 @@ impl DataModuleIter { Self { x: width - 1, y: width - 1, - width: width, + width, timing_pattern_column: match version { Version::Micro(_) => 0, Version::Normal(_) => 6, diff --git a/src/lib.rs b/src/lib.rs index 5db21e7..04cecf0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,12 +31,16 @@ #![cfg_attr(feature = "bench", feature(test, external_doc))] // Unstable libraries #![cfg_attr(feature = "cargo-clippy", deny(warnings, clippy_pedantic))] -#![cfg_attr(feature = "cargo-clippy", - allow(unreadable_literal, missing_docs_in_private_items, shadow_reuse, - range_plus_one))] - +#![cfg_attr( + feature = "cargo-clippy", + allow( + indexing_slicing, + write_literal, // see https://github.com/rust-lang-nursery/rust-clippy/issues/2976 + ) +)] #![cfg_attr(feature = "bench", doc(include = "../README.md"))] // ^ make sure we can test our README.md. +#![cfg_attr(feature = "cargo-clippy", allow())] extern crate checked_int_cast; #[cfg(feature = "image")] @@ -147,12 +151,7 @@ impl QrCode { canvas.draw_all_functional_patterns(); canvas.draw_data(&*encoded_data, &*ec_data); let canvas = canvas.apply_best_mask(); - Ok(Self { - content: canvas.into_colors(), - version: version, - ec_level: ec_level, - width: version.width().as_usize(), - }) + Ok(Self { content: canvas.into_colors(), version, ec_level, width: version.width().as_usize() }) } /// Gets the version of this QR code. diff --git a/src/optimize.rs b/src/optimize.rs index cbaf91d..3dee91c 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -271,14 +271,14 @@ impl> Optimizer { parser: segments, last_segment: Segment { mode: Mode::Numeric, begin: 0, end: 0 }, last_segment_size: 0, - version: version, + version, ended: true, }, Some(segment) => Self { parser: segments, last_segment: segment, last_segment_size: segment.encoded_len(version), - version: version, + version, ended: false, }, } diff --git a/src/types.rs b/src/types.rs index 88a0aba..1e069e8 100644 --- a/src/types.rs +++ b/src/types.rs @@ -129,8 +129,8 @@ pub enum Version { impl Version { /// Get the number of "modules" on each size of the QR code, i.e. the width /// and height of the code. - pub fn width(&self) -> i16 { - match *self { + pub fn width(self) -> i16 { + match self { Version::Normal(v) => v * 4 + 17, Version::Micro(v) => v * 2 + 9, } @@ -146,11 +146,11 @@ impl Version { /// /// If the entry compares equal to the default value of T, this method /// returns `Err(QrError::InvalidVersion)`. - pub fn fetch(&self, ec_level: EcLevel, table: &[[T; 4]]) -> QrResult + pub fn fetch(self, ec_level: EcLevel, table: &[[T; 4]]) -> QrResult where T: PartialEq + Default + Copy, { - match *self { + match self { Version::Normal(v @ 1...40) => { return Ok(table[(v - 1).as_usize()][ec_level as usize]); } @@ -166,16 +166,16 @@ impl Version { } /// The number of bits needed to encode the mode indicator. - pub fn mode_bits_count(&self) -> usize { - match *self { + pub fn mode_bits_count(self) -> usize { + match self { Version::Micro(a) => (a - 1).as_usize(), _ => 4, } } /// Checks whether is version refers to a Micro QR code. - pub fn is_micro(&self) -> bool { - match *self { + pub fn is_micro(self) -> bool { + match self { Version::Normal(_) => false, Version::Micro(_) => true, } @@ -212,28 +212,28 @@ impl Mode { /// /// This method will return `Err(QrError::UnsupportedCharacterSet)` if the /// mode is not supported in the given version. - pub fn length_bits_count(&self, version: Version) -> usize { + pub fn length_bits_count(self, version: Version) -> usize { match version { Version::Micro(a) => { let a = a.as_usize(); - match *self { + match self { Mode::Numeric => 2 + a, Mode::Alphanumeric | Mode::Byte => 1 + a, Mode::Kanji => a, } } - Version::Normal(1...9) => match *self { + Version::Normal(1...9) => match self { Mode::Numeric => 10, Mode::Alphanumeric => 9, Mode::Byte | Mode::Kanji => 8, }, - Version::Normal(10...26) => match *self { + Version::Normal(10...26) => match self { Mode::Numeric => 12, Mode::Alphanumeric => 11, Mode::Byte => 16, Mode::Kanji => 10, }, - Version::Normal(_) => match *self { + Version::Normal(_) => match self { Mode::Numeric => 14, Mode::Alphanumeric => 13, Mode::Byte => 16, @@ -250,8 +250,8 @@ impl Mode { /// /// Note that in Kanji mode, the `raw_data_len` is the number of Kanjis, /// i.e. half the total size of bytes. - pub fn data_bits_count(&self, raw_data_len: usize) -> usize { - match *self { + pub fn data_bits_count(self, raw_data_len: usize) -> usize { + match self { Mode::Numeric => (raw_data_len * 10 + 2) / 3, Mode::Alphanumeric => (raw_data_len * 11 + 1) / 2, Mode::Byte => raw_data_len * 8, @@ -269,10 +269,10 @@ impl Mode { /// assert!(a <= c); /// assert!(b <= c); /// - pub fn max(&self, other: Self) -> Self { + pub fn max(self, other: Self) -> Self { match self.partial_cmp(&other) { Some(Ordering::Less) | Some(Ordering::Equal) => other, - Some(Ordering::Greater) => *self, + Some(Ordering::Greater) => self, None => Mode::Byte, } }