Needless clone, borrows, casts and lifetimes (#504)

This commit is contained in:
Marek Barvíř 2023-04-01 15:53:13 +02:00 committed by GitHub
parent 9eb6174b22
commit 5e5b1bba51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 37 additions and 31 deletions

View File

@ -279,7 +279,7 @@ fn compile_once(world: &mut SystemWorld, command: &CompileSettings) -> StrResult
// Print diagnostics. // Print diagnostics.
Err(errors) => { Err(errors) => {
status(command, Status::Error).unwrap(); status(command, Status::Error).unwrap();
print_diagnostics(&world, *errors) print_diagnostics(world, *errors)
.map_err(|_| "failed to print diagnostics")?; .map_err(|_| "failed to print diagnostics")?;
Ok(true) Ok(true)
} }
@ -576,10 +576,10 @@ impl PathHash {
/// Read a file. /// Read a file.
fn read(path: &Path) -> FileResult<Vec<u8>> { fn read(path: &Path) -> FileResult<Vec<u8>> {
let f = |e| FileError::from_io(e, path); 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) Err(FileError::IsDirectory)
} else { } else {
fs::read(&path).map_err(f) fs::read(path).map_err(f)
} }
} }

View File

@ -457,7 +457,7 @@ pub fn round(
Num::Int(n) if digits == 0 => Value::Int(n), Num::Int(n) if digits == 0 => Value::Int(n),
_ => { _ => {
let n = value.float(); 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) Value::Float((n * factor).round() / factor)
} }
} }

View File

