rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.48k stars 1.55k forks source link

clippy issue with #[wasm_bindgen] #13606

Open WenqingZong opened 3 weeks ago

WenqingZong commented 3 weeks ago

Summary

Hi, found this message in cargo clippy output so post the issue here.

Original code: https://github.com/WenqingZong/wasm-game-of-life/pull/1/commits/b388612cddb1e5507bb580f979dfcb8e030de491 just git checkout it.

Cargo Clippy output:

➜  wasm-game-of-life git:(version1) cargo clippy
    Checking once_cell v1.20.2
    Checking cfg-if v1.0.0
    Checking wasm-bindgen v0.2.95
    Checking wasm-game-of-life v0.1.0 (/Users/wenqingzong/Projects/wasm-game-of-life)
warning: using `write!()` with a format string that ends in a single newline
  --> src/lib.rs:26:13
   |
26 |             write!(f, "\n")?;
   |             ^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#write_with_newline
   = note: `#[warn(clippy::write_with_newline)]` on by default
help: use `writeln!` instead
   |
26 -             write!(f, "\n")?;
26 +             writeln!(f)?;
   |

warning: you should consider adding a `Default` implementation for `Universe`
  --> src/lib.rs:35:5
   |
35 | /     pub fn new() -> Universe {
36 | |         let width = 64;
37 | |         let height = 64;
...  |
53 | |         }
54 | |     }
   | |_____^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default
   = note: `#[warn(clippy::new_without_default)]` on by default
help: try adding this
   |
34 + impl Default for Universe {
35 +     fn default() -> Self {
36 +         Self::new()
37 +     }
38 + }
   |

warning: `wasm-game-of-life` (lib) generated 2 warnings (run `cargo clippy --fix --lib -p wasm-game-of-life` to apply 2 suggestions)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.06s

➜  wasm-game-of-life git:(version1) cargo clippy --fix --lib -p wasm-game-of-life
    Checking wasm-game-of-life v0.1.0 (/Users/wenqingzong/Projects/wasm-game-of-life)
warning: failed to automatically apply fixes suggested by rustc to crate `wasm_game_of_life`

after fixes were automatically applied the compiler reported errors within these files:

  * src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: #[wasm_bindgen] trait impls are not supported
  --> src/lib.rs:34:6
   |
34 | impl Default for Universe {
   |      ^^^^^^^

error: aborting due to 1 previous error

Original diagnostics will follow.

warning: using `write!()` with a format string that ends in a single newline
  --> src/lib.rs:26:13
   |
26 |             write!(f, "\n")?;
   |             ^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#write_with_newline
   = note: `#[warn(clippy::write_with_newline)]` on by default
help: use `writeln!` instead
   |
26 -             write!(f, "\n")?;
26 +             writeln!(f)?;
   |

warning: you should consider adding a `Default` implementation for `Universe`
  --> src/lib.rs:35:5
   |
35 | /     pub fn new() -> Universe {
36 | |         let width = 64;
37 | |         let height = 64;
...  |
53 | |         }
54 | |     }
   | |_____^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default
   = note: `#[warn(clippy::new_without_default)]` on by default
help: try adding this
   |
34 + impl Default for Universe {
35 +     fn default() -> Self {
36 +         Self::new()
37 +     }
38 + }
   |

warning: `wasm-game-of-life` (lib) generated 2 warnings (run `cargo clippy --fix --lib -p wasm-game-of-life` to apply 2 suggestions)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s

Reproducer

Code: https://github.com/WenqingZong/wasm-game-of-life/pull/1/commits/b388612cddb1e5507bb580f979dfcb8e030de491

Step to reproduce: cargo clippy --fix --lib -p wasm-game-of-life

Version

rustc 1.82.0 (f6e511eec 2024-10-15) binary: rustc commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14 commit-date: 2024-10-15 host: aarch64-apple-darwin release: 1.82.0 LLVM version: 19.1.1

Additional Labels

No response

GnomedDev commented 3 weeks ago

The error is caused by the impl Default for Universe being generated in-between the #[wasm_bindgen] attribute applied to the impl and the impl itself, leading to the impl Default for Universe getting the #[wasm_bindgen] instead and erroring.