rust-lang / rust-lang/rust-clippy

Rust 2024 match ergonomics and `needless_borrowed_reference`

Open
#13,616 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-category C-enhancement D-confusing S-needs-discussion
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), writing mut, ref, or ref 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:

cc @Nadrieril @joshtriplett

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.