Programming Search⌘K

Rust lints, page 2 of 5

The lint rules of Rust, with what each one finds. This page has 844 cards.

Checks if we’re passing a c_void raw pointer to {Box,Rc,Arc,Weak}::from_raw(_) from_raw_with_void_ptr

When dealing with c_void raw pointers in FFI, it is easy to run into the pitfall of calling from_raw with the c_void pointer. The type signature of Box::from_raw is fn from_raw(raw: *mut T) -> Box<T>, so if you pass a *mut c_void you will get a Box<c_void> (and similarly for Rc, Arc and Weak). For this to be safe, c_void would need to have the same memory layout as the original type, which is often not the case. Clippy puts this lint in the suspicious group, at the level warn.

Code
from_raw_with_void_ptr
Severity
warn
Since
1.67.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

This lint requires Future implementations returned from functions and methods to implement the Send marker trait, ignoring type parameters. future_not_send

A Future implementation captures some state that it needs to eventually produce its final value. When targeting a multithreaded executor (which is the norm on non-embedded devices) this means that this state may need to be transported to other threads, in other words the whole Future needs to implement the Send marker trait. If it does not, then the resulting Future cannot be submitted to a thread pool in the end user’s code. Clippy puts this lint in the nursery group, at the level allow.

Code
future_not_send
Severity
allow
Since
1.44.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for if-else that could be written using either bool::then or bool::then_some. if_then_some_else_none

Looks a little redundant. Using bool::then is more concise and incurs no loss of clarity. For simple calculations and known values, use bool::then_some, which is eagerly evaluated in comparison to bool::then. Clippy puts this lint in the restriction group, at the level allow.

Code
if_then_some_else_none
Severity
allow
Since
1.53.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

This lint is concerned with the semantics of Borrow and Hash for a type that implements all three of Hash, Borrow<str> and Borrow<[u8]> as it is impossible to satisfy the semantics of Borrow and Hash for both Borrow<str> and Borrow<[u8]>. impl_hash_borrow_with_str_and_bytes

When providing implementations for Borrow<T>, one should consider whether the different implementations should act as facets or representations of the underlying type. Generic code typically uses Borrow<T> when it relies on the identical behavior of these additional trait implementations. These traits will likely appear as additional trait bounds. Clippy puts this lint in the correctness group, at the level deny.

Code
impl_hash_borrow_with_str_and_bytes
Severity
deny
Since
1.76.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for the usage of _.to_owned(), vec.to_vec(), or similar when calling _.clone() would be clearer. implicit_clone

These methods do the same thing as _.clone() but may be confusing as to why we are calling to_vec on something that is already a Vec or calling to_owned on something that is already owned. Clippy puts this lint in the pedantic group, at the level allow.

Code
implicit_clone
Severity
allow
Since
1.52.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Looks for bounds in impl Trait in return position that are implied by other bounds. implied_bounds_in_impls

This can happen when a trait is specified that another trait already has as a supertrait (e.g. fn() -> impl Deref + DerefMut<Target = i32> has an unnecessary Deref bound, because Deref is a supertrait of DerefMut) Specifying more bounds than necessary adds needless complexity for the reader. Clippy puts this lint in the complexity group, at the level warn.

Code
implied_bounds_in_impls
Severity
warn
Since
1.74.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

The lint checks for slice bindings in patterns that are only used to access individual slice values. index_refutable_slice

Accessing slice values using indices can lead to panics. Using refutable patterns can avoid these. Binding to individual values also improves the readability as they can be named. Clippy puts this lint in the pedantic group, at the level allow.

Code
index_refutable_slice
Severity
allow
Since
1.59.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for usage of .to_string() on an &&T where T implements ToString directly (like &&str or &&String). inefficient_to_string

In versions of the compiler before Rust 1.82.0, this bypasses the specialized implementation of ToString and instead goes through the more expensive string formatting facilities. Clippy puts this lint in the pedantic group, at the level allow.

Code
inefficient_to_string
Severity
allow
Since
1.40.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for the definition of inherent methods with a signature of to_string(&self) -> String and if the type implementing this method also implements the Display trait. inherent_to_string_shadow_display

