Skip to content

Commit

Permalink
Clippy: Fixes and CI (#3300)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslihotzki committed May 17, 2023
1 parent 2d882c9 commit 85f72c9
Show file tree
Hide file tree
Showing 1,642 changed files with 2,175 additions and 612 deletions.
13 changes: 13 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,16 @@
# that.
[target.wasm32-unknown-unknown]
runner = 'cargo run -p wasm-bindgen-cli --bin wasm-bindgen-test-runner --'

[target.'cfg(all())']
rustflags = [
"-Adead_code",
"-Anon_upper_case_globals",
"-Aunused_doc_comments",
"-Aclippy::large_enum_variant",
"-Aclippy::missing_safety_doc",
"-Aclippy::new_without_default",
"-Aclippy::overly_complex_bool_expr",
"-Aclippy::too_many_arguments",
"-Aclippy::type_complexity"
]
30 changes: 30 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ jobs:
- run: rustup update --no-self-update stable && rustup default stable
- run: cargo check --all

# Run `cargo clippy` over everything
clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: rustup update --no-self-update stable && rustup default stable
- run: rustup target add wasm32-unknown-unknown
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-backend -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-cli -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-cli-support -- -D warnings
- run: cargo clippy --no-deps --all-features -p example-tests -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-externref-xform -- -D warnings
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p wasm-bindgen-futures -- -D warnings
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p js-sys -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-macro -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-macro-support -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-multi-value-xform -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-shared -- -D warnings
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p wasm-bindgen-test -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-test-macro -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-threads-xform -- -D warnings
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p typescript-tests -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-wasm-conventions -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-wasm-interpreter -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-webidl -- -D warnings
- run: cargo clippy --no-deps --all-features -p webidl-tests -- -D warnings
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p web-sys -- -D warnings
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -- -D warnings

test_wasm_bindgen:
name: "Run wasm-bindgen crate tests (unix)"
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ description = """
Easy support for interacting between JS and Rust.
"""
edition = "2018"
rust-version = "1.56"

[package.metadata.docs.rs]
features = ["serde-serialize"]
Expand Down
1 change: 1 addition & 0 deletions crates/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = """
Backend code generation of the wasm-bindgen tool
"""
edition = "2018"
rust-version = "1.56"

[features]
spans = []
Expand Down
10 changes: 6 additions & 4 deletions crates/backend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ impl Program {
/// In contrast to Program, LinkToModule must expand to an expression.
/// linked_modules of the inner Program must contain exactly one element
/// whose link is produced by the expression.
#[cfg_attr(feature = "extra-traits", derive(Debug))]
#[derive(Clone)]
pub struct LinkToModule(pub Program);

/// A rust to js interface. Allows interaction with rust objects/functions
Expand Down Expand Up @@ -349,7 +351,7 @@ pub struct Function {
}

/// Information about a Struct being exported
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[cfg_attr(feature = "extra-traits", derive(Debug))]
#[derive(Clone)]
pub struct Struct {
/// The name of the struct in Rust code
Expand All @@ -369,7 +371,7 @@ pub struct Struct {
}

/// The field of a struct
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[cfg_attr(feature = "extra-traits", derive(Debug))]
#[derive(Clone)]
pub struct StructField {
/// The name of the field in Rust code
Expand Down Expand Up @@ -464,10 +466,10 @@ impl Export {
pub(crate) fn rust_symbol(&self) -> Ident {
let mut generated_name = String::from("__wasm_bindgen_generated");
if let Some(class) = &self.js_class {
generated_name.push_str("_");
generated_name.push('_');
generated_name.push_str(class);
}
generated_name.push_str("_");
generated_name.push('_');
generated_name.push_str(&self.function.name.to_string());
Ident::new(&generated_name, Span::call_site())
}
Expand Down
6 changes: 3 additions & 3 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl ToTokens for ast::StructField {
.to_tokens(tokens);

Descriptor {
ident: &getter,
ident: getter,
inner: quote! {
<#ty as WasmDescribe>::describe();
},
Expand Down Expand Up @@ -1276,7 +1276,7 @@ impl<'a> ToTokens for DescribeImport<'a> {
#inform_ret
},
attrs: f.function.rust_attrs.clone(),
wasm_bindgen: &self.wasm_bindgen,
wasm_bindgen: self.wasm_bindgen,
}
.to_tokens(tokens);
}
Expand Down Expand Up @@ -1377,7 +1377,7 @@ impl ToTokens for ast::ImportStatic {
.to_tokens(into);

Descriptor {
ident: &shim_name,
ident: shim_name,
inner: quote! {
<#ty as WasmDescribe>::describe();
},
Expand Down
17 changes: 7 additions & 10 deletions crates/backend/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ impl Interner {
return Ok(ImportModule::Named(self.intern_str(&file.new_identifier)));
}
self.check_for_package_json();
let path = if id.starts_with("/") {
self.root.join(&id[1..])
let path = if let Some(id) = id.strip_prefix('/') {
self.root.join(id)
} else if id.starts_with("./") || id.starts_with("../") {
let msg = "relative module paths aren't supported yet";
return Err(Diagnostic::span_error(span, msg));
Expand Down Expand Up @@ -144,7 +144,7 @@ fn shared_program<'a>(
typescript_custom_sections: prog
.typescript_custom_sections
.iter()
.map(|x| -> &'a str { &x })
.map(|x| -> &'a str { x })
.collect(),
linked_modules: prog
.linked_modules
Expand Down Expand Up @@ -186,13 +186,10 @@ fn shared_export<'a>(
export: &'a ast::Export,
intern: &'a Interner,
) -> Result<Export<'a>, Diagnostic> {
let consumed = match export.method_self {
Some(ast::MethodSelf::ByValue) => true,
_ => false,
};
let consumed = matches!(export.method_self, Some(ast::MethodSelf::ByValue));
let method_kind = from_ast_method_kind(&export.function, intern, &export.method_kind)?;
Ok(Export {
class: export.js_class.as_ref().map(|s| &**s),
class: export.js_class.as_deref(),
comments: export.comments.iter().map(|s| &**s).collect(),
consumed,
function: shared_function(&export.function, intern),
Expand Down Expand Up @@ -410,7 +407,7 @@ impl Encode for usize {
impl<'a> Encode for &'a [u8] {
fn encode(&self, dst: &mut Encoder) {
self.len().encode(dst);
dst.dst.extend_from_slice(*self);
dst.dst.extend_from_slice(self);
}
}

Expand All @@ -420,7 +417,7 @@ impl<'a> Encode for &'a str {
}
}

impl<'a> Encode for String {
impl Encode for String {
fn encode(&self, dst: &mut Encoder) {
self.as_bytes().encode(dst);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/backend/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Diagnostic {
/// Attempt to generate a `Diagnostic` from a vector of other `Diagnostic` instances.
/// If the `Vec` is empty, returns `Ok(())`, otherwise returns the new `Diagnostic`
pub fn from_vec(diagnostics: Vec<Diagnostic>) -> Result<(), Diagnostic> {
if diagnostics.len() == 0 {
if diagnostics.is_empty() {
Ok(())
} else {
Err(Diagnostic {
Expand Down
10 changes: 5 additions & 5 deletions crates/backend/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ use crate::ast;
use proc_macro2::{self, Ident};

/// Check whether a given `&str` is a Rust keyword
#[rustfmt::skip]
fn is_rust_keyword(name: &str) -> bool {
match name {
matches!(name,
"abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue"
| "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | "if"
| "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut"
| "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | "return"
| "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | "true"
| "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | "while"
| "yield" | "bool" | "_" => true,
_ => false,
}
| "yield" | "bool" | "_"
)
}

/// Create an `Ident`, possibly mangling it if it conflicts with a Rust keyword.
pub fn rust_ident(name: &str) -> Ident {
if name == "" {
if name.is_empty() {
panic!("tried to create empty Ident (from \"\")");
} else if is_rust_keyword(name) {
Ident::new(&format!("{}_", name), proc_macro2::Span::call_site())
Expand Down
1 change: 1 addition & 0 deletions crates/cli-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = """
Shared support for the wasm-bindgen-cli package, an internal dependency
"""
edition = '2018'
rust-version = "1.56"

[dependencies]
anyhow = "1.0"
Expand Down
8 changes: 4 additions & 4 deletions crates/cli-support/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ pub trait Decode<'src>: Sized {

fn decode_all(mut data: &'src [u8]) -> Self {
let ret = Self::decode(&mut data);
assert!(data.len() == 0);
return ret;
assert!(data.is_empty());
ret
}
}

fn get<'a>(b: &mut &'a [u8]) -> u8 {
fn get(b: &mut &[u8]) -> u8 {
let r = b[0];
*b = &b[1..];
return r;
r
}

impl<'src> Decode<'src> for bool {
Expand Down
2 changes: 1 addition & 1 deletion crates/cli-support/src/descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl WasmBindgenDescriptorsSection {
replacement: FunctionId,
}

impl<'a> VisitorMut for UpdateDescribeClosure {
impl VisitorMut for UpdateDescribeClosure {
fn visit_call_mut(&mut self, call: &mut Call) {
if call.func == self.wbindgen_describe_closure {
call.func = self.replacement;
Expand Down
Loading

0 comments on commit 85f72c9

Please sign in to comment.