Incorrect info from clippy

Running cargo clippy on the below:

        const CAP: usize = 4096;
        let file = File::open("my_file.txt")?;
        let mut reader = BufReader::with_capacity(CAP, file);

I'm getting:

warning: constant is never used: `CAP`

Is this correct? I don't think so, but prefer to ask.
Thanks

I can't reproduce it on the playground. This example doesn't throw any clippy warning

Hi, Yes, I can see that. I thought that the toolchain version was a problem so I've updated to 1.49. But I'm still getting it.
Anyone?

Is the function containing the use unused?

2 Likes

I've sometimes wished for a less-strict lint from rustc that only flags non-transitively unused items, i.e. in this case the function that uses CAP could still be flagged while CAP itself would not. Of course the sharper version is important for code that's going to be released, but when I'm working on a crate that's still taking shape I usually want something between full #![warn(unused)] and #![allow(unused)].

What about 2 functions that are only called by each other?

In that case I'd expect neither function to be marked unused by this hypothetical lint. The decision procedure I want is:

  1. is this item marked pub? if so, it's not unused
  2. otherwise, is this item used by some item in the current crate? if so, it's not unused
  3. otherwise, mark it unused

(And again, this would be an alternative to the current unused lint, not a replacement.)