This method is also implicitly defined if a type implements the Display trait. The less versatile inherent method will then shadow the implementation introduced by Display. Clippy puts this lint in the correctness group, at the level deny.

Code
inherent_to_string_shadow_display
Severity
deny
Since
1.38.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Enforce that where bounds are used for all trait bounds. inline_trait_bounds

Enforce a single style throughout a codebase. Avoid uncertainty about whether a bound should be inline or out-of-line (i.e. a where bound). Avoid complex inline bounds, which could make a function declaration more difficult to read. Clippy puts this lint in the restriction group, at the level allow.

Code
inline_trait_bounds
Severity
allow
Since
1.97.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for the usage of division (/) and remainder (%) operations when performed on any integer types using the default Div and Rem trait implementations. integer_division_remainder_used

In cryptographic contexts, division can result in timing sidechannel vulnerabilities, and needs to be replaced with constant-time code instead (e.g. Barrett reduction). Clippy puts this lint in the restriction group, at the level allow.

Code
integer_division_remainder_used
Severity
allow
Since
1.79.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

This is the opposite of the iter_without_into_iter lint. into_iter_without_iter

It looks for IntoIterator for (&|&mut) Type implementations without an inherent iter or iter_mut method on the type or on any of the types in its Deref chain. It’s not bad, but having them is idiomatic and allows the type to be used in iterator chains by just calling .iter(), instead of the more awkward <&Type>::into_iter or (&val).into_iter() syntax in case of ambiguity with another IntoIterator impl. Clippy puts this lint in the pedantic group, at the level allow.

Code
into_iter_without_iter
Severity
allow
Since
1.75.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for comparisons where the relation is always either true or false, but where one side has been upcast so that the comparison is necessary. invalid_upcast_comparisons

Only integer types are checked. An expression like let x : u8 = ...; (x as u32) > 300 will mistakenly imply that it is possible for x to be outside the range of u8. Clippy puts this lint in the pedantic group, at the level allow.

Code
invalid_upcast_comparisons
Severity
allow

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for comparisons between integers, followed by subtracting the greater value from the lower one. inverted_saturating_sub

This could result in an underflow and is most likely not what the user wants. If this was intended to be a saturated subtraction, consider using the saturating_sub method directly. Clippy puts this lint in the correctness group, at the level deny.

Code
inverted_saturating_sub
Severity
deny
Since
1.83.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for IP addresses that could be replaced with predefined constants such as Ipv4Addr::new(127, 0, 0, 1) instead of using the appropriate constants. ip_constant

Using specific IP addresses like 127.0.0.1 or ::1 is less clear and less maintainable than using the predefined constants Ipv4Addr::LOCALHOST or Ipv6Addr::LOCALHOST. These constants improve code readability, make the intent explicit, and are less error-prone. Clippy puts this lint in the pedantic group, at the level allow.

Code
ip_constant
Severity
allow
Since
1.89.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for usage of .filter(Result::is_ok) that may be replaced with a .flatten() call. iter_filter_is_ok

This lint will require additional changes to the follow-up calls as it affects the type. This pattern is often followed by manual unwrapping of Result. The simplification results in more readable and succinct code without the need for manual unwrapping. Clippy puts this lint in the pedantic group, at the level allow.

Code
iter_filter_is_ok
Severity
allow
Since
1.77.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for usage of .filter(Option::is_some) that may be replaced with a .flatten() call. iter_filter_is_some

This lint will require additional changes to the follow-up calls as it affects the type. This pattern is often followed by manual unwrapping of the Option. The simplification results in more readable and succinct code without the need for manual unwrapping. Clippy puts this lint in the pedantic group, at the level allow.

Code
iter_filter_is_some
Severity
allow
Since
1.77.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for loops on x.next(). iter_next_loop

next() returns either Some(value) if there was a value, or None otherwise. The insidious thing is that Option<_> implements IntoIterator, so that possibly one value will be iterated, leading to some hard to find bugs. No one will want to write such code except to win an Underhanded Rust Contest. Clippy puts this lint in the correctness group, at the level deny.

