From 5e5b1bba510179a1c50f34b3a239f1913e7be78d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Barv=C3=AD=C5=99?= Date: Sat, 1 Apr 2023 15:53:13 +0200 Subject: [PATCH] Needless clone, borrows, casts and lifetimes (#504) --- cli/src/main.rs | 6 +++--- library/src/compute/calc.rs | 2 +- library/src/layout/flow.rs | 4 ++-- library/src/layout/fragment.rs | 5 +++++ library/src/layout/grid.rs | 2 +- library/src/layout/list.rs | 2 +- library/src/layout/mod.rs | 10 +++++----- library/src/layout/par.rs | 9 +++++---- library/src/layout/stack.rs | 2 +- library/src/math/ctx.rs | 4 ++-- library/src/math/mod.rs | 2 +- library/src/meta/bibliography.rs | 6 +++--- library/src/meta/outline.rs | 2 +- library/src/meta/reference.rs | 2 +- library/src/symbols/emoji.rs | 2 +- library/src/symbols/sym.rs | 2 +- library/src/text/shaping.rs | 2 +- library/src/text/shift.rs | 2 +- library/src/visualize/polygon.rs | 2 +- 19 files changed, 37 insertions(+), 31 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 95bafa656..e8b08bdb3 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -279,7 +279,7 @@ fn compile_once(world: &mut SystemWorld, command: &CompileSettings) -> StrResult // Print diagnostics. Err(errors) => { status(command, Status::Error).unwrap(); - print_diagnostics(&world, *errors) + print_diagnostics(world, *errors) .map_err(|_| "failed to print diagnostics")?; Ok(true) } @@ -576,10 +576,10 @@ impl PathHash { /// Read a file. fn read(path: &Path) -> FileResult> { let f = |e| FileError::from_io(e, path); - if fs::metadata(&path).map_err(f)?.is_dir() { + if fs::metadata(path).map_err(f)?.is_dir() { Err(FileError::IsDirectory) } else { - fs::read(&path).map_err(f) + fs::read(path).map_err(f) } } diff --git a/library/src/compute/calc.rs b/library/src/compute/calc.rs index d98d09b2b..bb5d90028 100644 --- a/library/src/compute/calc.rs +++ b/library/src/compute/calc.rs @@ -457,7 +457,7 @@ pub fn round( Num::Int(n) if digits == 0 => Value::Int(n), _ => { let n = value.float(); - let factor = 10.0_f64.powi(digits as i32) as f64; + let factor = 10.0_f64.powi(digits as i32); Value::Float((n * factor).round() / factor) } } diff --git a/library/src/layout/flow.rs b/library/src/layout/flow.rs index da2ddba69..cd7b1f606 100644 --- a/library/src/layout/flow.rs +++ b/library/src/layout/flow.rs @@ -30,7 +30,7 @@ impl Layout for FlowElem { let mut styles = styles; if let Some((elem, map)) = child.to_styled() { child = elem; - styles = outer.chain(&map); + styles = outer.chain(map); } if let Some(elem) = child.to::() { @@ -54,7 +54,7 @@ impl Layout for FlowElem { true, )); } else if child.can::() { - layouter.layout_multiple(vt, &child, styles)?; + layouter.layout_multiple(vt, child, styles)?; } else if child.is::() { if !layouter.regions.backlog.is_empty() || layouter.regions.last.is_some() { diff --git a/library/src/layout/fragment.rs b/library/src/layout/fragment.rs index d1f6b524e..3550df2a1 100644 --- a/library/src/layout/fragment.rs +++ b/library/src/layout/fragment.rs @@ -15,6 +15,11 @@ impl Fragment { Self(frames) } + /// Return `true` if the length is 0. + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + /// The number of frames in the fragment. pub fn len(&self) -> usize { self.0.len() diff --git a/library/src/layout/grid.rs b/library/src/layout/grid.rs index e0c8b6841..01bf7e30c 100644 --- a/library/src/layout/grid.rs +++ b/library/src/layout/grid.rs @@ -267,7 +267,7 @@ impl<'a, 'v> GridLayouter<'a, 'v> { // We use these regions for auto row measurement. Since at that moment, // columns are already sized, we can enable horizontal expansion. - let mut regions = regions.clone(); + let mut regions = regions; regions.expand = Axes::new(true, false); Self { diff --git a/library/src/layout/list.rs b/library/src/layout/list.rs index 9f8d5a92e..6cb1bc7e6 100644 --- a/library/src/layout/list.rs +++ b/library/src/layout/list.rs @@ -197,7 +197,7 @@ cast_from_value! { ListMarker, v: Content => Self::Content(vec![v]), array: Array => { - if array.len() == 0 { + if array.is_empty() { Err("array must contain at least one marker")?; } Self::Content(array.into_iter().map(Value::display).collect()) diff --git a/library/src/layout/mod.rs b/library/src/layout/mod.rs index 1e4377233..e93b2e028 100644 --- a/library/src/layout/mod.rs +++ b/library/src/layout/mod.rs @@ -172,7 +172,7 @@ fn realize_root<'a>( return Ok((content.clone(), styles)); } - let mut builder = Builder::new(vt, &scratch, true); + let mut builder = Builder::new(vt, scratch, true); builder.accept(content, styles)?; builder.interrupt_page(Some(styles))?; let (pages, shared) = builder.doc.unwrap().pages.finish(); @@ -197,7 +197,7 @@ fn realize_block<'a>( return Ok((content.clone(), styles)); } - let mut builder = Builder::new(vt, &scratch, false); + let mut builder = Builder::new(vt, scratch, false); builder.accept(content, styles)?; builder.interrupt_par()?; let (children, shared) = builder.flow.0.finish(); @@ -314,7 +314,7 @@ impl<'a, 'v, 't> Builder<'a, 'v, 't> { ) -> SourceResult<()> { let stored = self.scratch.styles.alloc(styles); let styles = stored.chain(map); - self.interrupt_style(&map, None)?; + self.interrupt_style(map, None)?; self.accept(elem, styles)?; self.interrupt_style(map, Some(styles))?; Ok(()) @@ -468,9 +468,9 @@ impl<'a> FlowBuilder<'a> { let above = BlockElem::above_in(styles); let below = BlockElem::below_in(styles); - self.0.push(above.clone().pack(), styles); + self.0.push(above.pack(), styles); self.0.push(content.clone(), styles); - self.0.push(below.clone().pack(), styles); + self.0.push(below.pack(), styles); return true; } diff --git a/library/src/layout/par.rs b/library/src/layout/par.rs index dc445c86b..7e43b43f7 100644 --- a/library/src/layout/par.rs +++ b/library/src/layout/par.rs @@ -365,12 +365,13 @@ impl<'a> Item<'a> { } /// Maps byte offsets back to spans. +#[derive(Default)] pub struct SpanMapper(Vec<(usize, Span)>); impl SpanMapper { /// Create a new span mapper. pub fn new() -> Self { - Self(vec![]) + Self::default() } /// Push a span for a segment with the given length. @@ -745,8 +746,8 @@ fn is_compatible(a: Script, b: Script) -> bool { /// Get a style property, but only if it is the same for all children of the /// paragraph. -fn shared_get<'a, T: PartialEq>( - styles: StyleChain<'a>, +fn shared_get( + styles: StyleChain<'_>, children: &[Content], getter: fn(StyleChain) -> T, ) -> Option { @@ -754,7 +755,7 @@ fn shared_get<'a, T: PartialEq>( children .iter() .filter_map(|child| child.to_styled()) - .all(|(_, local)| getter(styles.chain(&local)) == value) + .all(|(_, local)| getter(styles.chain(local)) == value) .then(|| value) } diff --git a/library/src/layout/stack.rs b/library/src/layout/stack.rs index c85eda2d4..8146114a3 100644 --- a/library/src/layout/stack.rs +++ b/library/src/layout/stack.rs @@ -198,7 +198,7 @@ impl<'a> StackLayouter<'a> { let aligns = if let Some(align) = block.to::() { align.alignment(styles) } else if let Some((_, local)) = block.to_styled() { - AlignElem::alignment_in(styles.chain(&local)) + AlignElem::alignment_in(styles.chain(local)) } else { AlignElem::alignment_in(styles) } diff --git a/library/src/math/ctx.rs b/library/src/math/ctx.rs index 904c43331..bad0fe5ab 100644 --- a/library/src/math/ctx.rs +++ b/library/src/math/ctx.rs @@ -61,7 +61,7 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> { Self { vt, regions: Regions::one(regions.base(), Axes::splat(false)), - font: &font, + font, ttf: font.ttf(), table, constants, @@ -120,7 +120,7 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> { pub fn layout_content(&mut self, content: &Content) -> SourceResult { Ok(content - .layout(&mut self.vt, self.outer.chain(&self.local), self.regions)? + .layout(self.vt, self.outer.chain(&self.local), self.regions)? .into_frame()) } diff --git a/library/src/math/mod.rs b/library/src/math/mod.rs index 467f91ff8..a25c50394 100644 --- a/library/src/math/mod.rs +++ b/library/src/math/mod.rs @@ -297,7 +297,7 @@ impl LayoutMath for Content { } if let Some((elem, styles)) = self.to_styled() { - if TextElem::font_in(ctx.styles().chain(&styles)) + if TextElem::font_in(ctx.styles().chain(styles)) != TextElem::font_in(ctx.styles()) { let frame = ctx.layout_content(self)?; diff --git a/library/src/meta/bibliography.rs b/library/src/meta/bibliography.rs index ac2dea750..19126291b 100644 --- a/library/src/meta/bibliography.rs +++ b/library/src/meta/bibliography.rs @@ -52,7 +52,7 @@ pub struct BibliographyElem { args.expect::>("path to bibliography file")?; for path in &mut paths.0 { // resolve paths - *path = vm.locate(&path).at(span)?.to_string_lossy().into(); + *path = vm.locate(path).at(span)?.to_string_lossy().into(); } // check that parsing works let _ = load(vm.world(), &paths).at(span)?; @@ -145,7 +145,7 @@ impl Show for BibliographyElem { let mut seq = vec![]; if let Some(title) = self.title(styles) { - let title = title.clone().unwrap_or_else(|| { + let title = title.unwrap_or_else(|| { TextElem::packed(self.local_name(TextElem::lang_in(styles))) .spanned(self.span()) }); @@ -526,7 +526,7 @@ fn create( // Make link from citation to here work. let backlink = { let mut content = Content::empty(); - content.set_location(ref_location(&reference.entry)); + content.set_location(ref_location(reference.entry)); MetaElem::set_data(vec![Meta::Elem(content)]) }; diff --git a/library/src/meta/outline.rs b/library/src/meta/outline.rs index dbf879c34..33b6b70a5 100644 --- a/library/src/meta/outline.rs +++ b/library/src/meta/outline.rs @@ -73,7 +73,7 @@ impl Show for OutlineElem { fn show(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult { let mut seq = vec![ParbreakElem::new().pack()]; if let Some(title) = self.title(styles) { - let title = title.clone().unwrap_or_else(|| { + let title = title.unwrap_or_else(|| { TextElem::packed(self.local_name(TextElem::lang_in(styles))) .spanned(self.span()) }); diff --git a/library/src/meta/reference.rs b/library/src/meta/reference.rs index 0b317db06..24687845a 100644 --- a/library/src/meta/reference.rs +++ b/library/src/meta/reference.rs @@ -119,7 +119,7 @@ impl Show for RefElem { .map(TextElem::packed) .unwrap_or_default(), Smart::Custom(None) => Content::empty(), - Smart::Custom(Some(Supplement::Content(content))) => content.clone(), + Smart::Custom(Some(Supplement::Content(content))) => content, Smart::Custom(Some(Supplement::Func(func))) => { func.call_vt(vt, [elem.clone().into()])?.display() } diff --git a/library/src/symbols/emoji.rs b/library/src/symbols/emoji.rs index 5db3a7993..44bc3e145 100644 --- a/library/src/symbols/emoji.rs +++ b/library/src/symbols/emoji.rs @@ -10,7 +10,7 @@ pub fn emoji() -> Module { } /// A list of named emoji. -const EMOJI: &[(&'static str, Symbol)] = symbols! { +const EMOJI: &[(&str, Symbol)] = symbols! { abacus: '🧮', abc: '🔤', abcd: '🔡', diff --git a/library/src/symbols/sym.rs b/library/src/symbols/sym.rs index f1cf6649e..22c9a1ec9 100644 --- a/library/src/symbols/sym.rs +++ b/library/src/symbols/sym.rs @@ -10,7 +10,7 @@ pub fn sym() -> Module { } /// The list of general symbols. -pub(crate) const SYM: &[(&'static str, Symbol)] = symbols! { +pub(crate) const SYM: &[(&str, Symbol)] = symbols! { // Control. wj: '\u{2060}', zwj: '\u{200D}', diff --git a/library/src/text/shaping.rs b/library/src/text/shaping.rs index 4a62d5389..b895b6f9a 100644 --- a/library/src/text/shaping.rs +++ b/library/src/text/shaping.rs @@ -384,7 +384,7 @@ pub fn shape<'a>( } /// Shape text with font fallback using the `families` iterator. -fn shape_segment<'a>( +fn shape_segment( ctx: &mut ShapingContext, base: usize, text: &str, diff --git a/library/src/text/shift.rs b/library/src/text/shift.rs index 1bea3673f..26978b87f 100644 --- a/library/src/text/shift.rs +++ b/library/src/text/shift.rs @@ -135,7 +135,7 @@ fn search_text(content: &Content, sub: bool) -> Option { } else if let Some(children) = content.to_sequence() { let mut full = EcoString::new(); for item in children { - match search_text(&item, sub) { + match search_text(item, sub) { Some(text) => full.push_str(&text), None => return None, } diff --git a/library/src/visualize/polygon.rs b/library/src/visualize/polygon.rs index 07725b722..642349fa1 100644 --- a/library/src/visualize/polygon.rs +++ b/library/src/visualize/polygon.rs @@ -62,7 +62,7 @@ impl Layout for PolygonElem { let mut frame = Frame::new(target); // Only create a path if there are more than zero points. - if points.len() > 0 { + if !points.is_empty() { let fill = self.fill(styles); let stroke = self.stroke(styles).map(PartialStroke::unwrap_or_default);