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 --fix generates unbuildable code for a useless_vec fix #14531

Open
martinling opened this issue Apr 3, 2025 · 2 comments · May be fixed by #14503
Open

clippy --fix generates unbuildable code for a useless_vec fix #14531

martinling opened this issue Apr 3, 2025 · 2 comments · May be fixed by #14503
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

@martinling
Copy link

Summary

Seen with Rust 1.85.1 when running clippy --fix on Packetry. at commit greatscottgadgets/packetry@e7c9dcc .

The problem involves a useless use of Vec, a reference to which is passed into a function that takes &[T].

When an array is used instead as suggested, the suggestion omits the necessary & operator.

Reproducer

Here's a minimised reproducer:

#[allow(clippy::result_unit_err)]
pub fn validate_packet(_packet: &[u8]) -> Result<(), ()> {
    Ok(())
}

fn main() {
    let packet = &vec![1, 2, 3, 4];
    assert_eq!(validate_packet(packet), Ok(()));
}

Running clippy-driver on this code results in:

warning: useless use of `vec!`
 --> src/usb.rs:7:18
  |
7 |     let packet = &vec![1, 2, 3, 4];
  |                  ^^^^^^^^^^^^^^^^^ help: you can use an array directly: `[1, 2, 3, 4]`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec
  = note: `#[warn(clippy::useless_vec)]` on by default

warning: 1 warning emitted

If the suggestion is applied, then compilation fails:

error[E0308]: mismatched types
 --> src/usb.rs:8:32
  |
8 |     assert_eq!(validate_packet(packet), Ok(()));
  |                --------------- ^^^^^^ expected `&[u8]`, found `[{integer}; 4]`
  |                |
  |                arguments to this function are incorrect
  |
note: function defined here
 --> src/usb.rs:2:8
  |
2 | pub fn validate_packet(_packet: &[u8]) -> Result<(), ()> {
  |        ^^^^^^^^^^^^^^^ --------------
help: consider borrowing here
  |
8 |     assert_eq!(validate_packet(&packet), Ok(()));
  |                                +

error: aborting due to 1 previous error

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

The correct suggestion would be &[1, 2, 3, 4] which compiles successfully.

Version

rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-unknown-linux-gnu
release: 1.85.1
LLVM version: 19.1.7

Additional Labels

@rustbot label I-suggestion-causes-error

@martinling martinling added the C-bug Category: Clippy is not doing the correct thing label Apr 3, 2025
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 3, 2025
@y21
Copy link
Member

y21 commented Apr 3, 2025

Looks like the refactoring I've done in #14503 also happened to (indirectly) fix this. Will add a test for this later.

warning: useless use of `vec!`
 --> x.rs:7:18
  |
7 |     let packet = &vec![1, 2, 3, 4];
  |                  ^^^^^^^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2, 3, 4]`
  |

@martinling
Copy link
Author

Just confirmed that this is broken in the new 1.86.0 too.

@y21 y21 linked a pull request Apr 3, 2025 that will close this issue
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.

3 participants