Skip to content

Clippy interprets Bytes type incorrectly as interior mutable which triggers errors on several lints #5812

Closed
@Matthias247

Description

@Matthias247

I tried this code:

use bytes::Bytes;

const MY_BYTES: Bytes = Bytes::from_static(b"asdf");

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=dec0c4a3d84023fdc3e8b27c6ed90c88

I expected to see this happen: Everything compiles fine and clippy checks succeed

Instead, this happened: Clippy emits a bunch of errors which abort compilation. For this particular piece of code it reports

error: a `const` item should never be interior mutable
 --> src/lib.rs:3:1
  |
3 | const MY_BYTES: Bytes = Bytes::from_static(b"asdf");
  | -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | |
  | make this a static item (maybe with lazy_static)
  |
  = note: `#[deny(clippy::declare_interior_mutable_const)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const

However in other contexts - e.g. when I use Bytes as a key type in a map - I also get the mutable_key_type and borrow_interior_mutable_const errors.

I guess clippy doesn't understand the layout of Bytes, which uses interior raw pointers and a vtable: https://github.com/tokio-rs/bytes/blob/6fdb7391ce83dc71ccaeda6a54211ce723e5d9a5/src/bytes.rs#L71-L77

While it is true that some data could be modified even from an immutable reference - e.g. due to a refcount change - the changes have no impact on the actual payload of the Bytes type - it always is an immutable byte array. Therefore I think the lints are wrongly applied here.

I'm however not sure if this is something that would need to be fixed in clippy or could be fixed in Bytes

Meta

  • cargo clippy -V: e.g. 0.0.212 (2020-07-15 7e11379)
  • rustc -Vv:
    rustc 1.44.1 stable
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions