Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix most clippy warnings #273

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/fuzz-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,20 @@ where
}

fn interp(&self, wasm: &[u8]) -> Result<String> {
fs::write(self.scratch.path(), &wasm).context("failed to write to scratch file")?;
fs::write(self.scratch.path(), wasm).context("failed to write to scratch file")?;
wasm_interp(self.scratch.path())
}

fn round_trip_through_walrus(&self, wasm: &[u8]) -> Result<Vec<u8>> {
let mut module =
walrus::Module::from_buffer(&wasm).context("walrus failed to parse the wasm buffer")?;
walrus::Module::from_buffer(wasm).context("walrus failed to parse the wasm buffer")?;
walrus::passes::gc::run(&mut module);
let buf = module.emit_wasm();
Ok(buf)
}

fn test_wat(&self, wat: &str) -> Result<()> {
let wasm = self.wat2wasm(&wat)?;
let wasm = self.wat2wasm(wat)?;
let expected = self.interp(&wasm)?;

let walrus_wasm = self.round_trip_through_walrus(&wasm)?;
Expand Down Expand Up @@ -313,7 +313,7 @@ impl<R: Rng> WatGen<R> {
self.wat.push_str(&operator.to_string());

for op in immediates.into_iter() {
self.wat.push_str(" ");
self.wat.push(' ');
self.wat.push_str(op.as_ref());
}

Expand Down
18 changes: 9 additions & 9 deletions crates/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Parse for WalrusFieldOpts {
if attr == "skip_visit" {
return Ok(Attr::SkipVisit);
}
return Err(Error::new(attr.span(), "unexpected attribute"));
Err(Error::new(attr.span(), "unexpected attribute"))
}
}
}
Expand Down Expand Up @@ -144,7 +144,7 @@ impl Parse for WalrusVariantOpts {
if attr == "skip_builder" {
return Ok(Attr::SkipBuilder);
}
return Err(Error::new(attr.span(), "unexpected attribute"));
Err(Error::new(attr.span(), "unexpected attribute"))
}
}
}
Expand All @@ -166,7 +166,7 @@ fn walrus_attrs(attrs: &mut Vec<syn::Attribute>) -> TokenStream {
ret.extend(group);
ret.extend(quote! { , });
}
return ret.into();
ret.into()
}

fn create_types(attrs: &[syn::Attribute], variants: &[WalrusVariant]) -> impl quote::ToTokens {
Expand Down Expand Up @@ -328,10 +328,10 @@ fn create_types(attrs: &[syn::Attribute], variants: &[WalrusVariant]) -> impl qu
}
}

fn visit_fields<'a>(
variant: &'a WalrusVariant,
fn visit_fields(
variant: &WalrusVariant,
allow_skip: bool,
) -> impl Iterator<Item = (syn::Ident, proc_macro2::TokenStream, bool)> + 'a {
) -> impl Iterator<Item = (syn::Ident, proc_macro2::TokenStream, bool)> + '_ {
return variant
.syn
.fields
Expand Down Expand Up @@ -439,7 +439,7 @@ fn create_visit(variants: &[WalrusVariant]) -> impl quote::ToTokens {
}
});

let doc = format!("Visit `{}`.", name.to_string());
let doc = format!("Visit `{}`.", name);
visitor_trait_methods.push(quote! {
#[doc=#doc]
#[inline]
Expand Down Expand Up @@ -723,13 +723,13 @@ fn create_builder(variants: &[WalrusVariant]) -> impl quote::ToTokens {

let doc = format!(
"Push a new `{}` instruction onto this builder's block.",
name.to_string()
name
);
let at_doc = format!(
"Splice a new `{}` instruction into this builder's block at the given index.\n\n\
# Panics\n\n\
Panics if `position > self.instrs.len()`.",
name.to_string()
name
);

let arg_names = &arg_names;
Expand Down
2 changes: 1 addition & 1 deletion crates/tests-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ where
cmd.arg(input);
cmd.arg("-o");
cmd.arg(tmp.path());
cmd.args(&[
cmd.args([
"--enable-threads",
"--enable-bulk-memory",
// "--enable-reference-types",
Expand Down
23 changes: 11 additions & 12 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ impl FileCheck {
let mut iter = contents.lines().map(str::trim);
while let Some(line) = iter.next() {
if line.starts_with("(; CHECK-ALL:") {
if patterns.len() != 0 {
if !patterns.is_empty() {
panic!("CHECK cannot be used with CHECK-ALL");
}
let mut pattern = Vec::new();
while let Some(line) = iter.next() {
for line in iter.by_ref() {
if line == ";)" {
break;
}
Expand All @@ -95,18 +95,17 @@ impl FileCheck {
return FileCheck::Exhaustive(pattern, path.to_path_buf());
}

if line.starts_with(";; CHECK:") {
let p = line[";; CHECK:".len()..].to_string();
patterns.push(vec![p]);
if let Some(p) = line.strip_prefix(";; CHECK:") {
patterns.push(vec![p.to_string()]);
}
if line.starts_with(";; NEXT:") {
if let Some(n) = line.strip_prefix(";; NEXT:") {
let p = patterns
.last_mut()
.expect("NEXT should never come before CHECK");
p.push(line[";; NEXT:".len()..].to_string());
p.push(n.to_string());
}
}
if patterns.len() == 0 {
if patterns.is_empty() {
FileCheck::None(path.to_path_buf())
} else {
FileCheck::Patterns(patterns)
Expand All @@ -125,7 +124,7 @@ impl FileCheck {

'inner: while let Some(pos) = output_lines[start..]
.iter()
.position(|l| matches(*l, first_line))
.position(|l| matches(l, first_line))
{
start = pos + 1;
if output_lines[pos..].len() + 1 < pattern.len() {
Expand Down Expand Up @@ -155,11 +154,11 @@ impl FileCheck {
}
}
FileCheck::None(_) => {
println!("");
println!();
println!("no test assertions were found in this file, but");
println!("you can rerun tests with `WALRUS_BLESS=1` to");
println!("automatically add assertions to this file");
println!("");
println!();
panic!("no tests to run")
}
}
Expand Down Expand Up @@ -210,7 +209,7 @@ fn update_output(path: &Path, output: &str) {
new_output.push_str(" ");
new_output.push_str(line.trim_end());
}
new_output.push_str("\n");
new_output.push('\n');
}
let new = format!(
"{}\n\n(; CHECK-ALL:\n{}\n;)\n",
Expand Down
4 changes: 2 additions & 2 deletions crates/tests/tests/custom_sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ fn smoke_test_code_transform() {
fn apply_code_transform(&mut self, transform: &CodeTransform) {
APPLIED_CODE_TRANSFORM.store(1, Ordering::SeqCst);
assert!(!transform.instruction_map.is_empty());
for (input_offset, output_offset) in transform.instruction_map.iter().cloned() {
assert_eq!(input_offset.data() as usize + 3, output_offset);
for (input_offset, output_offset) in transform.instruction_map.iter() {
assert_eq!(input_offset.data() as usize + 3, *output_offset);
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/tests/tests/spec-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ fn run(wast: &Path) -> Result<(), anyhow::Error> {
let proposal = wast
.iter()
.skip_while(|part| *part != "proposals")
.skip(1)
.next()
.nth(1)
.map(|s| s.to_str().unwrap());

let extra_args: &[&str] = match proposal {
Expand Down Expand Up @@ -168,7 +167,7 @@ fn run(wast: &Path) -> Result<(), anyhow::Error> {
}

let wasm = module.emit_wasm();
fs::write(&file, wasm)?;
fs::write(file, wasm)?;
}

run_spectest_interp(tempdir.path(), extra_args)?;
Expand Down
8 changes: 4 additions & 4 deletions src/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<T: DotNode> Dot for T {

impl FieldAggregator for AppendFields<'_> {
fn add_field(&mut self, field: &[&str]) {
assert!(field.len() > 0);
assert!(!field.is_empty());
self.out.push_str("<tr>");
for f in field {
self.out.push_str("<td>");
Expand All @@ -114,7 +114,7 @@ impl<T: DotNode> Dot for T {
}

fn add_field_with_port(&mut self, port: &str, field: &str) {
assert!(field.len() > 0);
assert!(!field.is_empty());
self.out.push_str("<tr>");
self.out.push_str("<td port=\"");
self.out.push_str(port);
Expand Down Expand Up @@ -142,7 +142,7 @@ impl<T: DotNode> Dot for T {
fn add_edge_from_port(&mut self, port: &str, to: &impl DotName) {
self.out.push_str(" ");
self.out.push_str(self.from);
self.out.push_str(":");
self.out.push(':');
self.out.push_str(port);
self.out.push_str(" -> ");
self.out.push_str(&to.dot_name());
Expand Down Expand Up @@ -174,7 +174,7 @@ impl Dot for Module {
// self.name.dot(out);
// self.config.dot(out);

out.push_str("}");
out.push('}');
}
}

Expand Down
25 changes: 7 additions & 18 deletions src/module/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,7 @@ where
T: CustomSection,
{
fn clone(&self) -> Self {
TypedCustomSectionId {
id: self.id,
_phantom: PhantomData,
}
*self
}
}

Expand Down Expand Up @@ -366,26 +363,18 @@ impl ModuleCustomSections {

/// Iterate over shared references to custom sections and their ids.
pub fn iter(&self) -> impl Iterator<Item = (UntypedCustomSectionId, &dyn CustomSection)> {
self.arena.iter().flat_map(|(id, s)| {
if let Some(s) = s.as_ref() {
Some((UntypedCustomSectionId(id), &**s))
} else {
None
}
})
self.arena
.iter()
.flat_map(|(id, s)| Some((UntypedCustomSectionId(id), &**s.as_ref()?)))
}

/// Iterate over exclusive references to custom sections and their ids.
pub fn iter_mut(
&mut self,
) -> impl Iterator<Item = (UntypedCustomSectionId, &mut dyn CustomSection)> {
self.arena.iter_mut().flat_map(|(id, s)| {
if let Some(s) = s.as_mut() {
Some((UntypedCustomSectionId(id), &mut **s))
} else {
None
}
})
self.arena
.iter_mut()
.flat_map(|(id, s)| Some((UntypedCustomSectionId(id), &mut **s.as_mut()?)))
}

/// Remove a custom section (by type) from the module.
Expand Down
7 changes: 2 additions & 5 deletions src/module/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ impl Data {

/// Is this a passive data segment?
pub fn is_passive(&self) -> bool {
match self.kind {
DataKind::Passive => true,
_ => false,
}
matches!(self.kind, DataKind::Passive)
}
}

Expand Down Expand Up @@ -129,7 +126,7 @@ impl ModuleData {
let mut any_passive = false;

for data in self.iter() {
cx.indices.set_data_index(data.id(), count as u32);
cx.indices.set_data_index(data.id(), count);
count += 1;
any_passive |= data.is_passive();
}
Expand Down
22 changes: 10 additions & 12 deletions src/module/debug/dwarf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ mod tests {
use gimli::*;
use std::cell::RefCell;

fn make_test_debug_line<'a>(
debug_line: &'a mut write::DebugLine<write::EndianVec<LittleEndian>>,
) -> IncompleteLineProgram<EndianSlice<'a, LittleEndian>> {
fn make_test_debug_line(
debug_line: &mut write::DebugLine<write::EndianVec<LittleEndian>>,
) -> IncompleteLineProgram<EndianSlice<'_, LittleEndian>> {
let encoding = Encoding {
format: Format::Dwarf32,
version: 4,
Expand Down Expand Up @@ -368,11 +368,9 @@ mod tests {
.unwrap();

let debug_line = read::DebugLine::new(debug_line.slice(), LittleEndian);
let incomplete_debug_line = debug_line
debug_line
.program(DebugLineOffset(0), 4, None, None)
.unwrap();

incomplete_debug_line
.unwrap()
}

#[test]
Expand Down Expand Up @@ -559,11 +557,11 @@ mod tests {
#[test]
fn test_convert_high_pc() {
let sections = make_test_debug_info();
let mut read_dwarf = Dwarf::default();

read_dwarf.debug_info = read::DebugInfo::new(sections.debug_info.slice(), LittleEndian);
read_dwarf.debug_abbrev =
read::DebugAbbrev::new(sections.debug_abbrev.slice(), LittleEndian);
let read_dwarf = Dwarf {
debug_info: read::DebugInfo::new(sections.debug_info.slice(), LittleEndian),
debug_abbrev: read::DebugAbbrev::new(sections.debug_abbrev.slice(), LittleEndian),
..Default::default()
};

let read_first_unit_header = read_dwarf.units().next().unwrap().unwrap();
let read_first_unit = read_dwarf.unit(read_first_unit_header).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/module/debug/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ mod tests {

let mut cursor = DebuggingInformationCursor::new(&mut unit1);

assert_eq!(cursor.current().is_none(), true);
assert!(cursor.current().is_none());
assert_eq!(cursor.next_dfs().unwrap().id(), root_id);
assert_eq!(cursor.next_dfs().unwrap().id(), child1_id);
assert_eq!(cursor.next_dfs().unwrap().id(), child2_id);
assert_eq!(cursor.next_dfs().is_none(), true);
assert!(cursor.next_dfs().is_none());
}
}
2 changes: 1 addition & 1 deletion src/module/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl Emit for ModuleElements {
Some(cx.indices.get_table_index(*table)).filter(|&index| index != 0);
wasm_element_section.active(
table_index,
&offset.to_wasmencoder_type(&cx),
&offset.to_wasmencoder_type(cx),
els,
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/module/functions/local_function/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<'a> ValidationContext<'a> {
}

pub fn pop_control(&mut self) -> Result<(ControlFrame, InstrSeqId)> {
let frame = impl_pop_control(&mut self.controls)?;
let frame = impl_pop_control(self.controls)?;
let block = frame.block;
Ok((frame, block))
}
Expand Down Expand Up @@ -214,7 +214,7 @@ fn impl_push_control_with_ty(
fn impl_pop_control(controls: &mut ControlStack) -> Result<ControlFrame> {
controls
.last()
.ok_or_else(|| ErrorKind::InvalidWasm)
.ok_or(ErrorKind::InvalidWasm)
.context("attempted to pop a frame from an empty control stack")?;
let frame = controls.pop().unwrap();
Ok(frame)
Expand Down
Loading