# Create new lint for unused constants, #\[allow(unused\_constants)\]

**URL:** https://internals.rust-lang.org/t/create-new-lint-for-unused-constants-allow-unused-constants/21990
**Category:** Uncategorized
**Created:** [December 8, 2024, 9:59pm UTC](https://internals.rust-lang.org/t/create-new-lint-for-unused-constants-allow-unused-constants/21990 "2024-12-08T21:59:37Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Takashiidobe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/takashiidobe/32/12781_2.png) [@Takashiidobe](https://internals.rust-lang.org/u/Takashiidobe)
#### Post date: [December 8, 2024, 9:59pm UTC](https://internals.rust-lang.org/t/create-new-lint-for-unused-constants-allow-unused-constants/21990/1 "2024-12-08T21:59:37Z")

</div>

Say I create some constants that I may or may not use because I'm prototyping code.

```rust
const EXAMPLE_INPUT: &str = include_str!("example.txt");
const INPUT: &str = include_str!("input.txt");

```

However, because of #[warn(dead\_code)], there is a warning if I compile my code if I don't use it.

```rust
warning: constant `EXAMPLE_INPUT` is never used
 --> src/main.rs:3:7
  |
3 | const EXAMPLE_INPUT: &str = include_str!("example.txt");
  | ^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

```

I can use #[allow(dead\_code)], but I might want all the other lints to be on (say I dont want any unused functions, but I'm fine with unused consts). I see lots of `unused_` like parens, assignments, variables, etc, but not one for consts.

I'd like for a way for the compiler to only ignore unused constants, instead of having to allow any form of dead code with #[allow(dead\_code)]. Is there a way to do that currently/if not, would people be interested in an explicit attribute for it for clarity (I don't know which one does outside of #[allow(dead\_code)]).

This has already been requested here: [How to suppress warnings for unused constants - help - The Rust Programming Language Forum](https://users.rust-lang.org/t/how-to-suppress-warnings-for-unused-constants/54010) but I'm wondering if there was any progress or a tracking issue for it.

---

<div class="post-metadata">

### Author: ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)
#### Post date: [December 9, 2024, 12:43am UTC](https://internals.rust-lang.org/t/create-new-lint-for-unused-constants-allow-unused-constants/21990/2 "2024-12-09T00:43:38Z")

</div>

Can you elaborate why unused constants should be allowed but not, say, unused functions? I don't follow why `const`s are different here.

FWIW, I like `#![cfg_attr(test, allow(unused))]`, so long as you `cargo run` occasionally and not only `cargo test`.

---

<div class="post-metadata">

### Author: ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)
#### Post date: [December 9, 2024, 1:19am UTC](https://internals.rust-lang.org/t/create-new-lint-for-unused-constants-allow-unused-constants/21990/3 "2024-12-09T01:19:00Z")

</div>

> [@scottmcm](#):
>
> Can you elaborate why unused constants should be allowed but not, say, unused functions? I don't follow why `const`s are different here.

The author cites the striation in `unused` warnings, although none of these seem to be related to dead code (that one's just `dead-code`, and doesn't care about what kind of code). But I don't understand why one would compile with warnings-as-errors _while prototyping_ in the first place, it is not usually a ton of noise to sort through.

```rust
unused-doc-comment
unused-tuple-struct-fields
unused_allocation
unused_assignments
unused_associated_type_bounds
unused_attributes
unused_braces
unused_comparisons
unused_doc_comments
unused_features
unused_imports
unused_labels
unused_macros
unused_must_use
unused_mut
unused_parens
unused_unsafe
unused_variables

```

---

<div class="post-metadata">

### Author: ![Takashiidobe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/takashiidobe/32/12781_2.png) [@Takashiidobe](https://internals.rust-lang.org/u/Takashiidobe)
#### Post date: [December 9, 2024, 6:32pm UTC](https://internals.rust-lang.org/t/create-new-lint-for-unused-constants-allow-unused-constants/21990/4 "2024-12-09T18:32:39Z")

</div>

I'm not compiling this code with warnings as errors -- I'm using neovim so warnings will be shown inline with the code.

My point is that there are lots of `unused_` lints which can be allowed so they don't warn. However there doesn't seem to be an easy at hand one for consts, which I believe would be nice to have, since I can turn that on and off just like I could parens, variables, imports, etc.

I also think there should be a lint for allowing unused functions, fwiw. I do think it would be helpful for people writing tutorials or creating boilerplate to allow the code to not have warnings when compiling without having to use the sledgehammer of #[allow(dead\_code)].

---

<div class="post-metadata">

### Author: ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)
#### Post date: [December 9, 2024, 7:04pm UTC](https://internals.rust-lang.org/t/create-new-lint-for-unused-constants-allow-unused-constants/21990/5 "2024-12-09T19:04:49Z")

</div>

> [@Takashiidobe](#):
>
> My point is that there are lots of `unused_` lints

To be clear, most of the `unused_` lints are for things like `if (a == b) {} // unused_parentheses`. They are not for dead code, the `dead_code` lint currently doesn't have these divisions. So possibly you are proposing to add a number of new divisions. Improving the prototyping experience in rust is a good motivation, though in my experience this is something I would rarely use.

In the meantime, if you only have a couple unused consts you might be fine with just

```rust
#[allow(dead_code)] // only affects `I`
const I: i32 = 2;

```

Even if you have a ton of such consts, modal editing makes it not too difficult to edit many lines.

---

<div class="post-metadata">

### Author: ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)
#### Post date: [June 2, 2026, 7:05pm UTC](https://internals.rust-lang.org/t/create-new-lint-for-unused-constants-allow-unused-constants/21990/6 "2026-06-02T19:05:38Z")

</div>

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
