Apply some manual clippy lints

Mostly replacing std::<type>::MAX with <type>::MAX.

Surprising here is replacing

.expect(format!(...))

with

.unwrap_or_else(|_| panic!(...))

It explains that this is because the "format!" would always be called.
This commit is contained in:
Fabian Boehm 2024-05-26 10:45:46 +02:00
parent 20830744a9
commit 52d1806e1f
6 changed files with 29 additions and 30 deletions

View file

@ -6,7 +6,7 @@ use std::sync::Mutex;
pub(crate) static LOCALE_LOCK: Mutex<()> = Mutex::new(());
/// It's CHAR_MAX.
const CHAR_MAX: libc::c_char = libc::c_char::max_value();
const CHAR_MAX: libc::c_char = libc::c_char::MAX;
/// Return the first character of a C string, or None if null, empty, has a length more than 1, or negative.
unsafe fn first_char(s: *const libc::c_char) -> Option<char> {

View file

@ -97,7 +97,7 @@ impl TermsizeData {
Self {
last_from_tty: None,
last_from_env: None,
last_tty_gen_count: u32::max_value(),
last_tty_gen_count: u32::MAX,
}
}

View file

@ -27,7 +27,6 @@
use std::{
f64::{
consts::{E, PI, TAU},
INFINITY, NAN, NEG_INFINITY,
},
fmt::Debug,
ops::{BitAnd, BitOr, BitXor},
@ -177,21 +176,21 @@ fn bitwise_op(a: f64, b: f64, f: fn(u64, u64) -> u64) -> f64 {
fn fac(n: f64) -> f64 {
if n < 0.0 {
return NAN;
return f64::NAN;
}
if n > (u64::MAX as f64) {
return INFINITY;
return f64::INFINITY;
}
let n = n as u64;
(1..=n)
.try_fold(1_u64, |acc, i| acc.checked_mul(i))
.map_or(INFINITY, |x| x as f64)
.map_or(f64::INFINITY, |x| x as f64)
}
fn maximum(n: &[f64]) -> f64 {
n.iter().fold(NEG_INFINITY, |a, &b| {
n.iter().fold(f64::NEG_INFINITY, |a, &b| {
if a.is_nan() {
return a;
}
@ -215,7 +214,7 @@ fn maximum(n: &[f64]) -> f64 {
}
fn minimum(n: &[f64]) -> f64 {
n.iter().fold(INFINITY, |a, &b| {
n.iter().fold(f64::INFINITY, |a, &b| {
if a.is_nan() {
return a;
}
@ -241,13 +240,13 @@ fn minimum(n: &[f64]) -> f64 {
fn ncr(n: f64, r: f64) -> f64 {
// Doing this for NAN takes ages - just return the result right away.
if n.is_nan() {
return INFINITY;
return f64::INFINITY;
}
if n < 0.0 || r < 0.0 || n < r {
return NAN;
return f64::NAN;
}
if n > (u64::MAX as f64) || r > (u64::MAX as f64) {
return INFINITY;
return f64::INFINITY;
}
let un = n as u64;
@ -260,7 +259,7 @@ fn ncr(n: f64, r: f64) -> f64 {
let mut result = 1_u64;
for i in 1..=ur {
let Some(next_result) = result.checked_mul(un - ur + i) else {
return INFINITY;
return f64::INFINITY;
};
result = next_result / i;
}
@ -594,7 +593,7 @@ impl<'s> State<'s> {
self.set_error(err, err_pos_len);
}
NAN
f64::NAN
}
Token::Open => {
self.next_token();
@ -610,7 +609,7 @@ impl<'s> State<'s> {
self.set_error(ErrorKind::MissingClosingParen, None)
}
NAN
f64::NAN
}
Token::End => {
// The expression ended before we expected it.
@ -620,7 +619,7 @@ impl<'s> State<'s> {
// "too few args".
self.set_error(ErrorKind::TooFewArgs, None);
NAN
f64::NAN
}
Token::Error | Token::Sep | Token::Close | Token::Infix(_) => {
@ -628,7 +627,7 @@ impl<'s> State<'s> {
self.set_error(ErrorKind::UnexpectedToken, None);
}
NAN
f64::NAN
}
}
}

View file

@ -66,7 +66,7 @@ pub type generation_t = u64;
impl FloggableDebug for topic_t {}
/// A generation value which indicates the topic is not of interest.
pub const INVALID_GENERATION: generation_t = std::u64::MAX;
pub const INVALID_GENERATION: generation_t = u64::MAX;
pub fn all_topics() -> [topic_t; 3] {
[topic_t::sighupint, topic_t::sigchld, topic_t::internal_exit]

View file

@ -227,7 +227,7 @@ mod tests {
// Helper to parse a float, expecting to succeed and consume the entire string.
fn parse(input: &str) -> f64 {
let res = parse_hex_float(input.chars(), '.')
.expect(format!("Failed to parse {}", input).as_str());
.unwrap_or_else(|_| panic!("Failed to parse {}", input));
// We expect to consume the entire string.
assert_eq!(res.1, input.len());
res.0
@ -320,7 +320,7 @@ mod tests {
fn test_parse_hex_float_length() {
let parse_len = |input: &str| {
let res = parse_hex_float(input.chars(), '.')
.expect(format!("Failed to parse {}", input).as_str());
.unwrap_or_else(|_| panic!("Failed to parse {}", input));
res.1
};
assert_eq!(parse_len("0x0ZZZ"), 3);
@ -363,7 +363,7 @@ mod tests {
// The mantissa and exponent can cancel each other out!
assert_eq!(parse(&format!("0x1{:0<512}p-2000", "")), 2.0f64.powi(48));
assert_eq!(parse(&format!("-0x1{:0<512}p-2000", "")), -2.0f64.powi(48));
assert_eq!(parse(&format!("-0x1{:0<512}p-2000", "")), -(2.0f64.powi(48)));
}
#[test]

View file

@ -375,20 +375,20 @@ mod tests {
// This is subtle. "567" in base 8 is "375" in base 10. The final "8" is not converted.
assert_eq!(run1_rad("5678", 8), Ok(375));
test_min_max(std::i8::MIN, std::i8::MAX);
test_min_max(std::i16::MIN, std::i16::MAX);
test_min_max(std::i32::MIN, std::i32::MAX);
test_min_max(std::i64::MIN, std::i64::MAX);
test_min_max(std::u8::MIN, std::u8::MAX);
test_min_max(std::u16::MIN, std::u16::MAX);
test_min_max(std::u32::MIN, std::u32::MAX);
test_min_max(std::u64::MIN, std::u64::MAX);
test_min_max(i8::MIN, i8::MAX);
test_min_max(i16::MIN, i16::MAX);
test_min_max(i32::MIN, i32::MAX);
test_min_max(i64::MIN, i64::MAX);
test_min_max(u8::MIN, u8::MAX);
test_min_max(u16::MIN, u16::MAX);
test_min_max(u32::MIN, u32::MAX);
test_min_max(u64::MIN, u64::MAX);
}
#[test]
fn test_unsigned() {
fn negu(x: u64) -> u64 {
std::u64::MAX - x + 1
u64::MAX - x + 1
}
let run1 = |s: &str| -> Result<u64, Error> {
@ -447,7 +447,7 @@ mod tests {
#[test]
fn test_wrap_neg() {
fn negu(x: u64) -> u64 {
std::u64::MAX - x + 1
u64::MAX - x + 1
}
let run1 = |s: &str, opts: Options| -> Result<u64, Error> { wcstoi_opts(s, opts) };