Skip to content

len_without_is_empty fires on setters/builders #7191

Closed
@wchargin

Description

@wchargin

Lint name: len_without_is_empty

I tried this code:

#[derive(Default)]
pub struct ZerosBuilder(usize);

impl ZerosBuilder {
    pub fn len(&mut self, len: usize) -> &mut Self {
        self.0 = len;
        self
    }
    pub fn build(&self) -> Vec<u8> {
        vec![0; self.0]
    }
}

I expected to see this happen: no warning, because len is a setter.

Instead, this happened:

warning: struct `ZerosBuilder` has a public `len` method, but no `is_empty` method
 --> src/lib.rs:5:5
  |
5 |     pub fn len(&mut self, len: usize) -> &mut Self {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(clippy::len_without_is_empty)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#len_without_is_empty

warning: 1 warning emitted

Actual code in the wild:
https://github.com/tensorflow/tensorboard/blob/66e05f7cace53fec4a53f70144419a28f36633c5/tensorboard/data/server/commit.rs#L358

This is buggy in 0.1.52 (88f19c6), but fixed in 0.1.53 beta (4bac69d),
presumably due to #6980. Just filing for posterity.

Meta

$ cargo clippy -V
clippy 0.1.52 (88f19c6 2021-05-03)
$ rustc -Vv
rustc 1.52.0 (88f19c6da 2021-05-03)
binary: rustc
commit-hash: 88f19c6dab716c6281af7602e30f413e809c5974
commit-date: 2021-05-03
host: x86_64-unknown-linux-gnu
release: 1.52.0
LLVM version: 12.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions