如何在Rust中禁用未使用的代码警告?

And*_*ner 179 warnings dead-code compiler-warnings rust

struct SemanticDirection;

fn main() {}
Run Code Online (Sandbox Code Playgroud)
warning: struct is never used: `SemanticDirection`
 --> src/main.rs:1:1
  |
1 | struct SemanticDirection;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default
Run Code Online (Sandbox Code Playgroud)

我会将这些警告重新发送给任何严肃的事情,但我只是在修补这种语言而这正在驱使我蝙蝠.

我尝试添加#[allow(dead_code)]到我的代码,但这不起作用.

Arj*_*jan 277

您可以

  1. allow在结构,模块,函数等上添加属性:

    #[allow(dead_code)]
    struct SemanticDirection;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 添加crate-level allow属性 ; 注意!:

    #![allow(dead_code)]
    
    Run Code Online (Sandbox Code Playgroud)
  3. 传递给rustc:

    rustc -A dead_code main.rs
    
    Run Code Online (Sandbox Code Playgroud)
  4. cargo通过RUSTFLAGS环境变量传递它:

    RUSTFLAGS="$RUSTFLAGS -A dead_code" cargo build
    
    Run Code Online (Sandbox Code Playgroud)

  • 请注意,最后一个将触发所有内容的重新编译。 (3认同)
  • 在我看来,最后一个效果最好。另外添加“-Aused_variables”有助于防止将“_”放在所有内容前面。 (2认同)
  • `#![allow(dead_code)]` 也必须在任何代码之前,否则 rust 会产生一些神秘的错误。 (2认同)

M. *_*put 75

将这两行放在文件的顶部:

#![allow(dead_code)]
#![allow(unused_variables)]
Run Code Online (Sandbox Code Playgroud)

  • 替换为 `#![allow(dead_code,未使用)]` ;-) (20认同)
  • @jwkoo !使其适用于整个板条箱 (6认同)
  • @jaques-sam `dead_code` 和 `unused_variables` 是 `unused` 的子集(有更多的 lints),这可能太板了,无法让 linter 静默特定的东西。此答案详细介绍了 lint 之间的差异:/sf/answers/4518980791/ (2认同)

ant*_*oyo 52

禁用此警告的另一种方法是为标识符添加前缀_:

struct _UnusedStruct {
    _unused_field: i32,
}

fn main() {
    let _unused_variable = 10;
}
Run Code Online (Sandbox Code Playgroud)

例如,对于SDL窗口,这可能很有用:

let _window = video_subsystem.window("Rust SDL2 demo", 800, 600);
Run Code Online (Sandbox Code Playgroud)

使用下划线进行前缀不同于使用单独的下划线作为名称.执行以下操作将立即破坏窗口,这不太可能是预期的行为.

let _ = video_subsystem.window("Rust SDL2 demo", 800, 600);
Run Code Online (Sandbox Code Playgroud)

  • 使用 `let _ =` 值将在语句末尾而不是块末尾被删除 (4认同)
  • “分配给下划线会破坏它”的行为似乎很奇怪(尽管我不怀疑你是对的)。你有参考吗? (3认同)
  • @MichaelAnderson请参阅“ RAII。您可能希望存在一个变量,以实现其析构函数的副作用,但不要使用它。在此用例中,不能简单地使用_,因为_不是变量绑定且值将在声明的末尾删除。” 来自/sf/answers/3385321061/ (3认同)

Muh*_*ssa 12

另外,Rust 提供了五个级别的 lint(允许、警告、强制警告、拒绝、禁止)。

https://doc.rust-lang.org/rustc/lints/levels.html#lint-levels


zan*_*ngw 9

  • 直接将以下内容放入文件头的方法

    #![allow(dead_code, unused_variables)]

    • lintdead_code检测未使用、未导出的项目。
    • lintunused_variables检测未以任何方式使用的变量。
  • 更简单的方法是将以下内容放在文件的头部

    #![allow(unused)]

参考:Rust lint 列表


小智 6

You can add the #[allow(dead_code)] attribute to the struct definition like so:

#[allow(dead_code)]
struct SemanticDirection;
Run Code Online (Sandbox Code Playgroud)

Or you can disable the warning for the entire file by adding the attribute at the top of the file, like so:

#![allow(dead_code)]

struct SemanticDirection;
Run Code Online (Sandbox Code Playgroud)

But these attributes only work if you have the dead_code lint enabled. By default, the dead_code lint is enabled in Rust, but you can disable it by adding the following to the top of your code:

#![deny(dead_code)]
Run Code Online (Sandbox Code Playgroud)

This will disable the dead_code lint for the entire file.

It's generally a good idea to keep the dead_code lint enabled, as it can help you catch mistakes in your code and ensure that you are not introducing unnecessary code into your project. However, it can be annoying when you are just experimenting and trying out different things, so it's understandable if you want to disable it in those cases.


Vit*_*uel 5

公开代码也停止了警告;您还需要将附件mod的公开。

当您编写库时,这很有意义:您的代码在内部是“未使用”的,因为它打算由客户端代码使用。


Luc*_*lla 5

对于未使用的函数,您应该将其公开,但要小心。如果该结构不是公开的,那么您仍然会收到如下错误:

//this should be public also
struct A{
   A{}
}

impl A {
    pub fn new() -> A {

    }
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您不想公开它,您应该将#[allow(unused)]


归档时间:

查看次数:

60942 次

最近记录:

6 年,5 月 前