rust-lang / rust-lang/rust-clippy

useless_asref dosent understand ` &mut &'a`.

Open
#10,790 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 is saying to remove the "useless_asref" even though it does not work without it.

Lint Name

useless_asref

Reproducer

I tried this code:

fn test_fn<'a>(data: &mut &'a [u8], take: usize) -> &'a [u8] {
    let (val, remaining) = data.split_at(take);
        *data = remaining;
        val
}

fn main(){
    let data : &[u8] = &[0u8,1,2,3,4,5];
    test_fn(&mut data.as_ref(), 2);
}

I saw this happen:

this call to `as_ref` does nothing

I expected to see this happen:
Should not show lint as it does not work without it.

Version
rustc 1.68.2 (9eb3afe9e 2023-03-27)
binary: rustc
commit-hash: 9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0
commit-date: 2023-03-27
host: x86_64-pc-windows-msvc
release: 1.68.2
LLVM version: 15.0.6
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 with the useless_asref lint and the Rust reproducer in this issue, then run Clippy against it to confirm the warning. Trace why the lint treats &mut &'a [u8] and as_ref() as equivalent; done means the valid call no longer produces a false warning and the reproducer is covered by a regression test.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.