How to suppress warnings for unused constants

#[allow(unused)]
impl Profile {
    const CIRCLE: u8 = 0x00;
    const SQUARE: u8 = 0x01;
   ...
}

We all know this case, the huge list of constants for compatibility with something else, with some constants unused but retained for completeness.

#[allow(unused)] suppresses messages for unused constants, but allowing all "unused" is too big a hammer for general use. "Unused" is supposedly all of unused_imports, unused_variables, unused_assignments, dead_code, unused_mut, unreachable_code, unused_must_use, unused_unsafe, path_statements, unused_attributes. Don't want to suppress all that for the entire impl section.

"Unused_variable" doesn't suppress warnings for constants. There doesn't seem to be an #[allow(unused_constants)].

There must be a proper way to do this. It's such a common case.

1 Like

As a workaround, can you create an entire separate impl block for just the constants and nothing else? (Any given type can have multiple disjoint impl blocks.)

2 Likes

That's the workaround I'm using now, putting the constants in a separate impl block. But there must be a clean way to do this. It's such a common case.

It's part of #[warn(dead_code)], if that's precise enough for you.

You could add it to each associated const instead of the entire impl block.

That helps a little. I tried unused_variable, but that doesn't apply to constants.

Would unused_constant be a useful feature to request? It's a common use case. An unused variable is usually dead code, but an unused constant has legitimate uses.

Seems reasonable to request to me .

Another option is to make them pub, if the struct is also pub. All items accessible from outside of the crate are "used".

And one more option is to change the name of each unused const to start with an underscore. (This means an extra step if you later end up using it, though.)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.