From: Ben Pfaff Date: Thu, 10 Apr 2025 20:10:17 +0000 (-0700) Subject: clippy X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0ceabb2e15f0a20ed5c0929be7213c4415bdfec3;p=pspp clippy --- diff --git a/rust/pspp/src/output/pivot/mod.rs b/rust/pspp/src/output/pivot/mod.rs index 1fe7e56eaa..5cbc719126 100644 --- a/rust/pspp/src/output/pivot/mod.rs +++ b/rust/pspp/src/output/pivot/mod.rs @@ -1923,7 +1923,7 @@ impl Display for DisplayValue<'_> { f.write_str(local) } - ValueInner::Template { args, local, .. } => self.template(f, &local, args), + ValueInner::Template { args, local, .. } => self.template(f, local, args), ValueInner::Empty => Ok(()), }?; diff --git a/rust/pspp/src/output/render.rs b/rust/pspp/src/output/render.rs index e4cd3ec3d6..8d2e518c7e 100644 --- a/rust/pspp/src/output/render.rs +++ b/rust/pspp/src/output/render.rs @@ -373,7 +373,7 @@ impl Page { fn new(table: Arc, device: &dyn Device, min_width: usize, look: &Look) -> Self { use Axis2::*; - let n = table.n.clone(); + let n = table.n; // Figure out rule widths. // @@ -381,7 +381,7 @@ impl Page { // `rules[Y]` is horizontal rules. let rules = EnumMap::from_fn(|axis| { (0..=n[axis]) - .map(|z| measure_rule(device, &*table, axis, z)) + .map(|z| measure_rule(device, &table, axis, z)) .collect::>() }); @@ -395,7 +395,7 @@ impl Page { // multiple columns. let mut unspanned_columns = [vec![0; n.x()], vec![0; n.x()]]; for cell in table.cells().filter(|cell| cell.col_span() == 1) { - let mut w = device.measure_cell_width(&DrawCell::new(cell.inner(), &*table)); + let mut w = device.measure_cell_width(&DrawCell::new(cell.inner(), &table)); if device.params().px_size.is_some() { if let Some(region) = table.heading_region(cell.coord) { let wr = &heading_widths[region]; @@ -1441,7 +1441,7 @@ impl Pager { .and_then(|x_break| { x_break.next( device, - (device.params().size[Axis2::X] as f64 / self.scale as f64) as usize, + (device.params().size[Axis2::X] as f64 / self.scale) as usize, ) }) .map(|page| Break::new(page, Axis2::Y)); diff --git a/rust/pspp/src/output/text.rs b/rust/pspp/src/output/text.rs index 6489fd2a60..31118aad69 100644 --- a/rust/pspp/src/output/text.rs +++ b/rust/pspp/src/output/text.rs @@ -433,53 +433,6 @@ fn new_line_breaks( } } -#[cfg(test)] -mod test { - use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; - - use crate::output::text::new_line_breaks; - - #[test] - fn unicode_width() { - // `\n` is a control character, so [UnicodeWidthChar] considers it to - // have no width. - assert_eq!('\n'.width(), None); - - // But [UnicodeWidthStr] has a different idea. - assert_eq!("\n".width(), 1); - assert_eq!("\r\n".width(), 1); - } - - #[track_caller] - fn test_line_breaks(input: &str, width: usize, expected: Vec<&str>) { - let actual = new_line_breaks(input, width).collect::>(); - if expected != actual { - panic!("filling {input:?} to {width} columns:\nexpected: {expected:?}\nactual: {actual:?}"); - } - } - #[test] - fn line_breaks() { - for width in 0..=6 { - test_line_breaks("abc def ghi", width, vec!["abc", "def", "ghi"]); - } - for width in 7..=10 { - test_line_breaks("abc def ghi", width, vec!["abc def", "ghi"]); - } - test_line_breaks("abc def ghi", 11, vec!["abc def ghi"]); - - for width in 0..=6 { - test_line_breaks("abc def ghi", width, vec!["abc", "def", "ghi"]); - } - test_line_breaks("abc def ghi", 7, vec!["abc", "def ghi"]); - for width in 8..=11 { - test_line_breaks("abc def ghi", width, vec!["abc def", "ghi"]); - } - test_line_breaks("abc def ghi", 12, vec!["abc def ghi"]); - - test_line_breaks("abc\ndef\nghi", 2, vec!["abc", "def", "ghi"]); - } -} - impl Driver for TextDriver { fn name(&self) -> Cow<'static, str> { Cow::from("text") @@ -574,7 +527,7 @@ impl Device for TextRenderer { HorzAlign::Left => bb[X].start, HorzAlign::Center => (bb[X].start + bb[X].end - width + 1) / 2, }; - let Some((x, text)) = clip_text(&text, &(x..x + width), &clip[X]) else { + let Some((x, text)) = clip_text(text, &(x..x + width), &clip[X]) else { continue; }; @@ -591,3 +544,50 @@ impl Device for TextRenderer { unimplemented!() } } + +#[cfg(test)] +mod test { + use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; + + use crate::output::text::new_line_breaks; + + #[test] + fn unicode_width() { + // `\n` is a control character, so [UnicodeWidthChar] considers it to + // have no width. + assert_eq!('\n'.width(), None); + + // But [UnicodeWidthStr] has a different idea. + assert_eq!("\n".width(), 1); + assert_eq!("\r\n".width(), 1); + } + + #[track_caller] + fn test_line_breaks(input: &str, width: usize, expected: Vec<&str>) { + let actual = new_line_breaks(input, width).collect::>(); + if expected != actual { + panic!("filling {input:?} to {width} columns:\nexpected: {expected:?}\nactual: {actual:?}"); + } + } + #[test] + fn line_breaks() { + for width in 0..=6 { + test_line_breaks("abc def ghi", width, vec!["abc", "def", "ghi"]); + } + for width in 7..=10 { + test_line_breaks("abc def ghi", width, vec!["abc def", "ghi"]); + } + test_line_breaks("abc def ghi", 11, vec!["abc def ghi"]); + + for width in 0..=6 { + test_line_breaks("abc def ghi", width, vec!["abc", "def", "ghi"]); + } + test_line_breaks("abc def ghi", 7, vec!["abc", "def ghi"]); + for width in 8..=11 { + test_line_breaks("abc def ghi", width, vec!["abc def", "ghi"]); + } + test_line_breaks("abc def ghi", 12, vec!["abc def ghi"]); + + test_line_breaks("abc\ndef\nghi", 2, vec!["abc", "def", "ghi"]); + } +} diff --git a/rust/pspp/src/output/text_line.rs b/rust/pspp/src/output/text_line.rs index 00cd44c7d7..d28dcc3417 100644 --- a/rust/pspp/src/output/text_line.rs +++ b/rust/pspp/src/output/text_line.rs @@ -282,6 +282,37 @@ impl Debug for Emphasis { } } +pub fn clip_text<'a>( + text: &'a str, + bb: &Range, + clip: &Range, +) -> Option<(usize, &'a str)> { + let mut x = bb.start; + let mut width = bb.len(); + + let mut iter = text.chars(); + while x < clip.start { + let c = iter.next()?; + if let Some(w) = c.width() { + x += w; + width = width.checked_sub(w)?; + } + } + if x + width > clip.end { + if x >= clip.end { + return None; + } + + while x + width > clip.end { + let c = iter.next_back()?; + if let Some(w) = c.width() { + width = width.checked_sub(w)?; + } + } + } + Some((x, iter.as_str())) +} + #[cfg(test)] mod test { use super::{Emphasis, TextLine}; @@ -561,34 +592,3 @@ mod test { } } } - -pub fn clip_text<'a>( - text: &'a str, - bb: &Range, - clip: &Range, -) -> Option<(usize, &'a str)> { - let mut x = bb.start; - let mut width = bb.len(); - - let mut iter = text.chars(); - while x < clip.start { - let c = iter.next()?; - if let Some(w) = c.width() { - x += w; - width = width.checked_sub(w)?; - } - } - if x + width > clip.end { - if x >= clip.end { - return None; - } - - while x + width > clip.end { - let c = iter.next_back()?; - if let Some(w) = c.width() { - width = width.checked_sub(w)?; - } - } - } - Some((x, iter.as_str())) -} diff --git a/rust/pspp/src/sack.rs b/rust/pspp/src/sack.rs index c6be5d1eef..5a596e6263 100644 --- a/rust/pspp/src/sack.rs +++ b/rust/pspp/src/sack.rs @@ -108,8 +108,8 @@ fn parse_data_item( } else { Err(lexer.error(format!( "{integer} is not in the valid range [{},{}]", - i32::min_value(), - u32::max_value() + i32::MIN, + u32::MAX )))?; }; } diff --git a/rust/pspp/src/settings.rs b/rust/pspp/src/settings.rs index 6bb33d7d13..6aad340605 100644 --- a/rust/pspp/src/settings.rs +++ b/rust/pspp/src/settings.rs @@ -128,7 +128,7 @@ impl Default for Settings { impl Settings { pub fn global() -> &'static Settings { static GLOBAL: OnceLock = OnceLock::new(); - &GLOBAL.get_or_init(|| Settings::default()) + GLOBAL.get_or_init( Settings::default) } }