"warning: variant `UploadTerrain` is never constructed" false alarm

"Warning: variant UploadTerrain is never constructed" as a false alarm.

Here's the enum declaration:

--> src/server/auth.rs:

pub enum AuthorizeType {
    /// Upload terrain. Can add and update terrain data.
    UploadTerrain,
    /// Upload impostors. Can add and upload impostor data.
    UploadImpostors,
}

Here's the use, in another file that brings in "auth" with "mod".

mod auth;
use auth::{Authorizer, AuthorizeType};
...

`self.owner_name = Some(Authorizer::authorize(AuthorizeType::UploadTerrain, env, params)?);`

The error message is:

warning: variant `UploadImpostors` is never constructed
  --> src/server/auth.rs:37:5
   |
33 | pub enum AuthorizeType {
   |          ------------- variant in this enum
...
37 |     UploadImpostors,
   |     ^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

As a check, I commented out that enum case, and then I get an error:

error[E0599]: no variant or associated item named `UploadImpostors` found for enum `AuthorizeType` in the current scope
   --> src/server/uploadimpostor.rs:470:77
    |
470 | ...horizeType::UploadImpostors, env, params)?);
    |                ^^^^^^^^^^^^^^^ variant or associated item not found in `AuthorizeType`
    |
   ::: src/server/auth.rs:33:1
    |
 33 | pub enum AuthorizeType {
    | ---------------------- variant or associated item `UploadImpostors` not found for this enum

So that enum case really was needed.

The enum is pub, so the known special case around non-pub enums does not apply.
The unusual thing here is that the file that uses the enum brings in the file that defines it with "mod" directly. There's no "lib.rs" or "mod.rs" file involved. That ought to work.

Obscure case, or lint bug?

Code is in GitHub - John-Nagle/maptools: Tools for manipulating Second Life / Open Simulator map data branch "newregionorder", if anybody cares

rustc 1.93.0 (254b59607 2026-01-19) stable.

  1. As quoted by yourself, the compiler states that the variant UploadImpostors is never constructed and does not complain about UploadTerrain.
  2. Even if the variant is used in other parts of the code, if this code is considered dead, the compiler will detect this transitively and still consider it unused.
  3. That the enum is pub is irrelevant as long as it is not exported from the library, i.e. is publicly accessible by third-party crates.
3 Likes

Each binary is compiled separately so when compiling uploadterrain, the UploadImposters variant isn't constructed. Your errors are coming from a different place in each case. You'll have to just silence the lint using #[allow(...)]. (Or switch to the more traditional model of exporting the type publicly through a lib crate)

This “works” only in that nothing stops you from doing it. When you do this, the file is being compiled multiple times (once for each crate that includes it) and the multiple rustc sessions involved in this process do not coordinate with each other in any way at all, so they have no knowledge that the variant might be used in a different compilation, and do not take it into account when issuing warnings.

Well-organized Rust projects generally do not share a file between crates like this. Sometimes it is the lesser evil (e.g. it is sometimes used to share a small amount of code between multiple test targets) but it means you are at risk of these spurious dead code warnings, and that the code is being redundantly compiled, increasing build time (though, of course, that is an insignificant cost when the code is so short).

4 Likes

Oh, right, the error messages from multiple independent compiles of three [bin] targets are interleaved.

Moved "auth" to the "common" library, which is better style and makes the compiler happy.