@ -30,7 +30,7 @@ impl Layout for FlowElem {
let mut styles = styles; let mut styles = styles;
if let Some((elem, map)) = child.to_styled() { if let Some((elem, map)) = child.to_styled() {
child = elem; child = elem;
styles = outer.chain(&map); styles = outer.chain(map);
} }
if let Some(elem) = child.to::<VElem>() { if let Some(elem) = child.to::<VElem>() {
@ -54,7 +54,7 @@ impl Layout for FlowElem {
true, true,
)); ));
} else if child.can::<dyn Layout>() { } else if child.can::<dyn Layout>() {
layouter.layout_multiple(vt, &child, styles)?; layouter.layout_multiple(vt, child, styles)?;
} else if child.is::<ColbreakElem>() { } else if child.is::<ColbreakElem>() {
if !layouter.regions.backlog.is_empty() || layouter.regions.last.is_some() if !layouter.regions.backlog.is_empty() || layouter.regions.last.is_some()
{ {

View File

@ -15,6 +15,11 @@ impl Fragment {
Self(frames) 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. /// The number of frames in the fragment.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.0.len() self.0.len()

View File

@ -267,7 +267,7 @@ impl<'a, 'v> GridLayouter<'a, 'v> {
// We use these regions for auto row measurement. Since at that moment, // We use these regions for auto row measurement. Since at that moment,
// columns are already sized, we can enable horizontal expansion. // columns are already sized, we can enable horizontal expansion.
let mut regions = regions.clone(); let mut regions = regions;
regions.expand = Axes::new(true, false); regions.expand = Axes::new(true, false);
Self { Self {

View File

@ -197,7 +197,7 @@ cast_from_value! {
ListMarker, ListMarker,
v: Content => Self::Content(vec![v]), v: Content => Self::Content(vec![v]),
array: Array => { array: Array => {
if array.len() == 0 { if array.is_empty() {
Err("array must contain at least one marker")?; Err("array must contain at least one marker")?;
} }
Self::Content(array.into_iter().map(Value::display).collect()) Self::Content(array.into_iter().map(Value::display).collect())

View File

@ -172,7 +172,7 @@ fn realize_root<'a>(
return Ok((content.clone(), styles)); 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.accept(content, styles)?;
builder.interrupt_page(Some(styles))?; builder.interrupt_page(Some(styles))?;
let (pages, shared) = builder.doc.unwrap().pages.finish(); let (pages, shared) = builder.doc.unwrap().pages.finish();
@ -197,7 +197,7 @@ fn realize_block<'a>(
return Ok((content.clone(), styles)); 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.accept(content, styles)?;
builder.interrupt_par()?; builder.interrupt_par()?;
let (children, shared) = builder.flow.0.finish(); let (children, shared) = builder.flow.0.finish();
@ -314,7 +314,7 @@ impl<'a, 'v, 't> Builder<'a, 'v, 't> {
) -> SourceResult<()> { ) -> SourceResult<()> {
let stored = self.scratch.styles.alloc(styles); let stored = self.scratch.styles.alloc(styles);
let styles = stored.chain(map); let styles = stored.chain(map);
self.interrupt_style(&map, None)?; self.interrupt_style(map, None)?;
self.accept(elem, styles)?; self.accept(elem, styles)?;
self.interrupt_style(map, Some(styles))?; self.interrupt_style(map, Some(styles))?;
Ok(()) Ok(())
@ -468,9 +468,9 @@ impl<'a> FlowBuilder<'a> {
let above = BlockElem::above_in(styles); let above = BlockElem::above_in(styles);
let below = BlockElem::below_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(content.clone(), styles);
self.0.push(below.clone().pack(), styles); self.0.push(below.pack(), styles);
return true; return true;
} }

View File

@ -365,12 +365,13 @@ impl<'a> Item<'a> {
} }
/// Maps byte offsets back to spans. /// Maps byte offsets back to spans.
#[derive(Default)]
pub struct SpanMapper(Vec<(usize, Span)>); pub struct SpanMapper(Vec<(usize, Span)>);
impl SpanMapper { impl SpanMapper {
/// Create a new span mapper. /// Create a new span mapper.
pub fn new() -> Self { pub fn new() -> Self {
Self(vec![]) Self::default()
} }
/// Push a span for a segment with the given length. /// 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 /// Get a style property, but only if it is the same for all children of the
/// paragraph. /// paragraph.
fn shared_get<'a, T: PartialEq>( fn shared_get<T: PartialEq>(
styles: StyleChain<'a>, styles: StyleChain<'_>,
children: &[Content], children: &[Content],
getter: fn(StyleChain) -> T, getter: fn(StyleChain) -> T,
) -> Option<T> { ) -> Option<T> {
@ -754,7 +755,7 @@ fn shared_get<'a, T: PartialEq>(
children children
.iter() .iter()
.filter_map(|child| child.to_styled()) .filter_map(|child| child.to_styled())
.all(|(_, local)| getter(styles.chain(&local)) == value) .all(|(_, local)| getter(styles.chain(local)) == value)
.then(|| value) .then(|| value)
} }

View File

@ -198,7 +198,7 @@ impl<'a> StackLayouter<'a> {
let aligns = if let Some(align) = block.to::<AlignElem>() { let aligns = if let Some(align) = block.to::<AlignElem>() {
align.alignment(styles) align.alignment(styles)
} else if let Some((_, local)) = block.to_styled() { } else if let Some((_, local)) = block.to_styled() {
AlignElem::alignment_in(styles.chain(&local)) AlignElem::alignment_in(styles.chain(local))
} else { } else {
AlignElem::alignment_in(styles) AlignElem::alignment_in(styles)
} }

View File

@ -61,7 +61,7 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
Self { Self {
vt, vt,
regions: Regions::one(regions.base(), Axes::splat(false)), regions: Regions::one(regions.base(), Axes::splat(false)),
font: &font, font,
ttf: font.ttf(), ttf: font.ttf(),
table, table,
constants, constants,
@ -120,7 +120,7 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
pub fn layout_content(&mut self, content: &Content) -> SourceResult<Frame> { pub fn layout_content(&mut self, content: &Content) -> SourceResult<Frame> {
Ok(content 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()) .into_frame())
} }

View File

@ -297,7 +297,7 @@ impl LayoutMath for Content {
} }
if let Some((elem, styles)) = self.to_styled() { 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()) != TextElem::font_in(ctx.styles())
{ {
let frame = ctx.layout_content(self)?; let frame = ctx.layout_content(self)?;

View File

@ -52,7 +52,7 @@ pub struct BibliographyElem {
args.expect::<Spanned<BibPaths>>("path to bibliography file")?; args.expect::<Spanned<BibPaths>>("path to bibliography file")?;
for path in &mut paths.0 { for path in &mut paths.0 {
// resolve paths // 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 // check that parsing works
let _ = load(vm.world(), &paths).at(span)?; let _ = load(vm.world(), &paths).at(span)?;
@ -145,7 +145,7 @@ impl Show for BibliographyElem {
let mut seq = vec![]; let mut seq = vec![];
if let Some(title) = self.title(styles) { 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))) TextElem::packed(self.local_name(TextElem::lang_in(styles)))
.spanned(self.span()) .spanned(self.span())
}); });
@ -526,7 +526,7 @@ fn create(
// Make link from citation to here work. // Make link from citation to here work.
let backlink = { let backlink = {
let mut content = Content::empty(); 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)]) MetaElem::set_data(vec![Meta::Elem(content)])
}; };

View File

@ -73,7 +73,7 @@ impl Show for OutlineElem {
fn show(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult<Content> { fn show(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult<Content> {
let mut seq = vec![ParbreakElem::new().pack()]; let mut seq = vec![ParbreakElem::new().pack()];
if let Some(title) = self.title(styles) { 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))) TextElem::packed(self.local_name(TextElem::lang_in(styles)))
.spanned(self.span()) .spanned(self.span())
}); });

View File

@ -119,7 +119,7 @@ impl Show for RefElem {
.map(TextElem::packed) .map(TextElem::packed)
.unwrap_or_default(), .unwrap_or_default(),
Smart::Custom(None) => Content::empty(), 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))) => { Smart::Custom(Some(Supplement::Func(func))) => {
func.call_vt(vt, [elem.clone().into()])?.display() func.call_vt(vt, [elem.clone().into()])?.display()
} }

View File

@ -10,7 +10,7 @@ pub fn emoji() -> Module {
} }
/// A list of named emoji. /// A list of named emoji.
const EMOJI: &[(&'static str, Symbol)] = symbols! { const EMOJI: &[(&str, Symbol)] = symbols! {
abacus: '🧮', abacus: '🧮',
abc: '🔤', abc: '🔤',
abcd: '🔡', abcd: '🔡',

View File

@ -10,7 +10,7 @@ pub fn sym() -> Module {
} }
/// The list of general symbols. /// The list of general symbols.
pub(crate) const SYM: &[(&'static str, Symbol)] = symbols! { pub(crate) const SYM: &[(&str, Symbol)] = symbols! {
// Control. // Control.
wj: '\u{2060}', wj: '\u{2060}',
zwj: '\u{200D}', zwj: '\u{200D}',

View File

@ -384,7 +384,7 @@ pub fn shape<'a>(
} }
/// Shape text with font fallback using the `families` iterator. /// Shape text with font fallback using the `families` iterator.
fn shape_segment<'a>( fn shape_segment(
ctx: &mut ShapingContext, ctx: &mut ShapingContext,
base: usize, base: usize,
text: &str, text: &str,

View File

@ -135,7 +135,7 @@ fn search_text(content: &Content, sub: bool) -> Option<EcoString> {
} else if let Some(children) = content.to_sequence() { } else if let Some(children) = content.to_sequence() {
let mut full = EcoString::new(); let mut full = EcoString::new();
for item in children { for item in children {
match search_text(&item, sub) { match search_text(item, sub) {
Some(text) => full.push_str(&text), Some(text) => full.push_str(&text),
None => return None, None => return None,
} }

View File

@ -62,7 +62,7 @@ impl Layout for PolygonElem {
let mut frame = Frame::new(target); let mut frame = Frame::new(target);
// Only create a path if there are more than zero points. // 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 fill = self.fill(styles);
let stroke = self.stroke(styles).map(PartialStroke::unwrap_or_default); let stroke = self.stroke(styles).map(PartialStroke::unwrap_or_default);