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
您可以
allow在结构,模块,函数等上添加属性:
#[allow(dead_code)]
struct SemanticDirection;
Run Code Online (Sandbox Code Playgroud)添加crate-level allow属性 ; 注意!:
#![allow(dead_code)]
Run Code Online (Sandbox Code Playgroud)传递给rustc:
rustc -A dead_code main.rs
Run Code Online (Sandbox Code Playgroud)cargo通过RUSTFLAGS环境变量传递它:
RUSTFLAGS="$RUSTFLAGS -A dead_code" cargo build
Run Code Online (Sandbox Code Playgroud)M. *_*put 75
将这两行放在文件的顶部:
#![allow(dead_code)]
#![allow(unused_variables)]
Run Code Online (Sandbox Code Playgroud)
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)
Muh*_*ssa 12
另外,Rust 提供了五个级别的 lint(允许、警告、强制警告、拒绝、禁止)。
https://doc.rust-lang.org/rustc/lints/levels.html#lint-levels
直接将以下内容放入文件头的方法
#![allow(dead_code, unused_variables)]
dead_code检测未使用、未导出的项目。unused_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.
对于未使用的函数,您应该将其公开,但要小心。如果该结构不是公开的,那么您仍然会收到如下错误:
//this should be public also
struct A{
A{}
}
impl A {
pub fn new() -> A {
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您不想公开它,您应该将#[allow(unused)]
| 归档时间: |
|
| 查看次数: |
60942 次 |
| 最近记录: |