Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redundant_slicing false positive on bytes::buf::UninitSlice move #6968

Closed
roblabla opened this issue Mar 25, 2021 · 3 comments · Fixed by #6975
Closed

redundant_slicing false positive on bytes::buf::UninitSlice move #6968

roblabla opened this issue Mar 25, 2021 · 3 comments · Fixed by #6975
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@roblabla
Copy link
Contributor

roblabla commented Mar 25, 2021

Lint name: redundant_slicing

While updating rust to 1.51, we hit a false positive on one of the new lints. Here's a minimal reproducer:

use bytes::*; // 1.0.1

fn main() {
    let mut storage = &mut [0u8; 512][..];
    let bytes = storage.chunk_mut();

    {
        let mut _bytes_msg = &mut bytes[..];
    }

    println!("{:?}", bytes);    
}

I expected to see this happen: No lint on the slicing at &mut bytes[..], as it is required: instead of moving the bytes variable to _bytes_msg, thus shortening its lifetime, it will instead create a new borrow with its own lifetime inside of _bytes_msg.

Instead, this happened: Clippy complained about redundant slicing:

warning: redundant slicing of the whole range
 --> src/main.rs:8:30
  |
8 |         let mut _bytes_msg = &mut bytes[..];
  |                              ^^^^^^^^^^^^^^ help: use the original slice instead: `bytes`
  |
  = note: `#[warn(clippy::redundant_slicing)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_slicing

warning: 1 warning emitted

Note that this does not reproduce when using a normal &mut [u8] slice. But bytes.chunk_mut() returns its own slice-like object UninitSlice, on which this rule triggers.

Meta

  • cargo clippy -V: 0.1.52 (2021-03-24 07e0e2e)
  • rustc -Vv:
    rustc 1.51.0 (2fd73fabe 2021-03-23)
    binary: rustc
    commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0
    commit-date: 2021-03-23
    host: x86_64-unknown-linux-gnu
    release: 1.51.0
    LLVM version: 11.0.1
    
@roblabla roblabla added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 25, 2021
@koivunej
Copy link
Contributor

koivunej commented Mar 25, 2021

@Jarcho
Copy link
Contributor

Jarcho commented Mar 25, 2021

I'll deal with this later today.

@rustbot claim

@Jarcho
Copy link
Contributor

Jarcho commented Mar 26, 2021

This isn't a false positive, but a bad suggestion. Correct code is:

use bytes::*; // 1.0.1

fn main() {
    let mut storage = &mut [0u8; 512][..];
    let bytes = storage.chunk_mut();

    {
        let mut _bytes_msg = &mut *bytes;
    }

    println!("{:?}", bytes);    
}

Basically the indexing is being used to reborrow the reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants