rust-lang / rust-lang/rust-clippy

FP redundant_pattern lifetime mismatch

Open
#14,928 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

.

Lint Name

redundant_pattern

Reproducer

I tried this code:

#![warn(clippy::redundant_pattern)]
fn _f(mut p @ _: &mut i32) {
    let mut number = 111;
    p = &mut number;
    *p = 2;
    println!("{}", *p);
}

pub fn main() {}

I saw this happen:

warning: the `p @ _` pattern can be written as just `p`
 --> src/main.rs:2:7
  |
2 | fn _f(mut p @ _: &mut i32) {
  |       ^^^^^^^^^ help: try: `mut p`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![warn(clippy::redundant_pattern)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^

the suggested code

#![warn(clippy::redundant_pattern)]
fn _f(mut p: &mut i32) {
    let mut number = 111;
    p = &mut number;
    *p = 2;
    println!("{}", *p);
}

pub fn main() {}

does not compile:

warning: value passed to `p` is never read
 --> src/main.rs:2:11
  |
2 | fn _f(mut p: &mut i32) {
  |           ^
  |
  = help: maybe it is overwritten before being read?
  = note: `#[warn(unused_assignments)]` on by default

error[E0597]: `number` does not live long enough
 --> src/main.rs:4:9
  |
2 | fn _f(mut p: &mut i32) {
  |              - let's call the lifetime of this reference `'1`
3 |     let mut number = 111;
  |         ---------- binding `number` declared here
4 |     p = &mut number;
  |     ----^^^^^^^^^^^
  |     |   |
  |     |   borrowed value does not live long enough
  |     assignment requires that `number` is borrowed for `'1`
...
7 | }
  | - `number` dropped here while still borrowed

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

Additional Labels

No response

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 running the Rust reproducer with the redundant_pattern lint enabled and compare the original code with the suggested replacement. Trace the lint's suggestion handling to ensure removing the pattern does not produce a lifetime-invalid program; done means the suggestion remains valid for this case and the regression is covered by an appropriate test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.