- Allowed-by-default lints
- anonymous-parameters
- bare-trait-object
- box-pointers
- elided-lifetime-in-path
- missing-copy-implementations
- missing-debug-implementations
- missing-docs
- single-use-lifetimes
- trivial-casts
- trivial-numeric-casts
- unreachable-pub
- unsafe-code
- unstable-features
- unused-extern-crates
- unused-import-braces
- unused-qualifications
- unused-results
- variant-size-differences
Allowed-by-default lints
These lints are all set to the 'allow' level by default. As such, they won't show upunless you set them to a higher lint level with a flag or attribute.
anonymous-parameters
This lint detects anonymous parameters. Some example code that triggers this lint:
#![allow(unused_variables)]
fn main() {
trait Foo {
fn foo(usize);
}
}
When set to 'deny', this will produce:
error: use of deprecated anonymous parameter
--> src/lib.rs:5:11
|
5 | fn foo(usize);
| ^
|
= 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 #41686 <https://github.com/rust-lang/rust/issues/41686>
This syntax is mostly a historical accident, and can be worked around quiteeasily:
#![allow(unused_variables)]
fn main() {
trait Foo {
fn foo(_: usize);
}
}
bare-trait-object
This lint suggests using dyn Trait
for trait objects. Some example codethat triggers this lint:
#![allow(unused_variables)]
#![feature(dyn_trait)]
fn main() {
trait Trait { }
fn takes_trait_object(_: Box<Trait>) {
}
}
When set to 'deny', this will produce:
error: trait objects without an explicit `dyn` are deprecated
--> src/lib.rs:7:30
|
7 | fn takes_trait_object(_: Box<Trait>) {
| ^^^^^ help: use `dyn`: `dyn Trait`
|
To fix it, do as the help message suggests:
#![allow(unused_variables)]
#![feature(dyn_trait)]
#![deny(bare_trait_objects)]
fn main() {
trait Trait { }
fn takes_trait_object(_: Box<dyn Trait>) {
}
}
box-pointers
This lints use of the Box type. Some example code that triggers this lint:
#![allow(unused_variables)]
fn main() {
struct Foo {
x: Box<isize>,
}
}
When set to 'deny', this will produce:
error: type uses owned (Box type) pointers: std::boxed::Box<isize>
--> src/lib.rs:6:5
|
6 | x: Box<isize> //~ ERROR type uses owned
| ^^^^^^^^^^^^^
|
This lint is mostly historical, and not particularly useful. Box<T>
used tobe built into the language, and the only way to do heap allocation. Today'sRust can call into other allocators, etc.
elided-lifetime-in-path
This lint detects the use of hidden lifetime parameters. Some example codethat triggers this lint:
#![allow(unused_variables)]
fn main() {
struct Foo<'a> {
x: &'a u32
}
fn foo(x: &Foo) {
}
}
When set to 'deny', this will produce:
error: hidden lifetime parameters are deprecated, try `Foo<'_>`
--> src/lib.rs:5:12
|
5 | fn foo(x: &Foo) {
| ^^^
|
Lifetime elision elides this lifetime, but that is being deprecated.
missing-copy-implementations
This lint detects potentially-forgotten implementations of Copy
. Someexample code that triggers this lint:
#![allow(unused_variables)]
fn main() {
pub struct Foo {
pub field: i32
}
}
When set to 'deny', this will produce:
error: type could implement `Copy`; consider adding `impl Copy`
--> src/main.rs:3:1
|
3 | / pub struct Foo { //~ ERROR type could implement `Copy`; consider adding `impl Copy`
4 | | pub field: i32
5 | | }
| |_^
|
You can fix the lint by deriving Copy
.
This lint is set to 'allow' because this code isn't bad; it's common to writenewtypes like this specifically so that a Copy
type is no longer Copy
.
missing-debug-implementations
This lint detects missing implementations of fmt::Debug
. Some example codethat triggers this lint:
#![allow(unused_variables)]
fn main() {
pub struct Foo;
}
When set to 'deny', this will produce:
error: type does not implement `fmt::Debug`; consider adding `#[derive(Debug)]` or a manual implementation
--> src/main.rs:3:1
|
3 | pub struct Foo;
| ^^^^^^^^^^^^^^^
|
You can fix the lint by deriving Debug
.
missing-docs
This lint detects missing documentation for public items. Some example codethat triggers this lint:
#![allow(unused_variables)]
fn main() {
pub fn foo() {}
}
When set to 'deny', this will produce:
error: missing documentation for crate
--> src/main.rs:1:1
|
1 | / #![deny(missing_docs)]
2 | |
3 | | pub fn foo() {}
4 | |
5 | | fn main() {}
| |____________^
|
error: missing documentation for a function
--> src/main.rs:3:1
|
3 | pub fn foo() {}
| ^^^^^^^^^^^^
To fix the lint, add documentation to all items.
single-use-lifetimes
This lint detects lifetimes that are only used once. Some example code thattriggers this lint:
#![allow(unused_variables)]
fn main() {
struct Foo<'x> {
x: &'x u32
}
}
When set to 'deny', this will produce:
error: lifetime name `'x` only used once
--> src/main.rs:3:12
|
3 | struct Foo<'x> {
| ^^
|
trivial-casts
This lint detects trivial casts which could be removed. Some example codethat triggers this lint:
#![allow(unused_variables)]
fn main() {
let x: &u32 = &42;
let _ = x as *const u32;
}
When set to 'deny', this will produce:
error: trivial cast: `&u32` as `*const u32`. Cast can be replaced by coercion, this might require type ascription or a temporary variable
--> src/main.rs:5:13
|
5 | let _ = x as *const u32;
| ^^^^^^^^^^^^^^^
|
note: lint level defined here
--> src/main.rs:1:9
|
1 | #![deny(trivial_casts)]
| ^^^^^^^^^^^^^
trivial-numeric-casts
This lint detects trivial casts of numeric types which could be removed. Someexample code that triggers this lint:
#![allow(unused_variables)]
fn main() {
let x = 42i32 as i32;
}
When set to 'deny', this will produce:
error: trivial numeric cast: `i32` as `i32`. Cast can be replaced by coercion, this might require type ascription or a temporary variable
--> src/main.rs:4:13
|
4 | let x = 42i32 as i32;
| ^^^^^^^^^^^^
|
unreachable-pub
This lint triggers for pub
items not reachable from the crate root. Someexample code that triggers this lint:
#![allow(unused_variables)]
fn main() {
mod foo {
pub mod bar {
}
}
}
When set to 'deny', this will produce:
error: unreachable `pub` item
--> src/main.rs:4:5
|
4 | pub mod bar {
| ---^^^^^^^^
| |
| help: consider restricting its visibility: `pub(crate)`
|
unsafe-code
This lint catches usage of unsafe
code. Some example code that triggers this lint:
fn main() {
unsafe {
}
}
When set to 'deny', this will produce:
error: usage of an `unsafe` block
--> src/main.rs:4:5
|
4 | / unsafe {
5 | |
6 | | }
| |_____^
|
unstable-features
This lint is deprecated and no longer used.
unused-extern-crates
This lint guards against extern crate
items that are never used. Someexample code that triggers this lint:
extern crate semver;
When set to 'deny', this will produce:
error: unused extern crate
--> src/main.rs:3:1
|
3 | extern crate semver;
| ^^^^^^^^^^^^^^^^^^^^
|
unused-import-braces
This lint catches unnecessary braces around an imported item. Some examplecode that triggers this lint:
use test::{A};
pub mod test {
pub struct A;
}
fn main() {}
When set to 'deny', this will produce:
error: braces around A is unnecessary
--> src/main.rs:3:1
|
3 | use test::{A};
| ^^^^^^^^^^^^^^
|
To fix it, use test::A;
unused-qualifications
This lint detects unnecessarily qualified names. Some example code that triggers this lint:
mod foo {
pub fn bar() {}
}
fn main() {
use foo::bar;
foo::bar();
}
When set to 'deny', this will produce:
error: unnecessary qualification
--> src/main.rs:9:5
|
9 | foo::bar();
| ^^^^^^^^
|
You can call bar()
directly, without the foo::
.
unused-results
This lint checks for the unused result of an expression in a statement. Someexample code that triggers this lint:
fn foo<T>() -> T { panic!() }
fn main() {
foo::<usize>();
}
When set to 'deny', this will produce:
error: unused result
--> src/main.rs:6:5
|
6 | foo::<usize>();
| ^^^^^^^^^^^^^^^
|
variant-size-differences
This lint detects enums with widely varying variant sizes. Some example code that triggers this lint:
#![allow(unused_variables)]
fn main() {
enum En {
V0(u8),
VBig([u8; 1024]),
}
}
When set to 'deny', this will produce:
error: enum variant is more than three times larger (1024 bytes) than the next largest
--> src/main.rs:5:5
|
5 | VBig([u8; 1024]), //~ ERROR variant is more than three times larger
| ^^^^^^^^^^^^^^^^
|