Code
iter_next_loop
Severity
deny

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Looks for iterator combinator calls such as .take(x) or .skip(x) where x is greater than the amount of items that an iterator will produce. iter_out_of_bounds

Taking or skipping more items than there are in an iterator either creates an iterator with all items from the original iterator or an iterator with no items at all. This is most likely not what the user intended to do. Clippy puts this lint in the suspicious group, at the level warn.

Code
iter_out_of_bounds
Severity
warn
Since
1.74.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

This is a restriction lint which prevents the use of hash types (i.e., HashSet and HashMap) in for loops. iter_over_hash_type

Because hash types are unordered, when iterated through such as in a for loop, the values are returned in an undefined order. As a result, on redundant systems this may cause inconsistencies and anomalies. In addition, the unknown order of the elements may reduce readability or introduce other undesired side effects. Clippy puts this lint in the restriction group, at the level allow.

Code
iter_over_hash_type
Severity
allow
Since
1.76.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Looks for iter and iter_mut methods without an associated IntoIterator for (&|&mut) Type implementation. iter_without_into_iter

It’s not bad, but having them is idiomatic and allows the type to be used in for loops directly (for val in &iter {}), without having to first call iter() or iter_mut(). Clippy puts this lint in the pedantic group, at the level allow.

Code
iter_without_into_iter
Severity
allow
Since
1.75.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for the inclusion of large files via include_bytes!() or include_str!(). large_include_file

Including large files can undesirably increase the size of the binary produced by the compiler. This lint may be used to catch mistakes where an unexpectedly large file is included, or temporarily to obtain a list of all large files. Clippy puts this lint in the restriction group, at the level allow.

Code
large_include_file
Severity
allow
Since
1.62.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for functions that use a lot of stack space. large_stack_frames

The stack region of memory is very limited in size (usually much smaller than the heap) and attempting to use too much will result in a stack overflow and crash the program. To avoid this, you should consider allocating large types on the heap instead (e.g. by boxing them). Clippy puts this lint in the nursery group, at the level allow.

Code
large_stack_frames
Severity
allow
Since
1.72.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for functions taking arguments by value, where the argument type is Copy and large enough to be worth considering passing by reference. large_types_passed_by_value

Does not trigger if the function is being exported, because that might induce API breakage, if the parameter is declared as mutable, or if the argument is a self. Arguments passed by value might result in an unnecessary shallow copy, taking up more space in the stack and requiring a call to memcpy, which can be expensive. Clippy puts this lint in the pedantic group, at the level allow.

Code
large_types_passed_by_value
Severity
allow
Since
1.49.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for items that implement .len() but not .is_empty(). len_without_is_empty

It is good custom to have both methods, because for some data structures, asking about the length will be a costly operation, whereas .is_empty() can usually answer in constant time. Also it used to lead to false positives on the len_zero lint – currently that lint will ignore such entities. Clippy puts this lint in the style group, at the level warn.

Code
len_without_is_empty
Severity
warn

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for getting the length of something via .len() just to compare to zero, and suggests using .is_empty() where applicable. len_zero

Some structures can answer .is_empty() much faster than calculating their length. So it is good to get into the habit of using .is_empty(), and having it is cheap. Besides, it makes the intent clearer than a manual comparison in some contexts. Clippy puts this lint in the style group, at the level warn.

Code
len_zero
Severity
warn

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for let _ = sync_lock. let_underscore_lock

This supports mutex and rwlock in parking_lot. For std locks see the rustc lint let_underscore_lock This statement immediately drops the lock instead of extending its lifetime to the end of the scope, which is often not intended. To extend lock lifetime to the end of the scope, use an underscore-prefixed name instead (i.e. _lock). If you want to explicitly drop the lock, std::mem::drop conveys your intention better and is less error-prone. Clippy puts this lint in the correctness group, at the level deny.

Code
let_underscore_lock
Severity
deny
Since
1.43.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for let _ = <expr> without a type annotation, and suggests to either provide one, or remove the let keyword altogether. let_underscore_untyped

