Skip to content

Commit 90b7523

Browse files
committed
Add our clippy lints.
1 parent ea01d16 commit 90b7523

File tree

8 files changed

+64
-40
lines changed

8 files changed

+64
-40
lines changed

clippy.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
avoid-breaking-exported-api = false
2+
disallowed-macros = [
3+
# Can also use an inline table with a `path` key.
4+
{ path = "std::print", reason = "no IO allowed" },
5+
{ path = "std::println", reason = "no IO allowed" },
6+
{ path = "std::format", reason = "no string allocation allowed" },
7+
{ path = "std::debug", reason = "debugging macros should not be present in any release" },
8+
# NOTE: unimplemented is fine because this can be for intentionally disabled methods
9+
{ path = "std::todo", reason = "should never have TODO macros in releases" },
10+
]
11+
disallowed-methods = [
12+
{ path = "std::io::stdout", reason = "no IO allowed" },
13+
{ path = "std::io::stdin", reason = "no IO allowed" },
14+
{ path = "std::io::stderr", reason = "no IO allowed" },
15+
]
16+
disallowed-types = [
17+
{ path = "std::io::File", reason = "no IO allowed" },
18+
{ path = "std::io::BufReader", reason = "need our own abstractions for reading/writing" },
19+
{ path = "std::io::BufWriter", reason = "need our own abstractions for reading/writing" },
20+
]

