Warn-by-default lints

These lints are all set to the 'warn' level by default.

const-err

This lint detects an erroneous expression while doing constant evaluation. Someexample code that triggers this lint:

  1. let b = 200u8 + 200u8;

This will produce:

  1. warning: attempt to add with overflow
  2. --> src/main.rs:2:9
  3. |
  4. 2 | let b = 200u8 + 200u8;
  5. | ^^^^^^^^^^^^^
  6. |

dead-code

This lint detects unused, unexported items. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. fn foo() {}
  4. }

This will produce:

  1. warning: function is never used: `foo`
  2. --> src/lib.rs:2:1
  3. |
  4. 2 | fn foo() {}
  5. | ^^^^^^^^
  6. |

deprecated

This lint detects use of deprecated items. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. #[deprecated]
  4. fn foo() {}
  5. fn bar() {
  6.     foo();
  7. }
  8. }

This will produce:

  1. warning: use of deprecated item 'foo'
  2. --> src/lib.rs:7:5
  3. |
  4. 7 | foo();
  5. | ^^^
  6. |

illegal-floating-point-literal-pattern

This lint detects floating-point literals used in patterns. Some example codethat triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. let x = 42.0;
  4. match x {
  5.     5.0 => {},
  6.     _ => {},
  7. }
  8. }

This will produce:

  1. warning: floating-point literals cannot be used in patterns
  2. --> src/main.rs:4:9
  3. |
  4. 4 | 5.0 => {},
  5. | ^^^
  6. |
  7. = note: `#[warn(illegal_floating_point_literal_pattern)]` on by default
  8. = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  9. = note: for more information, see issue #41620 <https://github.com/rust-lang/rust/issues/41620>

improper-ctypes

This lint detects proper use of libc types in foreign modules. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. extern "C" {
  4.     static STATIC: String;
  5. }
  6. }

This will produce:

  1. warning: found struct without foreign-function-safe representation annotation in foreign module, consider adding a `#[repr(C)]` attribute to the type
  2. --> src/main.rs:2:20
  3. |
  4. 2 | static STATIC: String;
  5. | ^^^^^^
  6. |

late-bound-lifetime-arguments

This lint detects generic lifetime arguments in path segments withlate bound lifetime parameters. Some example code that triggers this lint:

  1. struct S;
  2. impl S {
  3.     fn late<'a, 'b>(self, _: &'a u8, _: &'b u8) {}
  4. }
  5. fn main() {
  6.     S.late::<'static>(&0, &0);
  7. }

This will produce:

  1. warning: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
  2. --> src/main.rs:8:14
  3. |
  4. 4 | fn late<'a, 'b>(self, _: &'a u8, _: &'b u8) {}
  5. | -- the late bound lifetime parameter is introduced here
  6. ...
  7. 8 | S.late::<'static>(&0, &0);
  8. | ^^^^^^^
  9. |
  10. = note: `#[warn(late_bound_lifetime_arguments)]` on by default
  11. = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  12. = note: for more information, see issue #42868 <https://github.com/rust-lang/rust/issues/42868>

non-camel-case-types

This lint detects types, variants, traits and type parameters that don't havecamel case names. Some example code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. struct s;
  4. }

This will produce:

  1. warning: type `s` should have a camel case name such as `S`
  2. --> src/main.rs:1:1
  3. |
  4. 1 | struct s;
  5. | ^^^^^^^^^
  6. |

non-shorthand-field-patterns

This lint detects using Struct { x: x } instead of Struct { x } in a pattern. Someexample code that triggers this lint:

  1. struct Point {
  2.     x: i32,
  3.     y: i32,
  4. }
  5. fn main() {
  6.     let p = Point {
  7.         x: 5,
  8.         y: 5,
  9.     };
  10.     match p {
  11.         Point { x: x, y: y } => (),
  12.     }
  13. }

This will produce:

  1. warning: the `x:` in this pattern is redundant
  2. --> src/main.rs:14:17
  3. |
  4. 14 | Point { x: x, y: y } => (),
  5. | --^^
  6. | |
  7. | help: remove this
  8. |
  9. warning: the `y:` in this pattern is redundant
  10. --> src/main.rs:14:23
  11. |
  12. 14 | Point { x: x, y: y } => (),
  13. | --^^
  14. | |
  15. | help: remove this

