rust-lang / rust-lang/rust-clippy

len_zero false negative for default implementations of `is_empty` in traits

Open
#8,384 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

The len_zero lint is only emitted when the is_empty method is implemented explicitly for a type, and is not raised when said method is inherited as a default implementation from a trait

Lint Name

len_zero

Reproducer

I tried this code:

trait ListTrait {
    fn slice(&self) -> &[u8];
    
    fn len(&self) -> usize {
        self.slice().len()
    }
    
    fn is_empty(&self) -> bool {
        self.slice().is_empty()
    }
}

pub struct Wrapper(Vec<u8>);

impl ListTrait for Wrapper {
    fn slice(&self) -> &[u8] {
        &self.0
    }
}

pub fn check_empty(wrapper: &Wrapper) -> bool {
    wrapper.len() == 0
}

I expected to see this happen:
The wrapper.len() == 0 should raise the len_zero lint

Instead, this happened:
No warning were emitted

Version
rustc 1.58.0 (02072b482 2022-01-11)
binary: rustc
commit-hash: 02072b482a8b5357f7fb5e5637444ae30e423c40
commit-date: 2022-01-11
host: x86_64-pc-windows-msvc
release: 1.58.0
LLVM version: 13.0.0

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 implementation of the len_zero lint and reproduce the provided ListTrait example. Trace how the lint identifies len and is_empty implementations, then add coverage showing that a trait default implementation triggers the warning for wrapper.len() == 0; done means the reproducer emits the len_zero lint.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.