Warning: associated function `new` is never used


After "cargo build", my code : desktopd/desktop.rs at master · mothsART/desktopd · GitHub goes up with some warnings :

warning: associated function `populate_i18n` is never used
  --> src/desktop.rs:29:8
29 |     fn populate_i18n(
   |        ^^^^^^^^^^^^^
   = note: `#[warn(dead_code)]` on by default

warning: associated function `new` is never used
  --> src/desktop.rs:45:12
45 |     pub fn new(path: &Path) -> DesktopFile {
   |            ^^^

warning: `desktopd` (bin "desktopd") generated 2 warnings
    Finished dev [unoptimized + debuginfo] target(s) in 2.57s

My rustc version : rustc 1.65.0 (897e37553 2022-11-02)

Can you explain what's going on ? (the 2 functions are used)

Best regards

You are declaring (mod desktop;) your modules twice (once in lib.rs and once in main.rs). This results in them being compiled twice, creating two separate definitions of the modules and everything in them, and if one of those definitions is not used then you get unused code warnings.

To avoid this and other problems, never declare a module more than once[1]. Instead, for your situation, declare them once in lib.rs, and import them from main.rs. Replace mod desktop; with

use desktopd::desktop;

(because desktopd is the name of your package and therefore the name of its library crate.)

  1. There are situations where this is necessary, such as code that must be shared between multiple integration tests. But those are much rarer. ↩︎


Brilliantly clear @kpreid. Thanks !