rust-lang / rust-lang/rust-clippy
Rust 2024 match ergonomics and `needless_borrowed_reference`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
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 muton 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:
- https://github.com/rust-lang/rust-clippy/issues/10245
- https://github.com/rust-lang/rust-clippy/issues/11566
- https://github.com/rust-lang/rust-clippy/issues/13035
cc @Nadrieril @joshtriplett
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by examining the needless_borrowed_reference lint and the linked Rust issue 131414, then review the related Clippy issues 10245, 11566, and 13035. Determine whether the lint should become allow-by-default for Rust 2024 migration cases; done means the team’s chosen default behavior is implemented and covered by the relevant lint tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100