Skip to content

Commit

Permalink
chore: Make Clippy happier. (#255)
Browse files Browse the repository at this point in the history
`cargo clippy` wasn't happy. Now it's happier.
  • Loading branch information
Hywan authored Nov 10, 2023
1 parent 040a149 commit b2ee3d8
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 171 deletions.
15 changes: 3 additions & 12 deletions crates/js-component-bindgen-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ impl Guest for JsComponentBindgenComponent {
name: options.name,
no_typescript: options.no_typescript.unwrap_or(false),
instantiation: options.instantiation.unwrap_or(false),
map: match options.map {
Some(map) => Some(map.into_iter().collect()),
None => None,
},
map: options.map.map(|map| map.into_iter().collect()),
no_nodejs_compat: options.no_nodejs_compat.unwrap_or(false),
base64_cutoff: options.base64_cutoff.unwrap_or(5000) as usize,
tla_compat: options
Expand Down Expand Up @@ -110,10 +107,7 @@ impl Guest for JsComponentBindgenComponent {
};
let id = resolve.push(pkg).map_err(|e| e.to_string())?;

let world_string = match &opts.world {
Some(world) => Some(world.to_string()),
None => None,
};
let world_string = opts.world.map(|world| world.to_string());
let world = resolve
.select_world(id, world_string.as_deref())
.map_err(|e| e.to_string())?;
Expand All @@ -123,10 +117,7 @@ impl Guest for JsComponentBindgenComponent {
no_typescript: false,
no_nodejs_compat: false,
instantiation: opts.instantiation.unwrap_or(false),
map: match opts.map {
Some(map) => Some(map.into_iter().collect()),
None => None,
},
map: opts.map.map(|map| map.into_iter().collect()),
tla_compat: opts.tla_compat.unwrap_or(false),
valid_lifting_optimization: false,
base64_cutoff: 0,
Expand Down
12 changes: 7 additions & 5 deletions crates/js-component-bindgen/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,17 @@ pub enum AugmentedOp {

impl<'a> Translation<'a> {
pub fn new(translation: ModuleTranslation<'a>) -> Result<Translation<'a>> {
let mut features = WasmFeatures::default();
features.multi_memory = false;
match Validator::new_with_features(features).validate_all(&translation.wasm) {
let mut features = WasmFeatures {
multi_memory: false,
..Default::default()
};
match Validator::new_with_features(features).validate_all(translation.wasm) {
// This module validates without multi-memory, no need to augment
// it
Ok(_) => return Ok(Translation::Normal(translation)),
Err(e) => {
features.multi_memory = true;
match Validator::new_with_features(features).validate_all(&translation.wasm) {
match Validator::new_with_features(features).validate_all(translation.wasm) {
// This module validates with multi-memory, so fall through
// to augmentation.
Ok(_) => {}
Expand Down Expand Up @@ -609,7 +611,7 @@ macro_rules! define_translate {
// the cases below.
($(@$p:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident)*) => {
$(
#[allow(clippy::drop_copy)]
#[allow(dropping_copy_types)]
fn $visit(&mut self $(, $($arg: $argty),*)?) {
#[allow(unused_imports)]
use wasm_encoder::Instruction::*;
Expand Down
22 changes: 10 additions & 12 deletions crates/js-component-bindgen/src/esm_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ impl EsmBindgen {
}

/// get the exports (including exported aliases) from the bindgen
pub fn exports<'a>(&'a self) -> Vec<(&'a str, &'a str)> {
pub fn exports(&self) -> Vec<(&str, &str)> {
self.export_aliases
.iter()
.map(|(alias, name)| (alias.as_ref(), name.as_ref()))
.chain(
self.exports
.iter()
.map(|(name, _)| (name.as_ref(), name.as_ref())),
.keys()
.map(|name| (name.as_ref(), name.as_ref())),
)
.collect()
}
Expand All @@ -146,7 +146,7 @@ impl EsmBindgen {
continue;
};
let (local_name, _) =
local_names.get_or_create(&format!("export:{}", export_name), &export_name);
local_names.get_or_create(&format!("export:{export_name}"), export_name);
uwriteln!(output, "const {local_name} = {{");
for (func_name, export) in iface {
let Binding::Local(local_name) = export else {
Expand All @@ -173,7 +173,7 @@ impl EsmBindgen {
Binding::Interface(_) => local_names.get(&format!("export:{}", export_name)),
};
let alias_maybe_quoted = maybe_quote_id(alias);
if local_name == &alias_maybe_quoted {
if local_name == alias_maybe_quoted {
output.push_str(local_name);
} else if instantiation {
uwrite!(output, "{alias_maybe_quoted}: {local_name}");
Expand All @@ -192,7 +192,7 @@ impl EsmBindgen {
Binding::Interface(_) => local_names.get(&format!("export:{}", export_name)),
};
let export_name_maybe_quoted = maybe_quote_id(export_name);
if local_name == &export_name_maybe_quoted {
if local_name == export_name_maybe_quoted {
output.push_str(local_name);
} else if instantiation {
uwrite!(output, "{export_name_maybe_quoted}: {local_name}");
Expand Down Expand Up @@ -230,7 +230,7 @@ impl EsmBindgen {
}
match binding {
Binding::Interface(bindings) => {
if !imports_object.is_some() && bindings.len() == 1 {
if imports_object.is_none() && bindings.len() == 1 {
let (import_name, import) = bindings.iter().next().unwrap();
if import_name == "default" {
let local_name = match import {
Expand Down Expand Up @@ -267,12 +267,10 @@ impl EsmBindgen {
};
if external_name == local_name {
uwrite!(output, "{external_name}");
} else if imports_object.is_some() {
uwrite!(output, "{external_name}: {local_name}");
} else {
if imports_object.is_some() {
uwrite!(output, "{external_name}: {local_name}");
} else {
uwrite!(output, "{external_name} as {local_name}");
}
uwrite!(output, "{external_name} as {local_name}");
}
}
if !first {
Expand Down
7 changes: 2 additions & 5 deletions crates/js-component-bindgen/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ impl Files {
}

pub fn get_size(&mut self, name: &str) -> Option<usize> {
match self.files.get(name) {
Some(data) => Some(data.len()),
None => None,
}
self.files.get(name).map(|data| data.len())
}

pub fn remove(&mut self, name: &str) -> Option<Vec<u8>> {
return self.files.remove(name);
self.files.remove(name)
}

pub fn iter(&self) -> impl Iterator<Item = (&'_ str, &'_ [u8])> {
Expand Down
16 changes: 8 additions & 8 deletions crates/js-component-bindgen/src/function_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl FunctionBindgen<'_> {

fn intrinsic(&mut self, intrinsic: Intrinsic) -> String {
self.intrinsics.insert(intrinsic);
return intrinsic.name().to_string();
intrinsic.name().to_string()
}

fn clamp_guest<T>(&mut self, results: &mut Vec<String>, operands: &[String], min: T, max: T)
Expand Down Expand Up @@ -149,7 +149,7 @@ impl Bindgen for FunctionBindgen<'_> {
type Operand = String;

fn sizes(&self) -> &SizeAlign {
&self.sizes
self.sizes
}

fn push_block(&mut self) {
Expand Down Expand Up @@ -351,7 +351,7 @@ impl Bindgen for FunctionBindgen<'_> {
for (field, op) in record.fields.iter().zip(operands) {
result.push_str(&format!("{}: {},\n", field.name.to_lower_camel_case(), op));
}
result.push_str("}");
result.push('}');
results.push(result);
}

Expand Down Expand Up @@ -534,7 +534,7 @@ impl Bindgen for FunctionBindgen<'_> {
assert!(block_results.len() == 1);
uwriteln!(self.src, " val: {}", block_results[0]);
} else {
assert!(block_results.len() == 0);
assert!(block_results.is_empty());
}
uwriteln!(
self.src,
Expand Down Expand Up @@ -612,7 +612,7 @@ impl Bindgen for FunctionBindgen<'_> {
Instruction::OptionLift { payload, .. } => {
let (some, some_results) = self.blocks.pop().unwrap();
let (none, none_results) = self.blocks.pop().unwrap();
assert!(none_results.len() == 0);
assert!(none_results.is_empty());
assert!(some_results.len() == 1);
let some_result = &some_results[0];

Expand Down Expand Up @@ -716,14 +716,14 @@ impl Bindgen for FunctionBindgen<'_> {
let (ok, ok_results) = self.blocks.pop().unwrap();
let ok_result = if result.ok.is_some() {
assert_eq!(ok_results.len(), 1);
format!("{}", ok_results[0])
ok_results[0].to_string()
} else {
assert_eq!(ok_results.len(), 0);
String::from("undefined")
};
let err_result = if result.err.is_some() {
assert_eq!(err_results.len(), 1);
format!("{}", err_results[0])
err_results[0].to_string()
} else {
assert_eq!(err_results.len(), 0);
String::from("undefined")
Expand Down Expand Up @@ -1072,7 +1072,7 @@ impl Bindgen for FunctionBindgen<'_> {
}

// after a high level call, we need to deactivate the component resource borrows
if self.cur_resource_borrows.len() > 0 {
if !self.cur_resource_borrows.is_empty() {
let symbol_resource_handle = self.intrinsic(Intrinsic::SymbolResourceHandle);
for resource in &self.cur_resource_borrows {
uwriteln!(self.src, "{resource}[{symbol_resource_handle}] = null;");
Expand Down
18 changes: 9 additions & 9 deletions crates/js-component-bindgen/src/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ impl<'a> LocalNames {
let goal_name = if let Some(last_char) = goal_name.rfind('/') {
&goal_name[last_char + 1..]
} else {
&goal_name
goal_name
};
let mut goal = to_js_identifier(goal_name);
if self.local_names.contains(&goal) {
let mut idx = 1;
loop {
let valid_name_suffixed = format!("{goal}${}", idx.to_string());
let valid_name_suffixed = format!("{goal}${idx}");
if !self.local_names.contains(&valid_name_suffixed) {
goal = valid_name_suffixed;
break;
Expand Down Expand Up @@ -75,7 +75,7 @@ pub fn to_js_identifier(goal_name: &str) -> String {
let goal = goal_name.to_lower_camel_case();
let mut identifier = String::new();
for char in goal.chars() {
let valid_char = if identifier.len() == 0 {
let valid_char = if identifier.is_empty() {
is_js_identifier_start(char)
} else {
is_js_identifier_char(char)
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn is_js_identifier(s: &str) -> bool {
} else {
return false;
}
while let Some(char) = chars.next() {
for char in chars {
if !is_js_identifier_char(char) {
return false;
}
Expand All @@ -119,21 +119,21 @@ pub fn is_js_identifier(s: &str) -> bool {
// https://tc39.es/ecma262/#prod-IdentifierStartChar
// Unicode ID_Start | "$" | "_"
fn is_js_identifier_start(code: char) -> bool {
return match code {
match code {
'A'..='Z' | 'a'..='z' | '$' | '_' => true,
// leaving out non-ascii for now...
_ => false,
};
}
}

// https://tc39.es/ecma262/#prod-IdentifierPartChar
// Unicode ID_Continue | "$" | U+200C | U+200D
fn is_js_identifier_char(code: char) -> bool {
return match code {
match code {
'0'..='9' | 'A'..='Z' | 'a'..='z' | '$' | '_' => true,
// leaving out non-ascii for now...
_ => false,
};
}
}

pub fn maybe_quote_id(name: &str) -> String {
Expand All @@ -154,7 +154,7 @@ pub fn maybe_quote_member(name: &str) -> String {
}
}

pub(crate) const RESERVED_KEYWORDS: &'static [&'static str] = &[
pub(crate) const RESERVED_KEYWORDS: &[&str] = &[
"await",
"break",
"case",
Expand Down
Loading

0 comments on commit b2ee3d8

Please sign in to comment.