Skip to content

Interior mutable const lints trigger on HeaderValue constants #7752

Closed
@sfackler

Description

@sfackler

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

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