non-snake-case

This lint detects variables, methods, functions, lifetime parameters andmodules that don't have snake case names. Some example code that triggersthis lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. let X = 5;
  4. }

This will produce:

  1. warning: variable `X` should have a snake case name such as `x`
  2. --> src/main.rs:2:9
  3. |
  4. 2 | let X = 5;
  5. | ^
  6. |

non-upper-case-globals

This lint detects static constants that don't have uppercase identifiers.Some example code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. static x: i32 = 5;
  4. }

This will produce:

  1. warning: static variable `x` should have an upper case name such as `X`
  2. --> src/main.rs:1:1
  3. |
  4. 1 | static x: i32 = 5;
  5. | ^^^^^^^^^^^^^^^^^^
  6. |

no-mangle-generic-items

This lint detects generic items must be mangled. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. #[no_mangle]
  4. fn foo<T>(t: T) {
  5. }
  6. }

This will produce:

  1. warning: functions generic over types must be mangled
  2. --> src/main.rs:2:1
  3. |
  4. 1 | #[no_mangle]
  5. | ------------ help: remove this attribute
  6. 2 | / fn foo<T>(t: T) {
  7. 3 | |
  8. 4 | | }
  9. | |_^
  10. |

path-statements

This lint detects path statements with no effect. Some example code thattriggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. let x = 42;
  4. x;
  5. }

This will produce:

  1. warning: path statement with no effect
  2. --> src/main.rs:3:5
  3. |
  4. 3 | x;
  5. | ^^
  6. |

private-in-public

This lint detects private items in public interfaces not caught by the old implementation. Someexample code that triggers this lint:

  1. pub trait Trait {
  2. type A;
  3. }
  4. pub struct S;
  5. mod foo {
  6. struct Z;
  7. impl ::Trait for ::S {
  8. type A = Z;
  9. }
  10. }
  11. fn main() {}

This will produce:

  1. error[E0446]: private type `foo::Z` in public interface
  2. --> src/main.rs:11:9
  3. |
  4. 11 | type A = Z;
  5. | ^^^^^^^^^^^ can't leak private type

private-no-mangle-fns

This lint detects functions marked #[no_mangle] that are also private.Given that private functions aren't exposed publicly, and #[no_mangle]controls the public symbol, this combination is erroneous. Some example codethat triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. #[no_mangle]
  4. fn foo() {}
  5. }

This will produce:

  1. warning: function is marked `#[no_mangle]`, but not exported
  2. --> src/main.rs:2:1
  3. |
  4. 2 | fn foo() {}
  5. | -^^^^^^^^^^
  6. | |
  7. | help: try making it public: `pub`
  8. |

To fix this, either make it public or remove the #[no_mangle].

private-no-mangle-statics

This lint detects any statics marked #[no_mangle] that are private.Given that private statics aren't exposed publicly, and #[no_mangle]controls the public symbol, this combination is erroneous. Some example codethat triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. #[no_mangle]
  4. static X: i32 = 4;
  5. }

This will produce:

  1. warning: static is marked `#[no_mangle]`, but not exported
  2. --> src/main.rs:2:1
  3. |
  4. 2 | static X: i32 = 4;
  5. | -^^^^^^^^^^^^^^^^^
  6. | |
  7. | help: try making it public: `pub`
  8. |

To fix this, either make it public or remove the #[no_mangle].

renamed-and-removed-lints

This lint detects lints that have been renamed or removed. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. #![deny(raw_pointer_derive)]
  3. fn main() {
  4. }

This will produce:

  1. warning: lint raw_pointer_derive has been removed: using derive with raw pointers is ok
  2. --> src/main.rs:1:9
  3. |
  4. 1 | #![deny(raw_pointer_derive)]
  5. | ^^^^^^^^^^^^^^^^^^
  6. |

To fix this, either remove the lint or use the new name.

safe-packed-borrows

This lint detects borrowing a field in the interior of a packed structurewith alignment other than 1. Some example code that triggers this lint:

  1. #[repr(packed)]
  2. pub struct Unaligned<T>(pub T);
  3. pub struct Foo {
  4.     start: u8,
  5.     data: Unaligned<u32>,
  6. }
  7. fn main() {
  8.     let x = Foo { start: 0, data: Unaligned(1) };
  9.     let y = &x.data.0;
  10. }

