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

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

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.

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 but I'm wondering if there was any progress or a tracking issue for it.

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

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

2 Likes

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.

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

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)].

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

#[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.

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