7

According to this answer, #[allow(dead_code)] should work, but it doesn't

fn main() {
    #[allow(dead_code)]
    let x = 0;
}
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
  • The best way would be to remove the unused code. Maybe you'd be better off explaining why that approach is one you're avoiding, someone might be able to give you a better solution. – loganfsmyth Jun 22 '21 at 01:18
  • For machine disabling you can look to my answer at below link. https://stackoverflow.com/a/71119013/10943567 – Kargat TTT Feb 14 '22 at 22:30

3 Answers3

16

These are different lints. dead_code refers to unused code at the item level, e.g. imports, functions and types. unused_variables refers to variables that are never accessed.

You can also cover both cases with #[allow(unused)].

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
  • 3
    Also, prefixing the variable name with an underscore suppresses the warning: `let _x = 0;` – Jmb Jun 22 '21 at 07:03
9

The correct is

fn main() {
    #[allow(unused_variables)]
    let x = 0;
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
8

Put #![allow(unused)] at the top of the file (note the !).

phocks
  • 2,933
  • 5
  • 27
  • 28