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

Clippy Creates broken code, while transforming .iter().filter().cloned().collect() into retain(). #11457

Closed
FractalFir opened this issue Sep 3, 2023 · 3 comments · Fixed by #12084
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@FractalFir
Copy link

FractalFir commented Sep 3, 2023

Summary

clippy fix failed with this output:
warning: failed to automatically apply fixes suggested by rustc to crate rustc_codegen_clr

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

  * src/clr_method.rs
  * src/statement.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:
warning: unused macro definition: `sign_cast`
  --> src/clr_method.rs:11:14
   |
11 | macro_rules! sign_cast {
   |              ^^^^^^^^^
   |
   = note: `#[warn(unused_macros)]` on by default

warning: consider removing unnecessary double parentheses
  --> src/statement.rs:16:9
   |
16 |         (<$dest>::from_ne_bytes(($var as $src).to_ne_bytes()))
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
82 |         Type::I32 => vec![BaseIR::LDConstI32(sign_cast!(scalar_u128, u32, i32))],
   |                                              --------------------------------- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#double_parens
   = note: `#[warn(clippy::double_parens)]` on by default
   = note: this warning originates in the macro `sign_cast` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: consider removing unnecessary double parentheses
  --> src/statement.rs:16:9
   |
16 |         (<$dest>::from_ne_bytes(($var as $src).to_ne_bytes()))
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
86 |         Type::I64 => vec![BaseIR::LDConstI64(sign_cast!(scalar_u128, u64, i64))],
   |                                              --------------------------------- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#double_parens
   = note: this warning originates in the macro `sign_cast` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0614]: type `base_ir::BaseIR` cannot be dereferenced
   --> src/clr_method.rs:116:30
    |
116 |         self.ops.retain(|op| **op != BaseIR::Nop);
    |                              ^^^^

error: aborting due to previous error; 3 warnings emitted

For more information about this error, try `rustc --explain E0614`.
Original diagnostics will follow.

Reproducer

Examples come from my project, and I have tagged the particular version of the source code which clippy had issues with. I had tried to extract and simplify the example, but so far I could not trigger it with simpler, isolated examples.

Clippy transformed this piece of code:

self.ops = self
            .ops
            .iter()
            .filter(|op| **op != BaseIR::Nop)
            .cloned()
            .collect();

Into this broken code:

self.ops.retain(|op|**op != BaseIR::Nop)

In the original filter, the type of op is &&BaseIR, but in the version using retain type of op is &BaseIR.
It seems like Clippy did not take that into account while changing the code.

A proper translation would look like this:

self.ops.retain(|op|*op != BaseIR::Nop)

Version

rustc 1.74.0-nightly (9f5fc1bd4 2023-09-02)
binary: rustc
commit-hash: 9f5fc1bd443f59583e7af0d94d289f95fe1e20c4
commit-date: 2023-09-02
host: x86_64-unknown-linux-gnu
release: 1.74.0-nightly
LLVM version: 17.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

@FractalFir FractalFir added the C-bug Category: Clippy is not doing the correct thing label Sep 3, 2023
@rustbot
Copy link
Collaborator

rustbot commented Sep 3, 2023

Error: Label I-suggestion-causes-error can only be set by Rust team members

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@Centri3 Centri3 added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Sep 3, 2023
@kiscad
Copy link
Contributor

kiscad commented Sep 15, 2023

A simpler example code:

let mut vals = vec![1, 2, 3, 4];
vals = vals.iter().filter(|v| **v != 1).cloned().collect();

@yuxqiu
Copy link
Contributor

yuxqiu commented Jan 4, 2024

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants