Skip to content

Interior mutable const lints trigger on HeaderValue constants #7752

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
sfackler opened this issue Oct 3, 2021 · 2 comments · Fixed by #12691
Closed

Interior mutable const lints trigger on HeaderValue constants #7752

sfackler opened this issue Oct 3, 2021 · 2 comments · Fixed by #12691
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

@sfackler
Copy link
Member

sfackler commented Oct 3, 2021

Lint name:
declare_interior_mutable_const
borrow_interior_mutable_const

I tried this code:

use http::{HeaderMap, HeaderValue};

const APPLICATION_JSON: HeaderValue = HeaderValue::from_static("application/json");

fn main() {
    let headers = HeaderMap::new();

    if headers.get("Content-Type") == Some(&APPLICATION_JSON) {
        println!("json!");
    }
}

I expected to see this happen:

The program should compile without warnings.

Instead, this happened:

warning: a `const` item should never be interior mutable
 --> src/main.rs:3:1
  |
3 | const APPLICATION_JSON: HeaderValue = HeaderValue::from_static("application/json");
  | -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | |
  | make this a static item (maybe with lazy_static)
  |
  = note: `#[warn(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

warning: a `const` item with interior mutability should not be borrowed
 --> src/main.rs:8:45
  |
8 |     if headers.get("Content-Type") == Some(&APPLICATION_JSON) {
  |                                             ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(clippy::borrow_interior_mutable_const)]` on by default
  = help: assign this const to a local or static variable, and use the variable here
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const

Meta

Rust version (rustc -Vv):

rustc 1.55.0 (c8dfcfe04 2021-09-06)
binary: rustc
commit-hash: c8dfcfe046a7680554bf4eb612bad840e7631c4b
commit-date: 2021-09-06
host: x86_64-unknown-linux-gnu
release: 1.55.0
LLVM version: 12.0.1
@sfackler sfackler 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 Oct 3, 2021
@Arnavion
Copy link
Contributor

Duplicate of #5812 (HeaderValue is a wrapper around a Bytes)

@rail-rain
Copy link
Contributor

The lints now (#11678) don't think of Bytes as interior mutable. It's probably a bug in the implementation that types that wrap Bytes still trigger the lints.

# for free to join this conversation on GitHub. Already have an account? # 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

Successfully merging a pull request may close this issue.

3 participants