rust-lang / rust-lang/rust-clippy

useless_asref false positive on `&mut &[T]`

Open
#8,566 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

Clippy complains about calling as_ref() on a mut slice when it shouldn't.

Lint Name

useless_asref

Reproducer
use std::{error::Error, io::BufRead};

fn main() {
  let buf = get_buf();

  // clippy complains with: "this call to `as_ref` does nothing"
  test(&mut buf.as_ref()).unwrap();

  // this is fine
  test(&mut &*buf).unwrap();
}

fn get_buf<'a>() -> &'a mut [u8] {
  todo!()
}

fn test<R>(_input: &mut R) -> Result<(), Box<dyn Error>>
where
  R: BufRead + Send + Sync,
{
  Ok(())
}
Version
rustc 1.59.0 (9d1b2106e 2022-02-23)
binary: rustc
commit-hash: 9d1b2106e23b1abd32fce1f17267604a5102f57a
commit-date: 2022-02-23
host: x86_64-unknown-linux-gnu
release: 1.59.0
LLVM version: 13.0.0
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 supplied Rust reproducer and locating the implementation and tests for the useless_asref lint. The fix is done when the &mut &[u8] case no longer produces a false positive while the existing valid-case behavior remains covered.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.