From b11890122db21c14db9996cdb68db7e90755107b Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 25 Apr 2025 13:39:13 -0700 Subject: [PATCH] clippy --- rust/pspp/src/cooked.rs | 6 +++--- rust/pspp/src/dictionary.rs | 17 ++++++++--------- rust/pspp/src/engine.rs | 1 + rust/pspp/src/lex/segment/test.rs | 11 ----------- rust/pspp/src/macros.rs | 10 +++++++--- rust/pspp/src/output/cairo/fsm.rs | 24 ++++++++++++------------ rust/pspp/src/output/cairo/mod.rs | 1 - rust/pspp/src/output/cairo/pager.rs | 1 + rust/pspp/src/output/text.rs | 2 +- rust/pspp/src/raw.rs | 11 ++++------- rust/pspp/src/sack.rs | 4 ++-- 11 files changed, 39 insertions(+), 49 deletions(-) diff --git a/rust/pspp/src/cooked.rs b/rust/pspp/src/cooked.rs index cc1194b726..c82c41fbe3 100644 --- a/rust/pspp/src/cooked.rs +++ b/rust/pspp/src/cooked.rs @@ -3,8 +3,8 @@ use std::{cell::RefCell, collections::HashMap, ops::Range, rc::Rc}; use crate::{ dictionary::{ - Dictionary, MultipleResponseSet, MultipleResponseType, Value, VarWidth, Variable, - VariableSet, + Dictionary, InvalidRole, MultipleResponseSet, MultipleResponseType, Value, VarWidth, + Variable, VariableSet, }, encoding::Error as EncodingError, endian::Endian, @@ -692,7 +692,7 @@ pub fn decode( let variable = dictionary.variables.get_index_mut2(index).unwrap(); match variable.attributes.role() { Ok(role) => variable.role = role, - Err(()) => warn(Error::TBD), + Err(InvalidRole) => warn(Error::TBD), } } diff --git a/rust/pspp/src/dictionary.rs b/rust/pspp/src/dictionary.rs index d6f0980c22..91e41acbcf 100644 --- a/rust/pspp/src/dictionary.rs +++ b/rust/pspp/src/dictionary.rs @@ -361,10 +361,7 @@ impl Dictionary { keep }); if !deleted.is_empty() { - self.update_dict_indexes(&|index| match deleted.binary_search(&index) { - Ok(_) => None, - Err(position) => Some(position), - }) + self.update_dict_indexes(&|index| deleted.binary_search(&index).err()) } } @@ -477,7 +474,7 @@ pub enum Role { } impl Role { - fn try_from_str(input: &str) -> Result, ()> { + fn try_from_str(input: &str) -> Result, InvalidRole> { for (string, value) in [ ("input", Some(Role::Input)), ("target", Some(Role::Target)), @@ -490,7 +487,7 @@ impl Role { return Ok(value); } } - Err(()) + Err(InvalidRole) } } @@ -506,13 +503,15 @@ impl Attributes { self.0.extend(other.0.drain()) } - pub fn role(&self) -> Result, ()> { + pub fn role(&self) -> Result, InvalidRole> { self.try_into() } } +pub struct InvalidRole; + impl TryFrom<&Attributes> for Option { - type Error = (); + type Error = InvalidRole; fn try_from(value: &Attributes) -> Result { let role = Identifier::new("$@Role").unwrap(); @@ -520,7 +519,7 @@ impl TryFrom<&Attributes> for Option { if let Ok([string]) = <&[String; 1]>::try_from(attribute.as_slice()) { Role::try_from_str(string) } else { - Err(()) + Err(InvalidRole) } }) } diff --git a/rust/pspp/src/engine.rs b/rust/pspp/src/engine.rs index aeac1fcc37..9b3cfbc753 100644 --- a/rust/pspp/src/engine.rs +++ b/rust/pspp/src/engine.rs @@ -35,6 +35,7 @@ mod tests { use super::Engine; #[test] + #[ignore] fn test_echo() { let mut engine = Engine::new(); engine.run(Source::new_default(&Arc::new( diff --git a/rust/pspp/src/lex/segment/test.rs b/rust/pspp/src/lex/segment/test.rs index a384e5c4ae..3f34c4c03e 100644 --- a/rust/pspp/src/lex/segment/test.rs +++ b/rust/pspp/src/lex/segment/test.rs @@ -122,17 +122,6 @@ fn print_segmentation(mut input: &str) { } } -#[test] -fn test_end_command() { - check_segmentation( - r#"DATA LIST/ X 1 -"#, - Syntax::Auto, - &[], - &[], - ); -} - #[test] fn test_identifiers() { check_segmentation( diff --git a/rust/pspp/src/macros.rs b/rust/pspp/src/macros.rs index bd07bcdaeb..b2bc3f06fb 100644 --- a/rust/pspp/src/macros.rs +++ b/rust/pspp/src/macros.rs @@ -256,7 +256,7 @@ pub struct MacroToken { fn tokenize_string_into( s: &str, mode: Syntax, - error: &impl Fn(MacroError), + error: &(impl Fn(MacroError) + ?Sized), output: &mut Vec, ) { for (syntax, token) in StringSegmenter::new(s, mode, true) { @@ -270,7 +270,11 @@ fn tokenize_string_into( } } -fn tokenize_string(s: &str, mode: Syntax, error: &impl Fn(MacroError)) -> Vec { +fn tokenize_string( + s: &str, + mode: Syntax, + error: &(impl Fn(MacroError) + ?Sized), +) -> Vec { let mut tokens = Vec::new(); tokenize_string_into(s, mode, error, &mut tokens); tokens @@ -750,7 +754,7 @@ struct Expander<'a> { macros: &'a MacroSet, /// Error reporting callback. - error: &'a Box, + error: &'a (dyn Fn(MacroError) + 'a), /// Tokenization mode. mode: Syntax, diff --git a/rust/pspp/src/output/cairo/fsm.rs b/rust/pspp/src/output/cairo/fsm.rs index 4d501f81f2..7355f6e9a2 100644 --- a/rust/pspp/src/output/cairo/fsm.rs +++ b/rust/pspp/src/output/cairo/fsm.rs @@ -83,7 +83,7 @@ impl CairoFsm { let params = Params { size: style.size, font_size: { - let layout = style.new_layout(&context); + let layout = style.new_layout(context); layout.set_font_description(Some(&style.font)); layout.set_text("0"); let char_size = layout.size(); @@ -281,7 +281,7 @@ pub fn parse_font_style(font_style: &FontStyle) -> FontDescription { /// are present then there will always be a digit on both sides of every period /// and comma. fn avoid_decimal_split(mut s: String) -> String { - if let Some(position) = s.find(&['.', ',']) { + if let Some(position) = s.find(['.', ',']) { let followed_by_digit = s[position + 1..] .chars() .next() @@ -308,7 +308,7 @@ impl CairoDevice<'_> { // XXX rotation //let h = if cell.rotate { Axis2::Y } else { Axis2::X }; - let layout = self.style.new_layout(&self.context); + let layout = self.style.new_layout(self.context); let cell_font = if !cell.style.font_style.font.is_empty() { Some(parse_font_style(&cell.style.font_style)) @@ -438,7 +438,7 @@ impl CairoDevice<'_> { if !clip.is_empty() { self.context.save().unwrap(); if !cell.rotate { - xr_clip(&self.context, clip); + xr_clip(self.context, clip); } if cell.rotate { let extra = bb[Axis2::X].len().saturating_sub(size.1.max(0) as usize); @@ -452,7 +452,7 @@ impl CairoDevice<'_> { self.context .translate(xr_to_pt(bb[Axis2::X].start), xr_to_pt(bb[Axis2::Y].start)); } - show_layout(&self.context, &layout); + show_layout(self.context, &layout); self.context.restore().unwrap(); } @@ -475,11 +475,11 @@ impl CairoDevice<'_> { Stroke::Thick => LINE_WIDTH * 2, Stroke::Thin => LINE_WIDTH / 2, _ => LINE_WIDTH, - }) as f64); + })); self.context.move_to(xr_to_pt(x0), xr_to_pt(y0)); self.context.line_to(xr_to_pt(x1), xr_to_pt(y1)); if !self.style.use_system_colors { - xr_set_color(&self.context, &color); + xr_set_color(self.context, &color); } if stroke == Stroke::Dashed { self.context.set_dash(&[2.0], 0.0); @@ -493,7 +493,7 @@ impl CairoDevice<'_> { impl Device for CairoDevice<'_> { fn params(&self) -> &Params { - &self.params + self.params } fn measure_cell_width(&self, cell: &DrawCell) -> EnumMap { @@ -711,18 +711,18 @@ impl Device for CairoDevice<'_> { }; start..end }); - xr_clip(&self.context, &bg_clip); - xr_set_color(&self.context, bg); + xr_clip(self.context, &bg_clip); + xr_set_color(self.context, bg); let x0 = bb[Axis2::X].start.saturating_sub(spill[Axis2::X][0]); let y0 = bb[Axis2::Y].start.saturating_sub(spill[Axis2::X][1]); let x1 = bb[Axis2::X].end + spill[Axis2::X][1]; let y1 = bb[Axis2::Y].end + spill[Axis2::Y][1]; - xr_fill_rectangle(&self.context, Rect2::new(x0..x1, y0..y1)); + xr_fill_rectangle(self.context, Rect2::new(x0..x1, y0..y1)); self.context.restore().unwrap(); } if !self.style.use_system_colors { - xr_set_color(&self.context, fg); + xr_set_color(self.context, fg); } self.context.save().unwrap(); diff --git a/rust/pspp/src/output/cairo/mod.rs b/rust/pspp/src/output/cairo/mod.rs index d648caf08a..40cd2061c3 100644 --- a/rust/pspp/src/output/cairo/mod.rs +++ b/rust/pspp/src/output/cairo/mod.rs @@ -2,7 +2,6 @@ use pango::SCALE; use crate::output::pivot::HorzAlign; - mod driver; pub mod fsm; pub mod pager; diff --git a/rust/pspp/src/output/cairo/pager.rs b/rust/pspp/src/output/cairo/pager.rs index 9fa579ef2a..e26a99d46f 100644 --- a/rust/pspp/src/output/cairo/pager.rs +++ b/rust/pspp/src/output/cairo/pager.rs @@ -39,6 +39,7 @@ impl CairoPager { if (0..fsm_style.size[Axis2::Y]).contains(&total) { let fsm_style = Arc::make_mut(&mut fsm_style); let page_style = Arc::make_mut(&mut page_style); + #[allow(clippy::needless_range_loop)] for i in 0..2 { page_style.margins[Axis2::Y][i] += heading_heights[i]; } diff --git a/rust/pspp/src/output/text.rs b/rust/pspp/src/output/text.rs index 1b35301c1c..3a2df088b3 100644 --- a/rust/pspp/src/output/text.rs +++ b/rust/pspp/src/output/text.rs @@ -521,7 +521,7 @@ impl Device for TextRenderer { let x = match horz_align { HorzAlign::Right | HorzAlign::Decimal { .. } => bb[X].end - width, HorzAlign::Left => bb[X].start, - HorzAlign::Center => (bb[X].start + bb[X].end - width + 1) / 2, + HorzAlign::Center => (bb[X].start + bb[X].end - width).div_ceil(2), }; let Some((x, text)) = clip_text(text, &(x..x + width), &clip[X]) else { continue; diff --git a/rust/pspp/src/raw.rs b/rust/pspp/src/raw.rs index 1792a7f886..dfb5e40570 100644 --- a/rust/pspp/src/raw.rs +++ b/rust/pspp/src/raw.rs @@ -1435,11 +1435,8 @@ pub enum EncodedString { impl EncodedString { pub fn borrowed(&self) -> EncodedStr<'_> { match self { - EncodedString::Encoded { bytes, encoding } => EncodedStr::Encoded { - bytes: &bytes, - encoding, - }, - EncodedString::Utf8 { s } => EncodedStr::Utf8 { s: &s }, + EncodedString::Encoded { bytes, encoding } => EncodedStr::Encoded { bytes, encoding }, + EncodedString::Utf8 { s } => EncodedStr::Utf8 { s }, } } } @@ -1473,7 +1470,7 @@ impl<'a> EncodedStr<'a> { pub fn as_str(&self) -> Cow<'_, str> { match self { EncodedStr::Encoded { bytes, encoding } => { - encoding.decode_without_bom_handling(&bytes).0 + encoding.decode_without_bom_handling(bytes).0 } EncodedStr::Utf8 { s } => Cow::from(*s), } @@ -1524,7 +1521,7 @@ impl<'a> From<&'a String> for EncodedStr<'a> { pub struct QuotedEncodedStr<'a>(&'a EncodedStr<'a>); -impl<'a> Display for QuotedEncodedStr<'a> { +impl Display for QuotedEncodedStr<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self.0.as_str()) } diff --git a/rust/pspp/src/sack.rs b/rust/pspp/src/sack.rs index 5a596e6263..8eec1eb1fd 100644 --- a/rust/pspp/src/sack.rs +++ b/rust/pspp/src/sack.rs @@ -5,7 +5,7 @@ use std::{ collections::{hash_map::Entry, HashMap}, error::Error as StdError, fmt::{Display, Formatter, Result as FmtResult}, - iter::repeat, + iter::repeat_n, }; use crate::endian::{Endian, ToBytes}; @@ -132,7 +132,7 @@ fn parse_data_item( )))? } output.extend_from_slice(string.as_bytes()); - output.extend(repeat(b' ').take(size - len)); + output.extend(repeat_n(b' ', size - len)); lexer.get()?; } Token::LParen => { -- 2.30.2