Fix clippy warnings.

This commit is contained in:
kennytm 2018-07-30 17:31:40 +08:00
parent 51dd9c4fa1
commit 049ebbefdb
6 changed files with 34 additions and 39 deletions

View file

@ -1,2 +1,3 @@
max_width = 120 max_width = 120
use_small_heuristics = "Max" use_small_heuristics = "Max"
use_field_init_shorthand = true

View file

@ -22,7 +22,7 @@ pub struct Bits {
impl Bits { impl Bits {
/// Constructs a new, empty bits structure. /// Constructs a new, empty bits structure.
pub fn new(version: Version) -> Self { 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. /// 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(2, 0b10);
self.push_number(14, eci_designator.as_u16()); self.push_number(14, eci_designator.as_u16());
} }
16384...999999 => { 16384...999_999 => {
self.push_number(3, 0b110); self.push_number(3, 0b110);
self.push_number(5, (eci_designator >> 16).as_u16()); self.push_number(5, (eci_designator >> 16).as_u16());
self.push_number(16, (eci_designator & 0xffff).as_u16()); self.push_number(16, (eci_designator & 0xffff).as_u16());
@ -688,7 +688,7 @@ impl Bits {
} }
if self.len() < data_length { if self.len() < data_length {
const PADDING_BYTES: &'static [u8] = &[0b11101100, 0b00010001]; const PADDING_BYTES: &[u8] = &[0b1110_1100, 0b0001_0001];
self.bit_offset = 0; self.bit_offset = 0;
let data_bytes_length = data_length / 8; let data_bytes_length = data_length / 8;

View file

@ -93,12 +93,7 @@ impl Canvas {
/// Constructs a new canvas big enough for a QR code of the given version. /// Constructs a new canvas big enough for a QR code of the given version.
pub fn new(version: Version, ec_level: EcLevel) -> Self { pub fn new(version: Version, ec_level: EcLevel) -> Self {
let width = version.width(); let width = version.width();
Self { Self { width, version, ec_level, modules: vec![Module::Empty; (width * width).as_usize()] }
width: width,
version: version,
ec_level: ec_level,
modules: vec![Module::Empty; (width * width).as_usize()],
}
} }
/// Converts the canvas into a human-readable string. /// Converts the canvas into a human-readable string.
@ -1149,7 +1144,7 @@ impl DataModuleIter {
Self { Self {
x: width - 1, x: width - 1,
y: width - 1, y: width - 1,
width: width, width,
timing_pattern_column: match version { timing_pattern_column: match version {
Version::Micro(_) => 0, Version::Micro(_) => 0,
Version::Normal(_) => 6, Version::Normal(_) => 6,

View file

@ -31,12 +31,16 @@
#![cfg_attr(feature = "bench", feature(test, external_doc))] // Unstable libraries #![cfg_attr(feature = "bench", feature(test, external_doc))] // Unstable libraries
#![cfg_attr(feature = "cargo-clippy", deny(warnings, clippy_pedantic))] #![cfg_attr(feature = "cargo-clippy", deny(warnings, clippy_pedantic))]
#![cfg_attr(feature = "cargo-clippy", #![cfg_attr(
allow(unreadable_literal, missing_docs_in_private_items, shadow_reuse, feature = "cargo-clippy",
range_plus_one))] allow(
indexing_slicing,
write_literal, // see https://github.com/rust-lang-nursery/rust-clippy/issues/2976
)
)]
#![cfg_attr(feature = "bench", doc(include = "../README.md"))] #![cfg_attr(feature = "bench", doc(include = "../README.md"))]
// ^ make sure we can test our README.md. // ^ make sure we can test our README.md.
#![cfg_attr(feature = "cargo-clippy", allow())]
extern crate checked_int_cast; extern crate checked_int_cast;
#[cfg(feature = "image")] #[cfg(feature = "image")]
@ -147,12 +151,7 @@ impl QrCode {
canvas.draw_all_functional_patterns(); canvas.draw_all_functional_patterns();
canvas.draw_data(&*encoded_data, &*ec_data); canvas.draw_data(&*encoded_data, &*ec_data);
let canvas = canvas.apply_best_mask(); let canvas = canvas.apply_best_mask();
Ok(Self { Ok(Self { content: canvas.into_colors(), version, ec_level, width: version.width().as_usize() })
content: canvas.into_colors(),
version: version,
ec_level: ec_level,
width: version.width().as_usize(),
})
} }
/// Gets the version of this QR code. /// Gets the version of this QR code.

View file

@ -271,14 +271,14 @@ impl<I: Iterator<Item = Segment>> Optimizer<I> {
parser: segments, parser: segments,
last_segment: Segment { mode: Mode::Numeric, begin: 0, end: 0 }, last_segment: Segment { mode: Mode::Numeric, begin: 0, end: 0 },
last_segment_size: 0, last_segment_size: 0,
version: version, version,
ended: true, ended: true,
}, },
Some(segment) => Self { Some(segment) => Self {
parser: segments, parser: segments,
last_segment: segment, last_segment: segment,
last_segment_size: segment.encoded_len(version), last_segment_size: segment.encoded_len(version),
version: version, version,
ended: false, ended: false,
}, },
} }

View file

@ -129,8 +129,8 @@ pub enum Version {
impl Version { impl Version {
/// Get the number of "modules" on each size of the QR code, i.e. the width /// Get the number of "modules" on each size of the QR code, i.e. the width
/// and height of the code. /// and height of the code.
pub fn width(&self) -> i16 { pub fn width(self) -> i16 {
match *self { match self {
Version::Normal(v) => v * 4 + 17, Version::Normal(v) => v * 4 + 17,
Version::Micro(v) => v * 2 + 9, 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 /// If the entry compares equal to the default value of T, this method
/// returns `Err(QrError::InvalidVersion)`. /// returns `Err(QrError::InvalidVersion)`.
pub fn fetch<T>(&self, ec_level: EcLevel, table: &[[T; 4]]) -> QrResult<T> pub fn fetch<T>(self, ec_level: EcLevel, table: &[[T; 4]]) -> QrResult<T>
where where
T: PartialEq + Default + Copy, T: PartialEq + Default + Copy,
{ {
match *self { match self {
Version::Normal(v @ 1...40) => { Version::Normal(v @ 1...40) => {
return Ok(table[(v - 1).as_usize()][ec_level as usize]); 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. /// The number of bits needed to encode the mode indicator.
pub fn mode_bits_count(&self) -> usize { pub fn mode_bits_count(self) -> usize {
match *self { match self {
Version::Micro(a) => (a - 1).as_usize(), Version::Micro(a) => (a - 1).as_usize(),
_ => 4, _ => 4,
} }
} }
/// Checks whether is version refers to a Micro QR code. /// Checks whether is version refers to a Micro QR code.
pub fn is_micro(&self) -> bool { pub fn is_micro(self) -> bool {
match *self { match self {
Version::Normal(_) => false, Version::Normal(_) => false,
Version::Micro(_) => true, Version::Micro(_) => true,
} }
@ -212,28 +212,28 @@ impl Mode {
/// ///
/// This method will return `Err(QrError::UnsupportedCharacterSet)` if the /// This method will return `Err(QrError::UnsupportedCharacterSet)` if the
/// mode is not supported in the given version. /// 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 { match version {
Version::Micro(a) => { Version::Micro(a) => {
let a = a.as_usize(); let a = a.as_usize();
match *self { match self {
Mode::Numeric => 2 + a, Mode::Numeric => 2 + a,
Mode::Alphanumeric | Mode::Byte => 1 + a, Mode::Alphanumeric | Mode::Byte => 1 + a,
Mode::Kanji => a, Mode::Kanji => a,
} }
} }
Version::Normal(1...9) => match *self { Version::Normal(1...9) => match self {
Mode::Numeric => 10, Mode::Numeric => 10,
Mode::Alphanumeric => 9, Mode::Alphanumeric => 9,
Mode::Byte | Mode::Kanji => 8, Mode::Byte | Mode::Kanji => 8,
}, },
Version::Normal(10...26) => match *self { Version::Normal(10...26) => match self {
Mode::Numeric => 12, Mode::Numeric => 12,
Mode::Alphanumeric => 11, Mode::Alphanumeric => 11,
Mode::Byte => 16, Mode::Byte => 16,
Mode::Kanji => 10, Mode::Kanji => 10,
}, },
Version::Normal(_) => match *self { Version::Normal(_) => match self {
Mode::Numeric => 14, Mode::Numeric => 14,
Mode::Alphanumeric => 13, Mode::Alphanumeric => 13,
Mode::Byte => 16, Mode::Byte => 16,
@ -250,8 +250,8 @@ impl Mode {
/// ///
/// Note that in Kanji mode, the `raw_data_len` is the number of Kanjis, /// Note that in Kanji mode, the `raw_data_len` is the number of Kanjis,
/// i.e. half the total size of bytes. /// i.e. half the total size of bytes.
pub fn data_bits_count(&self, raw_data_len: usize) -> usize { pub fn data_bits_count(self, raw_data_len: usize) -> usize {
match *self { match self {
Mode::Numeric => (raw_data_len * 10 + 2) / 3, Mode::Numeric => (raw_data_len * 10 + 2) / 3,
Mode::Alphanumeric => (raw_data_len * 11 + 1) / 2, Mode::Alphanumeric => (raw_data_len * 11 + 1) / 2,
Mode::Byte => raw_data_len * 8, Mode::Byte => raw_data_len * 8,
@ -269,10 +269,10 @@ impl Mode {
/// assert!(a <= c); /// assert!(a <= c);
/// assert!(b <= c); /// assert!(b <= c);
/// ///
pub fn max(&self, other: Self) -> Self { pub fn max(self, other: Self) -> Self {
match self.partial_cmp(&other) { match self.partial_cmp(&other) {
Some(Ordering::Less) | Some(Ordering::Equal) => other, Some(Ordering::Less) | Some(Ordering::Equal) => other,
Some(Ordering::Greater) => *self, Some(Ordering::Greater) => self,
None => Mode::Byte, None => Mode::Byte,
} }
} }