src/binary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn compute_float<F: Float>(q: i64, mut w: u64) -> AdjustedMantissa {
1717
w <<= lz;
1818
let (lo, hi) = compute_product_approx(q, w, F::MANTISSA_EXPLICIT_BITS + 3);
1919
if lo == 0xFFFF_FFFF_FFFF_FFFF {
20-
let inside_safe_exponent = (q >= -27) && (q <= 55);
20+
let inside_safe_exponent = (-27..=55).contains(&q);
2121
if !inside_safe_exponent {
2222
return am_error;
2323
}

src/common.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl<'a> AsciiStr<'a> {
1414
Self {
1515
ptr: s.as_ptr(),
1616
end: unsafe { s.as_ptr().add(s.len()) },
17-
_marker: PhantomData::default(),
17+
_marker: PhantomData,
1818
}
1919
}
2020

@@ -72,11 +72,11 @@ impl<'a> AsciiStr<'a> {
7272

7373
#[inline]
7474
pub fn first(&self) -> Option<u8> {
75-
if !self.is_empty() {
75+
if self.is_empty() {
76+
None
77+
} else {
7678
// SAFETY: safe since `!self.is_empty()`
7779
Some(unsafe { self.first_unchecked() })
78-
} else {
79-
None
8080
}
8181
}
8282

@@ -87,20 +87,12 @@ impl<'a> AsciiStr<'a> {
8787

8888
#[inline]
8989
pub fn first_is2(&self, c1: u8, c2: u8) -> bool {
90-
if let Some(c) = self.first() {
91-
c == c1 || c == c2
92-
} else {
93-
false
94-
}
90+
self.first().map_or(false, |c| c == c1 || c == c2)
9591
}
9692

9793
#[inline]
9894
pub fn first_is_digit(&self) -> bool {
99-
if let Some(c) = self.first() {
100-
c.is_ascii_digit()
101-
} else {
102-
false
103-
}
95+
self.first().map_or(false, |c| c.is_ascii_digit())
10496
}
10597

10698
#[inline]
@@ -116,13 +108,10 @@ impl<'a> AsciiStr<'a> {
116108

117109
#[inline]
118110
pub fn try_read_digit(&mut self) -> Option<u8> {
119-
if let Some(digit) = self.first_digit() {
120-
// SAFETY: Safe since `first_digit` means the buffer is not empty
121-
unsafe { self.step() };
122-
Some(digit)
123-
} else {
124-
None
125-
}
111+
let digit = self.first_digit()?;
112+
// SAFETY: Safe since `first_digit` means the buffer is not empty
113+
unsafe { self.step() };
114+
Some(digit)
126115
}
127116

128117
#[inline]
@@ -145,6 +134,7 @@ impl<'a> AsciiStr<'a> {
145134
///
146135
/// Safe if `self.len() >= 8`
147136
#[inline]
137+
#[allow(clippy::cast_ptr_alignment)]
148138
pub unsafe fn read_u64_unchecked(&self) -> u64 {
149139
debug_assert!(self.len() >= 8, "overflowing buffer: buffer is not 8 bytes long");
150140
let src = self.ptr as *const u64;
@@ -203,6 +193,7 @@ pub trait ByteSlice: AsRef<[u8]> + AsMut<[u8]> {
203193
///
204194
/// Safe if `self.len() >= 8`.
205195
#[inline]
196+
#[allow(clippy::cast_ptr_alignment)]
206197
unsafe fn read_u64(&self) -> u64 {
207198
debug_assert!(self.as_ref().len() >= 8);
208199
let src = self.as_ref().as_ptr() as *const u64;
@@ -214,6 +205,7 @@ pub trait ByteSlice: AsRef<[u8]> + AsMut<[u8]> {
214205
///
215206
/// Safe if `self.len() >= 8`.
216207
#[inline]
208+
#[allow(clippy::cast_ptr_alignment)]
217209
unsafe fn write_u64(&mut self, value: u64) {
218210
debug_assert!(self.as_ref().len() >= 8);
219211
let dst = self.as_mut().as_mut_ptr() as *mut u64;

src/decimal.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Decimal {
8787
if dp < self.num_digits {
8888
round_up = self.digits[dp] >= 5;
8989
if self.digits[dp] == 5 && dp + 1 == self.num_digits {
90-
round_up = self.truncated || ((dp != 0) && (1 & self.digits[dp - 1] != 0))
90+
round_up = self.truncated || ((dp != 0) && (1 & self.digits[dp - 1] != 0));
9191
}
9292
}
9393
if round_up {
@@ -267,6 +267,7 @@ pub fn parse_decimal(mut s: &[u8]) -> Decimal {
267267
}
268268

269269
#[inline]
270+
#[allow(clippy::redundant_else)]
270271
fn number_of_digits_decimal_left_shift(d: &Decimal, mut shift: usize) -> usize {
271272
const TABLE: [u16; 65] = [
272273
0x0000, 0x0800, 0x0801, 0x0803, 0x1006, 0x1009, 0x100D, 0x1812, 0x1817, 0x181D, 0x2024,
@@ -326,7 +327,7 @@ fn number_of_digits_decimal_left_shift(d: &Decimal, mut shift: usize) -> usize {
326327
shift &= 63;
327328
let x_a = TABLE[shift];
328329
let x_b = TABLE[shift + 1];
329-
let num_new_digits = (x_a >> 11) as _;
330+
let num_new_digits = (x_a >> 11) as usize;
330331
let pow5_a = (0x7FF & x_a) as usize;
331332
let pow5_b = (0x7FF & x_b) as usize;
332333
let pow5 = &TABLE_POW5[pow5_a..];

src/float.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ impl Float for f32 {
6868

6969
#[inline]
7070
fn from_u64(v: u64) -> Self {
71-
v as _
71+
v as f32
7272
}
7373

7474
#[inline]
7575
fn from_u64_bits(v: u64) -> Self {
76-
f32::from_bits((v & 0xFFFFFFFF) as u32)
76+
f32::from_bits((v & 0xFFFF_FFFF) as u32)
7777
}
7878

7979
#[inline]
@@ -108,7 +108,7 @@ impl Float for f64 {
108108

109109
#[inline]
110110
fn from_u64(v: u64) -> Self {
111-
v as _
111+
v as f64
112112
}
113113

114114
#[inline]

src/lib.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
//! returns an error if there are invalid characters or if the string is
1313
//! empty.
1414
//! - [`parse_partial`](crate::parse_partial()) tries to find the longest
15-
//! substring at the
16-
//! beginning of the given input string that can be parsed as a decimal number
17-
//! and, in the case of success, returns the parsed value along the number of
18-
//! characters processed; an error is returned if the string doesn't start with
19-
//! a decimal number or if it is empty. This function is most useful as a
20-
//! building block when constructing more complex parsers, or when parsing
21-
//! streams of data.
15+
//! substring at the beginning of the given input string that can be parsed as
16+
//! a decimal number and, in the case of success, returns the parsed value
17+
//! along the number of characters processed; an error is returned if the
18+
//! string doesn't start with a decimal number or if it is empty. This
19+
//! function is most useful as a building block when constructing more complex
20+
//! parsers, or when parsing streams of data.
2221
//!
2322
//! ## Examples
2423
//!
@@ -36,7 +35,17 @@
3635
//! assert_eq!(&s[n..], "foo");
3736
//! ```
3837
38+
#![allow(unused_unsafe)]
39+
#![warn(unsafe_op_in_unsafe_fn)]
3940
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
41+
#![deny(
42+
clippy::doc_markdown,
43+
clippy::unnecessary_safety_comment,
44+
clippy::semicolon_if_nothing_returned,
45+
clippy::unwrap_used,
46+
clippy::as_underscore,
47+
clippy::doc_markdown
48+
)]
4049
#![allow(
4150
clippy::cast_possible_truncation,
4251
clippy::cast_possible_wrap,
@@ -46,7 +55,8 @@
4655
clippy::missing_const_for_fn,
4756
clippy::use_self,
4857
clippy::module_name_repetitions,
49-
clippy::cargo_common_metadata
58+
clippy::cargo_common_metadata,
59+
clippy::struct_field_names
5060
)]
5161

5262
use core::fmt::{self, Display};

src/number.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::float::Float;
33

44
const MIN_19DIGIT_INT: u64 = 100_0000_0000_0000_0000;
55

6+
#[allow(clippy::unreadable_literal)]
67
pub const INT_POW10: [u64; 16] = [
78
1,
89
10,
@@ -46,9 +47,9 @@ impl Number {
4647
// normal fast path
4748
let value = F::from_u64(self.mantissa);
4849
if self.exponent < 0 {
49-
value / F::pow10_fast_path((-self.exponent) as _)
50+
value / F::pow10_fast_path((-self.exponent) as usize)
5051
} else {
51-
value * F::pow10_fast_path(self.exponent as _)
52+
value * F::pow10_fast_path(self.exponent as usize)
5253
}
5354
} else {
5455
// disguised fast path
@@ -57,7 +58,7 @@ impl Number {
5758
if mantissa > F::MAX_MANTISSA_FAST_PATH {
5859
return None;
5960
}
60-
F::from_u64(mantissa) * F::pow10_fast_path(F::MAX_EXPONENT_FAST_PATH as _)
61+
F::from_u64(mantissa) * F::pow10_fast_path(F::MAX_EXPONENT_FAST_PATH as usize)
6162
};
6263
if self.negative {
6364
value = -value;
@@ -201,7 +202,7 @@ pub fn parse_number(s: &[u8]) -> Option<(Number, usize)> {
201202
let exp_number = parse_scientific(&mut s);
202203
exponent += exp_number;
203204

204-
let len = s.offset_from(&start) as _;
205+
let len = s.offset_from(&start) as usize;
205206

206207
// handle uncommon case with many digits
207208
if n_digits <= 19 {

src/simple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn parse_long_mantissa<F: Float>(s: &[u8]) -> AdjustedMantissa {
4545
_ => 1,
4646
}
4747
} else {
48-
get_shift((-d.decimal_point) as _)
48+
get_shift((-d.decimal_point) as usize)
4949
};
5050
d.left_shift(shift);
5151
if d.decimal_point > Decimal::DECIMAL_POINT_RANGE {

0 commit comments

Comments
 (0)