This will produce:

  1. warning: borrow of packed field requires unsafe function or block (error E0133)
  2. --> src/main.rs:11:13
  3. |
  4. 11 | let y = &x.data.0;
  5. | ^^^^^^^^^
  6. |
  7. = note: `#[warn(safe_packed_borrows)]` on by default
  8. = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  9. = note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>

stable-features

This lint detects a #[feature] attribute that's since been made stable. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. #![feature(test_accepted_feature)]
  3. fn main() {
  4. }

This will produce:

  1. warning: this feature has been stable since 1.0.0. Attribute no longer needed
  2. --> src/main.rs:1:12
  3. |
  4. 1 | #![feature(test_accepted_feature)]
  5. | ^^^^^^^^^^^^^^^^^^^^^
  6. |

To fix, simply remove the #![feature] attribute, as it's no longer needed.

type-alias-bounds

This lint detects bounds in type aliases. These are not currently enforced.Some example code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. #[allow(dead_code)]
  4. type SendVec<T: Send> = Vec<T>;
  5. }

This will produce:

  1. warning: bounds on generic parameters are not enforced in type aliases
  2. --> src/lib.rs:2:17
  3. |
  4. 2 | type SendVec<T: Send> = Vec<T>;
  5. | ^^^^
  6. |
  7. = note: `#[warn(type_alias_bounds)]` on by default
  8. = help: the bound will not be checked when the type alias is used, and should be removed

tyvar-behind-raw-pointer

This lint detects raw pointer to an inference variable. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. let data = std::ptr::null();
  4. let _ = &data as *const *const ();
  5. if data.is_null() {}
  6. }

This will produce:

  1. warning: type annotations needed
  2. --> src/main.rs:4:13
  3. |
  4. 4 | if data.is_null() {}
  5. | ^^^^^^^
  6. |
  7. = note: `#[warn(tyvar_behind_raw_pointer)]` on by default
  8. = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
  9. = note: for more information, see issue #46906 <https://github.com/rust-lang/rust/issues/46906>

unconditional-recursion

This lint detects functions that cannot return without calling themselves.Some example code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. fn foo() {
  4.     foo();
  5. }
  6. }

This will produce:

  1. warning: function cannot return without recursing
  2. --> src/main.rs:1:1
  3. |
  4. 1 | fn foo() {
  5. | ^^^^^^^^ cannot return without recursing
  6. 2 | foo();
  7. | ----- recursive call site
  8. |

unknown-lints

This lint detects unrecognized lint attribute. Someexample code that triggers this lint:

  1. #[allow(not_a_real_lint)]

This will produce:

  1. warning: unknown lint: `not_a_real_lint`
  2. --> src/main.rs:1:10
  3. |
  4. 1 | #![allow(not_a_real_lint)]
  5. | ^^^^^^^^^^^^^^^
  6. |

unreachable-code

This lint detects unreachable code paths. Some example code thattriggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. panic!("we never go past here!");
  4. let x = 5;
  5. }

This will produce:

  1. warning: unreachable statement
  2. --> src/main.rs:4:5
  3. |
  4. 4 | let x = 5;
  5. | ^^^^^^^^^^
  6. |

unreachable-patterns

This lint detects unreachable patterns. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. let x = 5;
  4. match x {
  5.     y => (),
  6.     5 => (),
  7. }
  8. }

This will produce:

  1. warning: unreachable pattern
  2. --> src/main.rs:5:5
  3. |
  4. 5 | 5 => (),
  5. | ^
  6. |

The y pattern will always match, so the five is impossible to reach.Remember, match arms match in order, you probably wanted to put the 5 caseabove the y case.

unstable-name-collision

This lint detects that you've used a name that the standard library plans toadd in the future, which means that your code may fail to compile withoutadditional type annotations in the future. Either rename, or add thoseannotations now.

unused-allocation

This lint detects unnecessary allocations that can be eliminated.

unused-assignments

This lint detects assignments that will never be read. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. let mut x = 5;
  4. x = 6;
  5. }

This will produce:

  1. warning: value assigned to `x` is never read
  2. --> src/main.rs:4:5
  3. |
  4. 4 | x = 6;
  5. | ^
  6. |

unused-attributes

