From: Ben Pfaff Date: Thu, 10 Apr 2025 18:07:12 +0000 (-0700) Subject: clippy X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2d27df73daecaf84ee66942e038d893be76cd012;p=pspp clippy --- diff --git a/rust/pspp/src/command/mod.rs b/rust/pspp/src/command/mod.rs index b0480be4cb..f7caf2834b 100644 --- a/rust/pspp/src/command/mod.rs +++ b/rust/pspp/src/command/mod.rs @@ -78,14 +78,14 @@ impl Parsed { pub fn new(value: T, rest: TokenSlice, warnings: Diagnostics) -> Self { Self { value, - rest: rest, + rest, diagnostics: warnings, } } pub fn ok(value: T, rest: TokenSlice) -> Self { Self { value, - rest: rest, + rest, diagnostics: Diagnostics::default(), } } @@ -354,7 +354,7 @@ where write!(f, "Subcommands[")?; for (index, item) in self.0.iter().enumerate() { if index > 0 { - write!(f, ",\n")?; + writeln!(f, ",")?; } write!(f, "{item:?}")?; } @@ -760,7 +760,7 @@ fn parse_string(input: &TokenSlice) -> ParseResult { } } -impl<'a> FromTokens for Identifier { +impl FromTokens for Identifier { fn from_tokens(input: &TokenSlice) -> ParseResult where Self: Sized, @@ -769,7 +769,7 @@ impl<'a> FromTokens for Identifier { } } -impl<'a> FromTokens for String { +impl FromTokens for String { fn from_tokens(input: &TokenSlice) -> ParseResult where Self: Sized, @@ -812,7 +812,7 @@ fn commands() -> &'static [Command] { } static COMMANDS: OnceLock> = OnceLock::new(); - COMMANDS.get_or_init(|| new_commands()).as_slice() + COMMANDS.get_or_init(new_commands).as_slice() } fn parse_command_word(lexer: &mut TokenSlice, s: &mut String, n: usize) -> bool { @@ -852,7 +852,7 @@ fn find_best_match(s: &str) -> (Option<&'static Command>, isize) { fn parse_command_name( lexer: &mut TokenSlice, - error: &Box, + error: &dyn Fn(Diagnostic), ) -> Result<(&'static Command, usize), ()> { let mut s = String::new(); let mut word = 0; @@ -909,7 +909,7 @@ pub fn end_of_command(context: &Context, range: RangeFrom) -> Result, _state: State) { +fn parse_in_state(mut lexer: TokenSlice, error: &dyn Fn(Diagnostic), _state: State) { match lexer.get_token(0) { None | Some(Token::End) => (), _ => match parse_command_name(&mut lexer, error) { @@ -926,12 +926,12 @@ fn parse_in_state(mut lexer: TokenSlice, error: &Box, _state } } -pub fn parse_command(lexer: TokenSlice, error: &Box) { +pub fn parse_command(lexer: TokenSlice, error: &dyn Fn(Diagnostic)) { parse_in_state(lexer, error, State::Initial) } pub struct Context<'a> { - error: &'a Box, + error: &'a dyn Fn(Diagnostic), lexer: TokenSlice, command_name: Option<&'static str>, } diff --git a/rust/pspp/src/dictionary.rs b/rust/pspp/src/dictionary.rs index 139180827e..67d5539ba7 100644 --- a/rust/pspp/src/dictionary.rs +++ b/rust/pspp/src/dictionary.rs @@ -215,7 +215,7 @@ impl Ord for Value { impl Hash for Value { fn hash(&self, state: &mut H) { match self { - Value::Number(number) => number.map(|x| OrderedFloat(x)).hash(state), + Value::Number(number) => number.map(OrderedFloat).hash(state), Value::String(string) => string.hash(state), } } @@ -236,7 +236,7 @@ impl Value { pub fn as_string(&self) -> Option<&[u8]> { match self { Value::Number(_) => None, - Value::String(s) => Some(&*s), + Value::String(s) => Some(s), } } } diff --git a/rust/pspp/src/engine.rs b/rust/pspp/src/engine.rs index 7e8c2fcaa5..aeac1fcc37 100644 --- a/rust/pspp/src/engine.rs +++ b/rust/pspp/src/engine.rs @@ -6,6 +6,7 @@ use crate::{ }; use std::rc::Rc; +#[derive(Default)] pub struct Engine; impl Engine { diff --git a/rust/pspp/src/format/display.rs b/rust/pspp/src/format/display.rs index b7975c62a9..b490e5fe39 100644 --- a/rust/pspp/src/format/display.rs +++ b/rust/pspp/src/format/display.rs @@ -38,7 +38,7 @@ impl Value { } } -impl<'a, 'b> Display for DisplayValue<'a, 'b> { +impl Display for DisplayValue<'_, '_> { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { let number = match self.value { Value::Number(number) => *number, @@ -464,7 +464,7 @@ impl<'a, 'b> DisplayValue<'a, 'b> { 'y' => { let epoch = self.settings.epoch.0; let offset = date.year() - epoch; - if offset < 0 || offset > 99 { + if !(0..=99).contains(&offset) { return self.overflow(f); } write!(&mut output, "{:02}", date.year().abs() % 100).unwrap(); @@ -519,7 +519,7 @@ impl<'a, 'b> DisplayValue<'a, 'b> { } break; } - c if n == 1 => output.push(c as char), + c if n == 1 => output.push(c), _ => unreachable!(), } } @@ -751,7 +751,7 @@ impl Display for Zeros { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { let mut n = self.0; while n > 0 { - static ZEROS: &'static str = "0000000000000000000000000000000000000000"; + static ZEROS: &str = "0000000000000000000000000000000000000000"; let chunk = n.min(ZEROS.len()); f.write_str(&ZEROS[..chunk])?; n -= chunk; @@ -773,7 +773,7 @@ fn output_hex(f: &mut Formatter<'_>, bytes: &[u8]) -> FmtResult { } fn allocate_space(want: usize, capacity: usize, used: &mut usize) -> bool { - if *used + want <= capacity as usize { + if *used + want <= capacity { *used += want; true } else { diff --git a/rust/pspp/src/format/parse.rs b/rust/pspp/src/format/parse.rs index 040951d464..6aa4463c57 100644 --- a/rust/pspp/src/format/parse.rs +++ b/rust/pspp/src/format/parse.rs @@ -277,11 +277,11 @@ impl<'a> ParseValue<'a> { input } - if p.strip_prefix(&*style.prefix.s) { + if p.strip_prefix(&style.prefix.s) { p.strip_ws(); } let sign = p.strip_one_of(&['-', '+']).inspect(|_| p.strip_ws()); - if sign.is_some() && p.strip_prefix(&*style.prefix.s) { + if sign.is_some() && p.strip_prefix(&style.prefix.s) { p.strip_ws(); } let integer = p.advance(strip_integer(p.0, style.grouping.map(char::from))); @@ -300,7 +300,7 @@ impl<'a> ParseValue<'a> { } else { (None, "") }; - if p.strip_prefix(&*style.suffix.s) { + if p.strip_prefix(&style.suffix.s) { p.strip_ws(); } @@ -398,7 +398,7 @@ impl<'a> ParseValue<'a> { fn parse_bcd(input: &[u8]) -> Result { let mut value = 0; - for byte in input.into_iter().copied() { + for byte in input.iter().copied() { let hi = nibble(byte >> 4)?; let lo = nibble(byte & 0x0f)?; value = value * 100 + hi * 10 + lo; @@ -492,7 +492,7 @@ impl<'a> ParseValue<'a> { fn parse_rbhex(&self, input: &str) -> Result { self.parse_hex(input) - .map(|value| Value::Number(value.map(|number| f64::from_bits(number)))) + .map(|value| Value::Number(value.map(f64::from_bits))) } fn parse_date(&self, input: &str) -> Result { @@ -586,7 +586,7 @@ impl<'a> ParseValue<'a> { Ok(Value::Number(Some(time_date))) } - fn parse_minute_second<'b>(&self, p: &mut StrParser<'b>) -> Result { + fn parse_minute_second(&self, p: &mut StrParser<'_>) -> Result { let minute = parse_int::(p)?; if self.format.type_ != Type::MTime && !(0..=59).contains(&minute) { return Err(ParseErrorKind::InvalidMinute(minute)); @@ -641,7 +641,7 @@ enum Sign { Negative, } -fn parse_trailer<'a>(p: &mut StrParser<'a>) -> Result<(), ParseErrorKind> { +fn parse_trailer(p: &mut StrParser<'_>) -> Result<(), ParseErrorKind> { p.strip_ws(); if p.0.is_empty() { Ok(()) @@ -650,7 +650,7 @@ fn parse_trailer<'a>(p: &mut StrParser<'a>) -> Result<(), ParseErrorKind> { } } -fn parse_sign<'a>(p: &mut StrParser<'a>, sign: Option) -> Sign { +fn parse_sign(p: &mut StrParser<'_>, sign: Option) -> Sign { if let Some(sign) = sign { sign } else if p.strip_one_of(&['-', '+']) == Some('-') { @@ -660,7 +660,7 @@ fn parse_sign<'a>(p: &mut StrParser<'a>, sign: Option) -> Sign { } } -fn parse_time<'a>(p: &mut StrParser<'a>) -> Result { +fn parse_time(p: &mut StrParser<'_>) -> Result { let number = parse_int::(p)?; if number < 0 { return Err(ParseErrorKind::DateSyntax); @@ -668,7 +668,7 @@ fn parse_time<'a>(p: &mut StrParser<'a>) -> Result { Ok(number as f64) } -fn parse_day<'a>(p: &mut StrParser<'a>) -> Result { +fn parse_day(p: &mut StrParser<'_>) -> Result { let day = parse_int::(p)?; if (1..=31).contains(&day) { Ok(day) @@ -677,7 +677,7 @@ fn parse_day<'a>(p: &mut StrParser<'a>) -> Result { } } -fn parse_yday<'a>(p: &mut StrParser<'a>) -> Result { +fn parse_yday(p: &mut StrParser<'_>) -> Result { let Some(s) = p.0.get(..3) else { return Err(ParseErrorKind::InvalidYDayLen); }; @@ -692,7 +692,7 @@ fn parse_yday<'a>(p: &mut StrParser<'a>) -> Result { Ok(yday) } -fn parse_month<'a>(p: &mut StrParser<'a>) -> Result { +fn parse_month(p: &mut StrParser<'_>) -> Result { if p.0.starts_with(|c: char| c.is_ascii_digit()) { let month = parse_int(p)?; if (1..=12).contains(&month) { @@ -718,7 +718,7 @@ fn parse_month<'a>(p: &mut StrParser<'a>) -> Result { Err(ParseErrorKind::InvalidMonth) } -fn parse_weekday<'a>(p: &mut StrParser<'a>) -> Result { +fn parse_weekday(p: &mut StrParser<'_>) -> Result { static WEEKDAY_NAMES: [&str; 7] = ["su", "mo", "tu", "we", "th", "fr", "sa"]; let name = p.strip_matches(|c| c.is_ascii_alphabetic()); name.get(..2) @@ -726,21 +726,21 @@ fn parse_weekday<'a>(p: &mut StrParser<'a>) -> Result { .ok_or(ParseErrorKind::InvalidWeekdayName) } -fn parse_quarter<'a>(p: &mut StrParser<'a>) -> Result { +fn parse_quarter(p: &mut StrParser<'_>) -> Result { match parse_int(p)? { quarter @ 1..=4 => Ok((quarter - 1) * 3 + 1), other => Err(ParseErrorKind::InvalidQuarter(other)), } } -fn parse_week<'a>(p: &mut StrParser<'a>) -> Result { +fn parse_week(p: &mut StrParser<'_>) -> Result { match parse_int(p)? { week @ 1..=53 => Ok((week - 1) * 7 + 1), other => Err(ParseErrorKind::InvalidWeek(other)), } } -fn parse_time_delimiter<'a>(p: &mut StrParser<'a>) -> Result<(), ParseErrorKind> { +fn parse_time_delimiter(p: &mut StrParser<'_>) -> Result<(), ParseErrorKind> { let delimiter = p.strip_matches(|c| c == ':' || c.is_ascii_whitespace()); if !delimiter.is_empty() { Ok(()) @@ -749,7 +749,7 @@ fn parse_time_delimiter<'a>(p: &mut StrParser<'a>) -> Result<(), ParseErrorKind> } } -fn parse_date_delimiter<'a>(p: &mut StrParser<'a>) -> Result<(), ParseErrorKind> { +fn parse_date_delimiter(p: &mut StrParser<'_>) -> Result<(), ParseErrorKind> { let delimiter = p .strip_matches(|c| c == '-' || c == '/' || c == '.' || c == ',' || c.is_ascii_whitespace()); if !delimiter.is_empty() { @@ -768,8 +768,8 @@ fn match_name(name: &str, candidates: &[&str]) -> Option { None } -fn parse_year<'a>( - p: &mut StrParser<'a>, +fn parse_year( + p: &mut StrParser<'_>, settings: &Settings, max_digits: usize, ) -> Result { @@ -787,7 +787,7 @@ fn parse_year<'a>( Ok(settings.epoch.apply(year)) } -fn parse_int<'a, T>(p: &mut StrParser<'a>) -> Result +fn parse_int<'a, T>(p: &mut StrParser<'_>) -> Result where T: FromStr, { @@ -1073,11 +1073,11 @@ mod test { } 'm' => { let m = self.date.month as usize - 1; - static ROMAN: [&'static str; 12] = [ + static ROMAN: [&str; 12] = [ "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii", ]; - static ENGLISH: [&'static str; 12] = [ + static ENGLISH: [&str; 12] = [ "january", "february", "march", diff --git a/rust/pspp/src/lex/command_name.rs b/rust/pspp/src/lex/command_name.rs index a954bee6a2..0e9957d4b3 100644 --- a/rust/pspp/src/lex/command_name.rs +++ b/rust/pspp/src/lex/command_name.rs @@ -107,7 +107,7 @@ impl<'a, T> CommandMatcher<'a, T> { } } -pub const COMMAND_NAMES: &'static [&'static str] = &[ +pub const COMMAND_NAMES: &[&str] = &[ "2SLS", "ACF", "ADD DOCUMENT", diff --git a/rust/pspp/src/macros.rs b/rust/pspp/src/macros.rs index bcd223c151..57abc4ec36 100644 --- a/rust/pspp/src/macros.rs +++ b/rust/pspp/src/macros.rs @@ -576,12 +576,10 @@ impl<'a> Parser<'a> { ParserState::Arg }; } + } else if self.args.iter().any(|arg| arg.is_none()) { + self.state = ParserState::Keyword; } else { - if self.args.iter().any(|arg| arg.is_none()) { - self.state = ParserState::Keyword; - } else { - self.finished(); - } + self.finished(); } } } diff --git a/rust/pspp/src/message.rs b/rust/pspp/src/message.rs index 5b99802bf5..8942223a32 100644 --- a/rust/pspp/src/message.rs +++ b/rust/pspp/src/message.rs @@ -75,18 +75,11 @@ impl Display for Location { } let l1 = span.start.line; let l2 = span.end.line; - if let (Some(c1), Some(c2)) = (span.start.column, span.end.column) { - if l2 > l1 { - write!(f, "{l1}.{c1}-{l2}.{}", c2 - 1)?; - } else { - write!(f, "{l1}.{c1}-{}", c2 - 1)?; - } - } else { - if l2 > l1 { - write!(f, "{l1}-{l2}")?; - } else { - write!(f, "{l1}")?; - } + match (span.start.column.zip(span.end.column), l2 > l1) { + (Some((c1, c2)), true) => write!(f, "{l1}.{c1}-{l2}.{}", c2 - 1)?, + (Some((c1, c2)), false) => write!(f, "{l1}.{c1}-{}", c2 - 1)?, + (None, true) => write!(f, "{l1}-{l2}")?, + (None, false) => write!(f, "{l1}")?, } } Ok(()) diff --git a/rust/pspp/src/output/pivot/mod.rs b/rust/pspp/src/output/pivot/mod.rs index 8e315100b8..7da87d5153 100644 --- a/rust/pspp/src/output/pivot/mod.rs +++ b/rust/pspp/src/output/pivot/mod.rs @@ -403,7 +403,7 @@ impl DimensionBuilder { self.hide_all_labels = true; self } - fn build(mut self) -> Dimension { + fn build(self) -> Dimension { let mut leaves = Vec::with_capacity(self.len); let root = self.root.build(None, &mut leaves); Dimension { @@ -443,10 +443,10 @@ impl GroupBuilder { self.push(value); self } - pub fn with_label_hidden(mut self) -> Self { + pub fn with_label_hidden(self) -> Self { self.with_show_label(false) } - pub fn with_label_shown(mut self) -> Self { + pub fn with_label_shown(self) -> Self { self.with_show_label(true) } pub fn with_show_label(mut self, show_label: bool) -> Self { @@ -562,15 +562,8 @@ impl PivotTableBuilder { dimensions: Vec::with_capacity(self.dimensions.len()), }); for d in self.dimensions { - let axis = d.axis; - let label_position = if axis == Axis3::Y && table.corner_text.is_none() { - self.look.row_label_position - } else { - LabelPosition::Nested - }; - let d = d.build(); - axes[axis].dimensions.push(dimensions.len()); - dimensions.push(d); + axes[d.axis].dimensions.push(dimensions.len()); + dimensions.push(d.build()); } table.dimensions = dimensions; table.axes = axes; diff --git a/rust/pspp/src/output/text.rs b/rust/pspp/src/output/text.rs index 6d6bbcbcc7..226e92d036 100644 --- a/rust/pspp/src/output/text.rs +++ b/rust/pspp/src/output/text.rs @@ -46,7 +46,7 @@ impl TextRenderer { emphasis: true, width, min_hbreak: 20, - box_chars: &*&UNICODE_BOX, + box_chars: &*UNICODE_BOX, n_objects: 0, params: Params { size: Coord2::new(width, usize::MAX),