Skip to content
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

len_without_is_empty fires on setters/builders #7191

Closed
wchargin opened this issue May 8, 2021 · 1 comment
Closed

len_without_is_empty fires on setters/builders #7191

wchargin opened this issue May 8, 2021 · 1 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

Comments

@wchargin
Copy link
Contributor

wchargin commented May 8, 2021

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
@wchargin wchargin 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 May 8, 2021
@wchargin
Copy link
Contributor Author

wchargin commented May 8, 2021

As noted in the issue body, this is already fixed.

@wchargin wchargin closed this as completed May 8, 2021
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
Projects
None yet
Development

No branches or pull requests

1 participant