Skip to content

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

Open
@martinling

Description

@martinling

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions