Skip to content

False positive redundant_slicing report when slicing as immutable slice to invoke Read trait methods taking (&mut self) #7972

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

Closed
EdMcBane opened this issue Nov 13, 2021 · 0 comments · Fixed by #8218
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@EdMcBane
Copy link

Lint name: redundant_slicing

I tried this code:

    let data = vec![1,2,3];
    let src: &[u8] = &data;
    let mut dest = vec![];
    (&src[..]).read_to_end(&mut dest).unwrap();

I expected no lint warnings, due to the fact that the slicing in the last line is needed to call read_to_end on the immutable slice src.

Instead clippy reports redundant slicing of the whole range

warning: redundant slicing of the whole range
 --> src/main.rs:6:5
  |
6 |     (&src[..]).read_to_end(&mut dest).unwrap();
  |     ^^^^^^^^^^ help: use the original value instead: `src`
  |
  = 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

Consequently, cargo clippy --fix fails with:

fdegrassi@homerunner:/tmp/asd$ cargo clippy --fix
    Checking asd v0.1.0 (/tmp/asd)
warning: failed to automatically apply fixes suggested by rustc to crate `asd`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see 
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0596]: cannot borrow `src` as mutable, as it is not declared as mutable
 --> src/main.rs:6:5
  |
4 |     let src: &[u8] = &data;
  |         --- help: consider changing this to be mutable: `mut src`
5 |     let mut dest = vec![];
6 |     src.read_to_end(&mut dest).unwrap();
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
Original diagnostics will follow.

warning: redundant slicing of the whole range
 --> src/main.rs:6:5
  |
6 |     (&src[..]).read_to_end(&mut dest).unwrap();
  |     ^^^^^^^^^^ help: use the original value instead: `src`
  |
  = 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: `asd` (bin "asd") generated 1 warning
warning: `asd` (bin "asd" test) generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 0.07s

Meta

Rust version (rustc -Vv):

rustc 1.58.0-nightly (ff0e14829 2021-10-31)
binary: rustc
commit-hash: ff0e14829e1806ca0d4226595f7fdf3e8658758f
commit-date: 2021-10-31
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0

@rustbot label +I-suggestion-causes-error

@EdMcBane EdMcBane 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 Nov 13, 2021
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Nov 13, 2021
@bors bors closed this as completed in 668b3e4 Feb 17, 2022
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
2 participants