Lints
在软件中,“lint”是一种用于帮助改进源代码的工具。Rust 编译器包含许多 lint,当它编译你的代码时,也会运行这些 lint。这些 lint 可能会产生警告、错误,或者什么都不产生,这取决于你的配置。
这是一个小例子
$ cat main.rs
fn main() {
let x = 5;
}
$ rustc main.rs
warning: unused variable: `x`
--> main.rs:2:9
|
2 | let x = 5;
| ^
|
= note: `#[warn(unused_variables)]` on by default
= note: to avoid this warning, consider using `_x` instead
这是 unused_variables
lint,它告诉你你在代码中引入了一个未使用的变量。这并非错误,所以它不是一个错误,但它可能是一个 bug,因此你会收到一个警告。
未来不兼容的 lint
有时需要更改编译器以修复可能导致现有代码停止编译的问题。“未来不兼容”的 lint 在这些情况下发出,以便让 Rust 用户平稳过渡到新的行为。最初,编译器将继续接受有问题的代码并发出警告。警告包含问题的描述,未来将变成错误的通知,以及指向跟踪问题的链接,该链接提供详细信息和反馈机会。这给用户一些时间来修复代码以适应更改。一段时间后,警告可能会变成错误。
以下是一个未来不兼容的 lint 的示例
warning: borrow of packed field is unsafe and requires unsafe function or block (error E0133)
--> lint_example.rs:11:13
|
11 | let y = &x.data.0;
| ^^^^^^^^^
|
= note: `#[warn(safe_packed_borrows)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
有关未来不兼容更改的过程和策略的更多信息,请参阅RFC 1589。