This lint detects attributes that were not used by the compiler. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. #![macro_export]
  3. fn main() {
  4. }

This will produce:

  1. warning: unused attribute
  2. --> src/main.rs:1:1
  3. |
  4. 1 | #![macro_export]
  5. | ^^^^^^^^^^^^^^^^
  6. |

unused-comparisons

This lint detects comparisons made useless by limits of the types involved. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. fn foo(x: u8) {
  4.     x >= 0;
  5. }
  6. }

This will produce:

  1. warning: comparison is useless due to type limits
  2. --> src/main.rs:6:5
  3. |
  4. 6 | x >= 0;
  5. | ^^^^^^
  6. |

unused-doc-comment

This lint detects doc comments that aren't used by rustdoc. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. /// docs for x
  4. let x = 12;
  5. }

This will produce:

  1. warning: doc comment not used by rustdoc
  2. --> src/main.rs:2:5
  3. |
  4. 2 | /// docs for x
  5. | ^^^^^^^^^^^^^^
  6. |

unused-features

This lint detects unused or unknown features found in crate-level #[feature] directives.To fix this, simply remove the feature flag.

unused-imports

This lint detects imports that are never used. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. use std::collections::HashMap;
  4. }

This will produce:

  1. warning: unused import: `std::collections::HashMap`
  2. --> src/main.rs:1:5
  3. |
  4. 1 | use std::collections::HashMap;
  5. | ^^^^^^^^^^^^^^^^^^^^^^^^^
  6. |

unused-macros

This lint detects macros that were not used. Some example code thattriggers this lint:

  1. macro_rules! unused {
  2.     () => {};
  3. }
  4. fn main() {
  5. }

This will produce:

  1. warning: unused macro definition
  2. --> src/main.rs:1:1
  3. |
  4. 1 | / macro_rules! unused {
  5. 2 | | () => {};
  6. 3 | | }
  7. | |_^
  8. |

unused-must-use

This lint detects unused result of a type flagged as #[must_use]. Someexample code that triggers this lint:

  1. fn returns_result() -> Result<(), ()> {
  2.     Ok(())
  3. }
  4. fn main() {
  5.     returns_result();
  6. }

This will produce:

  1. warning: unused `std::result::Result` that must be used
  2. --> src/main.rs:6:5
  3. |
  4. 6 | returns_result();
  5. | ^^^^^^^^^^^^^^^^^
  6. |

unused-mut

This lint detects mut variables which don't need to be mutable. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. let mut x = 5;
  4. }

This will produce:

  1. warning: variable does not need to be mutable
  2. --> src/main.rs:2:9
  3. |
  4. 2 | let mut x = 5;
  5. | ----^
  6. | |
  7. | help: remove this `mut`
  8. |

unused-parens

This lint detects if, match, while and return with parentheses; theydo not need them. Some example code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. if(true) {}
  4. }

This will produce:

  1. warning: unnecessary parentheses around `if` condition
  2. --> src/main.rs:2:7
  3. |
  4. 2 | if(true) {}
  5. | ^^^^^^ help: remove these parentheses
  6. |

unused-unsafe

This lint detects unnecessary use of an unsafe block. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. unsafe {}
  4. }

This will produce:

  1. warning: unnecessary `unsafe` block
  2. --> src/main.rs:2:5
  3. |
  4. 2 | unsafe {}
  5. | ^^^^^^ unnecessary `unsafe` block
  6. |

unused-variables

This lint detects variables which are not used in any way. Someexample code that triggers this lint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. let x = 5;
  4. }

This will produce:

  1. warning: unused variable: `x`
  2. --> src/main.rs:2:9
  3. |
  4. 2 | let x = 5;
  5. | ^ help: consider using `_x` instead
  6. |

warnings

This lint is a bit special; by changing its level, you change every other warningthat would produce a warning to whatever value you'd like:

  1. #![allow(unused_variables)]
  2. #![deny(warnings)]
  3. fn main() {
  4. }

As such, you won't ever trigger this lint in your code directly.

while-true

This lint detects while true { }. Some example code that triggers thislint:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. while true {
  4. }
  5. }

This will produce:

  1. warning: denote infinite loops with `loop { ... }`
  2. --> src/main.rs:2:5
  3. |
  4. 2 | while true {
  5. | ^^^^^^^^^^ help: use `loop`
  6. |