The let _ = <expr> expression ignores the value of <expr>, but will continue to do so even if the type were to change, thus potentially introducing subtle bugs. By supplying a type annotation, one will be forced to re-visit the decision to ignore the value in such cases. Clippy puts this lint in the restriction group, at the level allow.

Code
let_underscore_untyped
Severity
allow
Since
1.69.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for usage of lines.filter_map(Result::ok) or lines.flat_map(Result::ok) when lines has type std::io::Lines. lines_filter_map_ok

Lines instances might produce a never-ending stream of Err, in which case filter_map(Result::ok) will enter an infinite loop while waiting for an Ok variant. Calling next() once is sufficient to enter the infinite loop, even in the absence of explicit loops in the user code. Clippy puts this lint in the suspicious group, at the level warn.

Code
lines_filter_map_ok
Severity
warn
Since
1.70.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for the manual creation of C strings (a string with a NUL byte at the end), either through one of the CStr constructor functions, or more plainly by calling .as_ptr() on a (byte) string literal with a hardcoded \0 byte at the end. manual_c_str_literals

This can be written more concisely using c"str" literals and is also less error-prone, because the compiler checks for interior NUL bytes and the terminating NUL byte is inserted automatically. Clippy puts this lint in the complexity group, at the level warn.

Code
manual_c_str_literals
Severity
warn
Since
1.78.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Suggests to use dedicated built-in methods, is_ascii_(lowercase|uppercase|digit|hexdigit) for checking on corresponding ascii range manual_is_ascii_check

Using the built-in functions is more readable and makes it clear that it’s not a specific subset of characters, but all ASCII (lowercase|uppercase|digit|hexdigit) characters. Clippy puts this lint in the style group, at the level warn.

Code
manual_is_ascii_check
Severity
warn
Since
1.67.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for manual implementation of midpoint. manual_midpoint

Using (x + y) / 2 or (x + y) >> 1 on integer types might cause an overflow on the intermediate addition result. The latter will only warn for unsigned integer types. Similar for floating-point (x + y) / 2.0 and (x + y) * 0.5. Clippy puts this lint in the pedantic group, at the level allow.

Code
manual_midpoint
Severity
allow
Since
1.87.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for repeat().take() that can be replaced with repeat_n(). manual_repeat_n

Using repeat_n() is more concise and clearer. Also, repeat_n() is sometimes faster than repeat().take() when the type of the element is non-trivial to clone because the original value can be reused for the last .next() call rather than always cloning. Clippy puts this lint in the style group, at the level warn.

Code
manual_repeat_n
Severity
warn
Since
1.86.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Suggests using strip_{prefix,suffix} over str::{starts,ends}_with and slicing using the pattern’s length. manual_strip

Using str:strip_{prefix,suffix} is safer and may have better performance as there is no slicing which may panic and the compiler does not need to insert this panic code. It is also sometimes more readable as it removes the need for duplicating or storing the pattern used by str::{starts,ends}_with and in the slicing. Clippy puts this lint in the complexity group, at the level warn.

Code
manual_strip
Severity
warn
Since
1.48.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for usage of Iterator::fold with a type that implements Try. manual_try_fold

The code should use try_fold instead, which short-circuits on failure, thus opening the door for additional optimizations not possible with fold as rustc can guarantee the function is never called on None, Err, etc., alleviating otherwise necessary checks. It’s also slightly more idiomatic. Clippy puts this lint in the perf group, at the level warn.

Code
manual_try_fold
Severity
warn
Since
1.72.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.

Checks for Iterator::map over ranges without using the parameter which could be more clearly expressed using std::iter::repeat(...).take(...) or std::iter::repeat_n. map_with_unused_argument_over_ranges

It expresses the intent more clearly to take the correct number of times from a generating function than to apply a closure to each number in a range only to discard them. Clippy puts this lint in the restriction group, at the level allow.

Code
map_with_unused_argument_over_ranges
Severity
allow
Since
1.84.0

Source: rust-lang.github.io. JSON record. This entry is a card. Help write this page.