Description
In Rust 2024, we're making some changes to match ergonomics. Those changes are:
- Rule 1C: When the DBM (default binding mode) is not
move
(whether or not behind a reference), writingmut
,ref
, orref mut
on a binding is an error. - Rule 2C: Reference patterns can only match against references in the scrutinee when the DBM is
move
.
The net result of this is that we're pushing some code to use a fully-explicit match rather than using match ergonomics, and this is what the edition migration lints do.
Here's where clippy comes in. Consider:
let [ref _x] = &[0u8]; // _x: &u8
This is accepted in Rust 2021 and clippy does not warn about it even though that ref
in the pattern is redundant. We'll be rejecting this code in Rust 2024 under Rule 1C, and the migration lints will move it to the fully-explicit form:
fn main() {
let &[ref _x] = &[0u8]; // _x: &u8
//[clippy]~^ WARN dereferencing a slice pattern where every element takes a reference
//[clippy]~| NOTE `#[warn(clippy::needless_borrowed_reference)]` on by default
}
However, as noted above, clippy lints on that.
warning: dereferencing a slice pattern where every element takes a reference
--> src/main.rs:2:9
|
2 | let &[ref _x] = &[0u8]; // _x: &u8
| ^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrowed_reference
= note: `#[warn(clippy::needless_borrowed_reference)]` on by default
help: try removing the `&` and `ref` parts
|
2 - let &[ref _x] = &[0u8]; // _x: &u8
2 + let [_x] = &[0u8]; // _x: &u8
|
The suggestion given in this case is accurate, but it may be a bit unfortunate for clippy to lint against how rustc suggested rewriting the code. It's not practical for rustc to be more clever here; we really just want to migrate these cases to the corresponding fully-explicit form.
With my edition hat on, in the interest of making the adoption of Rust 2024 the most seamless for the most people, I'd probably prefer that this lint be made allow-by-default
by clippy before the release of Rust 2024, but I'm interested to hear what the @rust-lang/clippy team thinks about this and what options there may be here.
Tracking:
Other open issues about this lint: