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 failed to automatically apply fixes suggested by rustc #14088

Closed
nwoods-cimpress opened this issue Jan 27, 2025 · 0 comments · Fixed by #14090
Closed

Clippy failed to automatically apply fixes suggested by rustc #14088

nwoods-cimpress opened this issue Jan 27, 2025 · 0 comments · Fixed by #14090
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

@nwoods-cimpress
Copy link

Summary

Code in report could not be fixed by clippy

Reproducer

I tried to fix the following code with cargo clippy --fix. (Yes I know the code is braindead; this is not my original code)

pub fn foo(s: Option<&str>) {
    let s: Option<&str> = s.as_ref().map(|x| x.as_ref());
    println!("{:?}", s);
}

pub fn main() {
    foo(Some("blah"));
}

I expected clippy to handle it but I got the following message:

Instead, this happened:

warning: failed to automatically apply fixes suggested by rustc to crate `rust_test`

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

  * src/main.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[E0308]: mismatched types
 --> src/main.rs:2:27
  |
2 |     let s: Option<&str> = s.as_ref().map(|x| x);
  |            ------------   ^^^^^^^^^^^^^^^^^^^^^ expected `Option<&str>`, found `Option<&&str>`
  |            |
  |            expected due to this
  |
  = note: expected enum `std::option::Option<&_>`
             found enum `std::option::Option<&&_>`
help: try using `.map(|v| &**v)` to convert `std::option::Option<&&str>` to `std::option::Option<&str>`
  |
2 |     let s: Option<&str> = s.as_ref().map(|x| x).map(|v| &**v);
  |                                                ++++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.

Version

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-pc-windows-msvc
release: 1.83.0
LLVM version: 19.1.1

Additional Labels

No response

@nwoods-cimpress nwoods-cimpress added the C-bug Category: Clippy is not doing the correct thing label Jan 27, 2025
@samueltardieu samueltardieu added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jan 27, 2025
github-merge-queue bot pushed a commit that referenced this issue Feb 7, 2025
)

Removing the `.as_ref()` or `.as_mut()` as the top-level expression in a
closure may change the type of the result. In this case, it may be
better not to lint rather than proposing a fix that would not work.

changelog: [`useless_asref`]: do not remove the `.as_ref()` or
`.as_mut()` call if this would change the type of the enclosing closure

Fix #14088
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.

2 participants