Skip to content

Commit

Permalink
clippy level up
Browse files Browse the repository at this point in the history
  • Loading branch information
digama0 committed Oct 8, 2023
1 parent 98496ba commit 25c8ad6
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 61 deletions.
29 changes: 16 additions & 13 deletions mm0-rs/components/mm0_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ impl<T> ArcList<T> {
}
l.join(t, tail).push(t2.clone())
}

/// Returns a new iterator over the contents of the [`ArcList`].
pub fn iter(&self) -> ArcListIter<'_, T> { ArcListIter(self) }
}

/// An iterator over an [`ArcList`].
Expand All @@ -324,7 +327,7 @@ pub struct ArcListIter<'a, T>(&'a ArcList<T>);
impl<'a, T> Iterator for ArcListIter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<&'a T> {
let (l, t) = &**self.0.0.as_ref()?;
let (l, t) = &**self.0 .0.as_ref()?;
self.0 = l;
Some(t)
}
Expand Down Expand Up @@ -354,7 +357,7 @@ impl<T> SliceUninit<T> {
#[allow(clippy::uninit_vec)] // rust-clippy#10565
// Safety: the newly constructed elements have type MaybeUninit<T>
// so it's fine to not initialize them
unsafe { res.set_len(size) };
(unsafe { res.set_len(size) });
Self(res.into_boxed_slice())
}

Expand Down Expand Up @@ -465,14 +468,13 @@ pub struct Range {

#[cfg(feature = "lined_string")]
/// A [`PathBuf`] lazily initialized to a canonicalized "."
static CURRENT_DIR: once_cell::sync::Lazy<PathBuf> =
once_cell::sync::Lazy::new(|| {
#[cfg(target_arch = "wasm32")]
let buf = PathBuf::from(".");
#[cfg(not(target_arch = "wasm32"))]
let buf = std::fs::canonicalize(".").expect("failed to find current directory");
buf
});
static CURRENT_DIR: once_cell::sync::Lazy<PathBuf> = once_cell::sync::Lazy::new(|| {
#[cfg(target_arch = "wasm32")]
let buf = PathBuf::from(".");
#[cfg(not(target_arch = "wasm32"))]
let buf = std::fs::canonicalize(".").expect("failed to find current directory");
buf
});

/// Given a [`PathBuf`] 'buf', constructs a relative path from [`CURRENT_DIR`]
/// to buf, returning it as a String.
Expand Down Expand Up @@ -630,9 +632,10 @@ fn get_memory_rusage() -> usize { 0 }
#[cfg(all(feature = "memory", target_os = "linux"))]
#[must_use]
pub fn get_memory_usage() -> usize {
procfs::process::Process::myself().and_then(|me| me.statm())
.map_or_else(|_| get_memory_rusage(), |stat|
usize::try_from(stat.data).expect("overflow") * 4096)
procfs::process::Process::myself().and_then(|me| me.statm()).map_or_else(
|_| get_memory_rusage(),
|stat| usize::try_from(stat.data).expect("overflow") * 4096,
)
}

/// Try to get total memory usage (stack + data) in bytes using the `/proc` filesystem.
Expand Down
2 changes: 1 addition & 1 deletion mm0-rs/components/mm0b_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ pub fn find_header_error(mmb: &[u8]) -> ParseError {
}
match find_header_error_aux(mmb) {
Err(e) => e,
Ok(_) => panic!(
Ok(()) => panic!(
"zerocopy errored out when parsing mmb header, \
but `inspect_header_aux` wasn't able to find a problem"
),
Expand Down
2 changes: 1 addition & 1 deletion mm0-rs/components/mmcc/src/build_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl<'a> Translator<'a, '_> {
}

fn locate(&mut self, var: VarId) -> &mut Vec<VarId> {
self.located.entry(var).or_insert_with(Vec::new)
self.located.entry(var).or_default()
}

fn add_gen(&mut self, dominator: GenId, gen: GenId, value: HashMap<HVarId, VarId>) {
Expand Down
60 changes: 30 additions & 30 deletions mm0-rs/components/mmcc/src/infer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Type inference and elaboration
#![allow(clippy::needless_collect)]
#![allow(clippy::needless_collect, clippy::tuple_array_conversions, clippy::redundant_closure_call)]

use std::borrow::{Borrow, Cow};
use std::{cell::RefCell, fmt::Debug, hash::{Hash, Hasher}, mem, ops::Index};
Expand Down Expand Up @@ -367,7 +367,7 @@ impl Unop {
// Unop::BitNot(sz) => ctx.common.t_uint(sz),
// }
// }
#[must_use] fn ret_ty<'a>(self, ctx: &mut InferCtx<'a, '_>) -> Ty<'a> {
#[must_use] fn ret_ty<'a>(self, ctx: &InferCtx<'a, '_>) -> Ty<'a> {
match self {
Unop::Not => ctx.common.t_bool,
Unop::Neg | Unop::BitNot(Size::Inf) => ctx.common.int(),
Expand Down Expand Up @@ -640,15 +640,15 @@ impl<'a> Subst<'a> {
ExprKind::Mm0(Mm0Expr {subst, expr}) => {
let subst2 = subst.iter().map(|e| self.subst_expr(ctx, sp, e)).collect::<Vec<_>>();
if subst == subst2 { return e }
let subst = ctx.alloc.alloc_slice_fill_iter(subst2.into_iter());
let subst = ctx.alloc.alloc_slice_fill_iter(subst2);
intern!(ctx, ExprKind::Mm0(Mm0Expr {subst, expr}))
}
ExprKind::Call {f, tys, args} => {
let tys2 = tys.iter().map(|e| self.subst_ty(ctx, sp, e)).collect::<Vec<_>>();
let args2 = args.iter().map(|e| self.subst_expr(ctx, sp, e)).collect::<Vec<_>>();
if tys == tys2 && args == args2 { return e }
let tys = ctx.alloc.alloc_slice_fill_iter(tys2.into_iter());
let args = ctx.alloc.alloc_slice_fill_iter(args2.into_iter());
let tys = ctx.alloc.alloc_slice_fill_iter(tys2);
let args = ctx.alloc.alloc_slice_fill_iter(args2);
intern!(ctx, ExprKind::Call {f, tys, args})
}
ExprKind::If {cond, then, els} =>
Expand Down Expand Up @@ -714,7 +714,7 @@ impl<'a> Subst<'a> {
TuplePatternKind::Tuple(pats, mk) => {
let pats2 = pats.iter().map(|&pat| self.subst_tup_pat(ctx, sp, pat)).collect::<Vec<_>>();
if unchanged && pats == pats2 { return pat }
let pats2 = ctx.alloc.alloc_slice_fill_iter(pats2.into_iter());
let pats2 = ctx.alloc.alloc_slice_fill_iter(pats2);
TuplePatternKind::Tuple(pats2, mk)
}
TuplePatternKind::Error(p) => {
Expand Down Expand Up @@ -767,7 +767,7 @@ impl<'a> Subst<'a> {
TyKind::Output |
TyKind::Error => ty,
TyKind::Array(t, e) => subst!(TyKind::Array; t; e),
TyKind::Own(t) => subst!(|t, _| TyKind::Own(t); t;),
TyKind::Own(t) => subst!(|t, ()| TyKind::Own(t); t;),
TyKind::Shr(lft, t) => {
let Some(lft2) = self.subst_lft(ctx, sp, lft) else { return ctx.common.t_error };
let t2 = self.subst_ty(ctx, sp, t);
Expand All @@ -791,7 +791,7 @@ impl<'a> Subst<'a> {
let mut copy = self.clone();
let args2 = args.iter().map(|&arg| copy.subst_arg(ctx, sp, arg)).collect::<Vec<_>>();
if args == args2 { return ty }
let args2 = ctx.alloc.alloc_slice_fill_iter(args2.into_iter());
let args2 = ctx.alloc.alloc_slice_fill_iter(args2);
intern!(ctx, TyKind::Struct(args2))
}
TyKind::All(pat, t) => {
Expand All @@ -801,26 +801,26 @@ impl<'a> Subst<'a> {
if pat == pat2 && t == t2 { return ty }
intern!(ctx, TyKind::All(pat2, t2))
}
TyKind::Imp(t1, t2) => subst!(|(t1, t2), _| TyKind::Imp(t1, t2); t1, t2;),
TyKind::Wand(t1, t2) => subst!(|(t1, t2), _| TyKind::Wand(t1, t2); t1, t2;),
TyKind::Not(t) => subst!(|t, _| TyKind::Not(t); t;),
TyKind::Imp(t1, t2) => subst!(|(t1, t2), ()| TyKind::Imp(t1, t2); t1, t2;),
TyKind::Wand(t1, t2) => subst!(|(t1, t2), ()| TyKind::Wand(t1, t2); t1, t2;),
TyKind::Not(t) => subst!(|t, ()| TyKind::Not(t); t;),
TyKind::And(tys) => substs!(TyKind::And; tys),
TyKind::Or(tys) => substs!(TyKind::Or; tys),
TyKind::If(e, t1, t2) => subst!(|(t1, t2), e| TyKind::If(e, t1, t2); t1, t2; e),
TyKind::Ghost(t) => subst!(|t, _| TyKind::Ghost(t); t;),
TyKind::Uninit(t) => subst!(|t, _| TyKind::Uninit(t); t;),
TyKind::Pure(e) => subst!(|_, e| TyKind::Pure(e);; e),
TyKind::Ghost(t) => subst!(|t, ()| TyKind::Ghost(t); t;),
TyKind::Uninit(t) => subst!(|t, ()| TyKind::Uninit(t); t;),
TyKind::Pure(e) => subst!(|(), e| TyKind::Pure(e);; e),
TyKind::User(f, tys, args) => {
let tys2 = tys.iter().map(|e| self.subst_ty(ctx, sp, e)).collect::<Vec<_>>();
let args2 = args.iter().map(|e| self.subst_expr(ctx, sp, e)).collect::<Vec<_>>();
if tys == tys2 && args == args2 { return ty }
let tys = ctx.alloc.alloc_slice_fill_iter(tys2.into_iter());
let args = ctx.alloc.alloc_slice_fill_iter(args2.into_iter());
let tys = ctx.alloc.alloc_slice_fill_iter(tys2);
let args = ctx.alloc.alloc_slice_fill_iter(args2);
intern!(ctx, TyKind::User(f, tys, args))
}
TyKind::Heap(e1, e2, t) => subst!(|t, (e1, e2)| TyKind::Heap(e1, e2, t); t; e1, e2),
TyKind::HasTy(e1, t) => subst!(|t, e1| TyKind::HasTy(e1, t); t; e1),
TyKind::Moved(t) => subst!(|t, _| TyKind::Moved(t); t;),
TyKind::Moved(t) => subst!(|t, ()| TyKind::Moved(t); t;),
TyKind::Infer(v) => {
if let Some(ty) = ctx.mvars.ty.lookup(v) { return self.subst_ty(ctx, sp, ty) }
let span = ctx.mvars.ty[v].span;
Expand Down Expand Up @@ -865,7 +865,7 @@ impl<'a, T: FromGlobal<'a>> FromGlobal<'a> for Box<[T]> {
type Output = &'a [T::Output];
fn from_global(&self, c: &mut FromGlobalCtx<'a, '_, '_>) -> Self::Output {
let vec = self.iter().map(|t| t.from_global(c)).collect::<Vec<_>>();
c.ic.alloc.alloc_slice_fill_iter(vec.into_iter())
c.ic.alloc.alloc_slice_fill_iter(vec)
}
}

Expand Down Expand Up @@ -2584,7 +2584,7 @@ impl<'a, 'n> InferCtx<'a, 'n> {
expect!(tys.len());
let tys = if wty.is_ty() { tys } else {
let tys = tys.iter().map(|ty| wty.map(ty).to_ty(self)).collect::<Vec<_>>();
self.alloc.alloc_slice_fill_iter(tys.into_iter())
self.alloc.alloc_slice_fill_iter(tys)
};
let mk = if matches!(wty.ty.k, TyKind::List(_)) {TupleMatchKind::List} else {TupleMatchKind::And};
TuplePatternResult::Tuple(mk, TupleIter::List(tys.iter()))
Expand Down Expand Up @@ -2649,7 +2649,7 @@ impl<'a, 'n> InferCtx<'a, 'n> {
let pats = pats.iter().map(|pat| {
self.finish_tuple_pattern_inner(pat, None).0
}).collect::<Vec<_>>();
let pats = self.alloc.alloc_slice_fill_iter(pats.into_iter());
let pats = self.alloc.alloc_slice_fill_iter(pats);
let v = self.fresh_var2(pat.ctx.var);
let k = TuplePatternKind::Tuple(pats, TupleMatchKind::List);
let pat = intern!(self, TuplePatternS { var: v, k, ty });
Expand All @@ -2664,7 +2664,7 @@ impl<'a, 'n> InferCtx<'a, 'n> {
es.push(Some(e));
pat
}).collect::<Vec<_>>();
let pats = self.alloc.alloc_slice_fill_iter(pats.into_iter());
let pats = self.alloc.alloc_slice_fill_iter(pats);
let e = mk.build(self, es).unwrap_or(self.common.e_error);
(TuplePatternKind::Tuple(pats, mk), e)
}
Expand Down Expand Up @@ -2776,12 +2776,12 @@ impl<'a, 'n> InferCtx<'a, 'n> {
intern!(self, TyKind::Not(self.lower_ty(p, ExpectTy::Any))),
ast::TypeKind::And(tys) => {
let tys = tys.iter().map(|ty| self.lower_ty(ty, ExpectTy::Any)).collect::<Vec<_>>();
let tys = self.alloc.alloc_slice_fill_iter(tys.into_iter());
let tys = self.alloc.alloc_slice_fill_iter(tys);
intern!(self, TyKind::And(tys))
}
ast::TypeKind::Or(tys) => {
let tys = tys.iter().map(|ty| self.lower_ty(ty, ExpectTy::Any)).collect::<Vec<_>>();
let tys = self.alloc.alloc_slice_fill_iter(tys.into_iter());
let tys = self.alloc.alloc_slice_fill_iter(tys);
intern!(self, TyKind::Or(tys))
}
ast::TypeKind::If(e, t, f) => {
Expand All @@ -2798,7 +2798,7 @@ impl<'a, 'n> InferCtx<'a, 'n> {
intern!(self, TyKind::Pure(self.check_pure_expr(e, self.common.t_bool))),
ast::TypeKind::User(f, tys, es) => {
let tys = tys.iter().map(|ty| self.lower_ty(ty, ExpectTy::Any)).collect::<Vec<_>>();
let tys = self.alloc.alloc_slice_fill_iter(tys.into_iter());
let tys = self.alloc.alloc_slice_fill_iter(tys);
let Some(Entity::Type(tc)) = self.names.get(f) else { unreachable!() };
let args = match &tc.k {
TypeTc::ForwardDeclared => return self.common.t_error,
Expand Down Expand Up @@ -2960,7 +2960,7 @@ impl<'a, 'n> InferCtx<'a, 'n> {
fn args_to_ty_args(&mut self, args: &[hir::Arg<'a>]) -> &'a [Arg<'a>] {
let args = args.iter().map(|(attr, arg)| intern!(self, (*attr, arg.into())))
.collect::<Vec<_>>();
self.alloc.alloc_slice_fill_iter(args.into_iter())
self.alloc.alloc_slice_fill_iter(args)
}

fn lower_variant(&mut self, variant: &'a Option<Box<ast::Variant>>) -> Option<hir::Variant<'a>> {
Expand Down Expand Up @@ -3039,7 +3039,7 @@ impl<'a, 'n> InferCtx<'a, 'n> {
pf: Option<&'a ast::Expr>
) -> Option<(hir::Call<'a>, RExprTy<'a>)> {
let tys = tys.iter().map(|ty| self.lower_ty(ty, ExpectTy::Any)).collect::<Vec<_>>();
let tys = &*self.alloc.alloc_slice_fill_iter(tys.into_iter());
let tys = &*self.alloc.alloc_slice_fill_iter(tys);
let Some(Entity::Proc(ty)) = self.names.get(&f) else { unreachable!() };
let ty = ty.k.ty()?;
let ProcTy {kind, tyargs, args, outs, rets, variant, ..} = ty.clone();
Expand Down Expand Up @@ -3075,7 +3075,7 @@ impl<'a, 'n> InferCtx<'a, 'n> {
subst.subst_arg(gctx.ic, span, arg)
}).collect::<Vec<_>>();
drop(gctx);
let rets = &*self.alloc.alloc_slice_fill_iter(rets.into_iter());
let rets = &*self.alloc.alloc_slice_fill_iter(rets);
let (mut rk, ret) = match rets.len() {
0 => (ReturnKind::Unit, self.common.t_unit),
1 => match rets[0].k.1 {
Expand Down Expand Up @@ -3456,7 +3456,7 @@ impl<'a, 'n> InferCtx<'a, 'n> {
TyKind::Error))
.unwrap_or_else(|| {
let tys = es.iter().map(|e| self.new_ty_mvar(&e.span)).collect::<Vec<_>>();
let tys = self.alloc.alloc_slice_fill_iter(tys.into_iter());
let tys = self.alloc.alloc_slice_fill_iter(tys);
intern!(self, TyKind::List(tys))
});
macro_rules! expect {($n:expr) => {{
Expand Down Expand Up @@ -3522,7 +3522,7 @@ impl<'a, 'n> InferCtx<'a, 'n> {
let (e, pe) = self.check_expr(e, tgt);
let pe = self.as_pure(e.span, pe);
if let Some(v) = val {
self.equate_expr(v, pe).unwrap_or_else(|_| {
self.equate_expr(v, pe).unwrap_or_else(|()| {
self.errors.push(hir::Spanned {span, k: TypeError::IAndUnify(v, pe)});
});
} else { val = Some(pe) };
Expand Down Expand Up @@ -3593,7 +3593,7 @@ impl<'a, 'n> InferCtx<'a, 'n> {
e
}).collect();
let tgt = expect.to_ty().unwrap_or_else(|| self.common.nat());
let pe = self.alloc.alloc_slice_fill_iter(p_subst.into_iter());
let pe = self.alloc.alloc_slice_fill_iter(p_subst);
let pe = intern!(self, ExprKind::Mm0(Mm0Expr {subst: pe, expr }));
ret![Mm0(types::Mm0Expr { subst, expr }), Ok(pe), tgt]
}
Expand Down
2 changes: 1 addition & 1 deletion mm0-rs/components/mmcc/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl<'a> Collector<'a> {
/// The start of the `.text` section, also the entry point for the program.
pub const TEXT_START: u32 = 0x40_0078;

//// A completed code object. This includes the list of instructions,
/// A completed code object. This includes the list of instructions,
/// and can be serialized to a list of bytes using the [`LinkedCode::write_elf`] method.
#[derive(Clone, Debug)]
pub struct LinkedCode {
Expand Down
6 changes: 3 additions & 3 deletions mm0-rs/components/mmcc/src/regalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl PCodeBuilder {

fn apply_edits(&mut self,
edits: &mut std::iter::Peekable<impl Iterator<Item=(ProgPoint, Edit)>>,
ar: &mut ApplyRegalloc,
ar: &ApplyRegalloc,
pt: ProgPoint
) {
while edits.peek().map_or(false, |p| p.0 == pt) {
Expand Down Expand Up @@ -349,7 +349,7 @@ impl VCode {
code.block_params.push_new();
code.code.block_addr.push(code.len);
};
code.apply_edits(&mut edits, &mut ar, ProgPoint::before(i));
code.apply_edits(&mut edits, &ar, ProgPoint::before(i));
match *inst {
Inst::Fallthrough { dst } => {
assert!(self.blocks[dst].1 == i.next());
Expand Down Expand Up @@ -452,7 +452,7 @@ impl VCode {
}
Inst::Ud2 => { code.push(PInst::Ud2); }
}
code.apply_edits(&mut edits, &mut ar, ProgPoint::after(i));
code.apply_edits(&mut edits, &ar, ProgPoint::after(i));
}
bb.finish_block(&mut code);
(self.abi, code.finish(saved_regs))
Expand Down
1 change: 1 addition & 0 deletions mm0-rs/components/mmcc/src/types/ty.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Types used in the rest of the compiler.
#![allow(clippy::tuple_array_conversions)]

use std::{fmt::Display, ops::BitOrAssign};
use num::BigInt;
Expand Down
2 changes: 1 addition & 1 deletion mm0-rs/components/mmcc/src/types/vcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl<I> VCode<I> {
let bl = self.blocks.push((id, inst, inst));
self.block_preds.push(vec![]);
self.block_succs.push(vec![]);
self.block_params.push(params.into_iter());
self.block_params.push(params);
bl
}
}
Expand Down
2 changes: 1 addition & 1 deletion mm0-rs/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ fn elaborate_and_send(path: FileRef, send: FSender<ElabResult<()>>, rd: ArcList<
pub(crate) fn elab_for_result(path: FileRef) -> io::Result<(FileContents, Option<FrozenEnv>)> {
let (path, file) = VFS.get_or_insert(path)?;
let env = match block_on(elaborate(path, Default::default()))? {
ElabResult::Ok(_, _, env) => Some(env),
ElabResult::Ok((), _, env) => Some(env),
_ => None
};
Ok((file.text.clone(), env))
Expand Down
1 change: 1 addition & 0 deletions mm0-rs/src/elab/frozen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ unsafe impl Sync for FrozenEnv {}

impl FrozenEnv {
/// Create a new [`FrozenEnv`] from an [`Environment`].
#[allow(clippy::arc_with_non_send_sync)]
#[must_use] pub fn new(env: Environment) -> Self { Self(Arc::new(env)) }

/// Convert a [`&FrozenEnv`](FrozenEnv) into an [`&Environment`](Environment).
Expand Down
2 changes: 1 addition & 1 deletion mm0-rs/src/elab/lisp/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ impl LocalCtx {
}
fn push(&mut self, x: AtomId) -> usize {
let old = self.ctx.len();
if x != AtomId::UNDER { self.names.entry(x).or_insert_with(Vec::new).push(old) }
if x != AtomId::UNDER { self.names.entry(x).or_default().push(old) }
self.ctx.push(x);
old
}
Expand Down
3 changes: 3 additions & 0 deletions mm0-rs/src/elab/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ impl<H: NodeHash> Dedup<H> {
}
}

/// Returns an iterator over the elements allocated by the [`Dedup`].
pub fn iter(&self) -> DedupIter<'_, H> { self.into_iter() }

/// Insert a new hash object `v`, originating from lisp object `p`,
/// into the [`Dedup`], returning the allocated index.
pub fn add(&mut self, kind: ProofKind, p: LispVal, v: H) -> usize {
Expand Down
3 changes: 3 additions & 0 deletions mm0-rs/src/elab/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,7 @@ impl<T> Spans<T> {
.for_each(|(_, v)| v.iter().for_each(|x| f(sp, x))))
}
}

/// Returns an iterator over the collected spans.
#[must_use] pub fn iter(&self) -> <&Self as IntoIterator>::IntoIter { self.into_iter() }
}
Loading

0 comments on commit 25c8ad6

Please sign in to comment.