Programming Search⌘K

Rust lints, page 4 of 5

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

Checks for items with #[repr(packed)]-attribute without ABI qualification repr_packed_without_abi

Without qualification, repr(packed) implies repr(Rust). The Rust-ABI is inherently unstable. While this is fine as long as the type is accessed correctly within Rust-code, most uses of #[repr(packed)] involve FFI and/or data structures specified by network-protocols or other external specifications. In such situations, the unstable Rust-ABI implied in #[repr(packed)] may lead to future bugs should the Rust-ABI change. Clippy puts this lint in the suspicious group, at the level warn.

Code
repr_packed_without_abi
Severity
warn
Since
1.85.0

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

Checks for functions that return Result with an unusually large Err-variant. result_large_err

A Result is at least as large as the Err-variant. While we expect that variant to be seldom used, the compiler needs to reserve and move that much memory every single time. Furthermore, errors are often simply passed up the call-stack, making use of the ?-operator and its type-conversion mechanics. If the Err-variant further up the call-stack stores the Err-variant in question (as library code often does), it itself needs to be at least as large, propagating the problem. Clippy puts this lint in the perf group, at the level warn.

Code
result_large_err
Severity
warn
Since
1.65.0

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

Checks for range expressions x..y where both x and y are constant and x is greater to y. reversed_empty_ranges

Also triggers if x is equal to y when they are conditions to a for loop. Empty ranges yield no values so iterating them is a no-op. Moreover, trying to use a reversed range to index a slice will panic at run-time. Clippy puts this lint in the correctness group, at the level deny.

Code
reversed_empty_ranges
Severity
deny
Since
1.45.0

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

Checks for usages of Vec::from_raw_parts and String::from_raw_parts where the same expression is used for the length and the capacity. same_length_and_capacity

If the same expression is being passed for the length and capacity, it is most likely a semantic error. In the case of a Vec, for example, the only way to end up with one that has the same length and capacity is by going through a boxed slice, e.g. Box::from(some_vec), which shrinks the capacity to match the length. Clippy puts this lint in the pedantic group, at the level allow.

Code
same_length_and_capacity
Severity
allow
Since
1.94.0

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

Suggests moving the semicolon after a block to the inside of the block, after its last expression. semicolon_inside_block

For consistency it’s best to have the semicolon inside/outside the block. Either way is fine and this lint suggests inside the block. Take a look at semicolon_outside_block for the other alternative. Clippy puts this lint in the restriction group, at the level allow.

Code
semicolon_inside_block
Severity
allow
Since
1.68.0

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

Suggests moving the semicolon from a block’s final expression outside of the block. semicolon_outside_block

For consistency it’s best to have the semicolon inside/outside the block. Either way is fine and this lint suggests outside the block. Take a look at semicolon_inside_block for the other alternative. Clippy puts this lint in the restriction group, at the level allow.

Code
semicolon_outside_block
Severity
allow
Since
1.68.0

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

Checks for bindings that shadow other bindings already in scope, either without an initialization or with one that does not even use the original value. shadow_unrelated

Shadowing a binding with a closely related one is part of idiomatic Rust, but shadowing a binding by accident with an unrelated one may indicate a mistake. Clippy puts this lint in the restriction group, at the level allow.

Code
shadow_unrelated
Severity
allow

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

Checks for methods that should live in a trait implementation of a std trait (see llogiq’s blog post for further information) instead of an inherent implementation. should_implement_trait

Implementing the traits improve ergonomics for users of the code, often with very little cost. Also people seeing a mul(...) method may expect * to work equally, so you should have good reason to disappoint them. Clippy puts this lint in the style group, at the level warn.

Code
should_implement_trait
Severity
warn

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

Checks for temporaries returned from function calls in a match scrutinee that have the clippy::has_significant_drop attribute. significant_drop_in_scrutinee

The clippy::has_significant_drop attribute can be added to types whose Drop impls have an important side-effect, such as unlocking a mutex, making it important for users to be able to accurately understand their lifetimes. When a temporary is returned in a function call in a match scrutinee, its lifetime lasts until the end of the match block, which may be surprising. Clippy puts this lint in the nursery group, at the level allow.

Code
significant_drop_in_scrutinee
Severity
allow
Since
1.60.0

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

Searches for elements marked with #[clippy::has_significant_drop] that could be early dropped but are in fact dropped at the end of their scopes. significant_drop_tightening

In other words, enforces the “tightening” of their possible lifetimes. Elements marked with #[clippy::has_significant_drop] are generally synchronizing primitives that manage shared resources, as such, it is desired to release them as soon as possible to avoid unnecessary resource contention. Clippy puts this lint in the nursery group, at the level allow.

Code
significant_drop_tightening
Severity
allow
Since
1.69.0

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

Checks for Vec or array initializations that contain only one range. single_range_in_vec_init

This is almost always incorrect, as it will result in a Vec that has only one element. Almost always, the programmer intended for it to include all elements in the range or for the end of the range to be the length instead. Clippy puts this lint in the suspicious group, at the level warn.

Code
single_range_in_vec_init
Severity
warn
Since
1.72.0

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

When sorting primitive values (integers, bools, chars, as well as arrays, slices, and tuples of such items), it is typically better to use an unstable sort than a stable sort. stable_sort_primitive

Typically, using a stable sort consumes more memory and cpu cycles. Because values which compare equal are identical, preserving their relative order (the guarantee that a stable sort provides) means nothing, while the extra costs still apply. Clippy puts this lint in the pedantic group, at the level allow.

Code
stable_sort_primitive
Severity
allow
Since
1.47.0

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

Finds items imported through std when available through alloc. std_instead_of_alloc

Crates which have no_std compatibility and require alloc may wish to ensure types are imported from alloc to ensure disabling std does not cause the crate to fail to compile. This lint is also useful for crates migrating to become no_std compatible. Clippy puts this lint in the restriction group, at the level allow.

Code
std_instead_of_alloc
Severity
allow
Since
1.64.0

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

Finds items imported through std when available through core. std_instead_of_core

Crates which have no_std compatibility may wish to ensure types are imported from core to ensure disabling std does not cause the crate to fail to compile. This lint is also useful for crates migrating to become no_std compatible. Clippy puts this lint in the restriction group, at the level allow.

Code
std_instead_of_core
Severity
allow
Since
1.64.0

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

Checks for all instances of x + _ where x is of type String, but only if string_add_assign does not match. string_add

This particular Add implementation is asymmetric (the other operand need not be String, but x does), while addition as mathematically defined is symmetric, and the String::push_str(_) function is a perfectly good replacement. Therefore, some dislike it and wish not to have it in their code. Clippy puts this lint in the restriction group, at the level allow.

Code
string_add
Severity
allow

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

Checks for slice operations on strings string_slice

UTF-8 characters span multiple bytes, and it is easy to inadvertently confuse character counts and string indices. This may lead to panics, and should warrant some test cases containing wide UTF-8 characters. This lint is most useful in code that should avoid panics at all costs. Clippy puts this lint in the restriction group, at the level allow.

Code
string_slice
Severity
allow
Since
1.58.0

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

Checks for Command::arg() invocations that look like they should be multiple arguments instead, such as arg("-t ext2"). suspicious_command_arg_space

Command::arg() does not split arguments by space. An argument like arg("-t ext2") will be passed as a single argument to the command, which is likely not what was intended. Clippy puts this lint in the suspicious group, at the level warn.

Code
suspicious_command_arg_space
Severity
warn
Since
1.69.0

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

Checks for formatting of else. suspicious_else_formatting

It lints if the else is followed immediately by a newline or the else seems to be missing. This is probably some refactoring remnant, even if the code is correct, it might look confusing. Clippy puts this lint in the suspicious group, at the level warn.

Code
suspicious_else_formatting
Severity
warn

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

Checks for calls to map followed by a count. suspicious_map

It looks suspicious. Maybe map was confused with filter. If the map call is intentional, this should be rewritten using inspect. Or, if you intend to drive the iterator to completion, you can just use for_each instead. Clippy puts this lint in the suspicious group, at the level warn.

Code
suspicious_map
Severity
warn
Since
1.39.0

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

Checks for the suspicious use of OpenOptions::create() without an explicit OpenOptions::truncate(). suspicious_open_options

create() alone will either create a new file or open an existing file. If the file already exists, it will be overwritten when written to, but the file will not be truncated by default. If less data is written to the file than it already contains, the remainder of the file will remain unchanged, and the end of the file will contain old data. In most cases, one should either use create_new to ensure the file is created from scratch, or ensure truncate is called so that the truncation behaviour is explicit. truncate(true) will ensure the file is entirely overwritten with new data, whereas truncate(false) will explicitly keep the default behavior. Clippy puts this lint in the suspicious group, at the level warn.

Code
suspicious_open_options
Severity
warn
Since
1.77.0

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

Checks for unlikely usages of binary operators that are almost certainly typos and/or copy/paste errors, given the other usages of binary operators nearby. suspicious_operation_groupings

They are probably bugs and if they aren’t then they look like bugs and you should add a comment explaining why you are doing such an odd set of operations. Clippy puts this lint in the nursery group, at the level allow.

Code
suspicious_operation_groupings
Severity
allow
Since
1.50.0

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

Checks for the usage of _.to_owned(), on a Cow<'_, _>. suspicious_to_owned

Calling to_owned() on a Cow creates a clone of the Cow itself, without taking ownership of the Cow contents (i.e. it’s equivalent to calling Cow::clone). The similarly named into_owned method, on the other hand, clones the Cow contents, effectively turning any Cow::Borrowed into a Cow::Owned. Clippy puts this lint in the suspicious group, at the level warn.

Code
suspicious_to_owned
Severity
warn
Since
1.65.0

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

Checks the formatting of a unary operator on the right hand side of a binary operator. suspicious_unary_op_formatting

It lints if there is no space between the binary and unary operators, but there is a space between the unary and its operand. This is either a typo in the binary operator or confusing. Clippy puts this lint in the suspicious group, at the level warn.

Code
suspicious_unary_op_formatting
Severity
warn
Since
1.40.0

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

Checks doc comments for usage of tab characters. tabs_in_doc_comments

The rust style-guide promotes spaces instead of tabs for indentation. To keep a consistent view on the source, also doc comments should not have tabs. Also, explaining ascii-diagrams containing tabs can get displayed incorrectly when the display settings of the author and reader differ. Clippy puts this lint in the style group, at the level warn.

Code
tabs_in_doc_comments
Severity
warn
Since
1.41.0

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

Checks for #[test] in doctests unless they are marked with either ignore, no_run or compile_fail. test_attr_in_doctest

Code in examples marked as #[test] will somewhat surprisingly not be run by cargo test. If you really want to show how to test stuff in an example, mark it no_run to make the intent clear. Clippy puts this lint in the suspicious group, at the level warn.

Code
test_attr_in_doctest
Severity
warn
Since
1.76.0

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

Triggers when a testing function (marked with the #[test] attribute) isn’t inside a testing module (marked with #[cfg(test)]). tests_outside_test_module

The idiomatic (and more performant) way of writing tests is inside a testing module (flagged with #[cfg(test)]), having test functions outside of this module is confusing and may lead to them being “hidden”. Clippy puts this lint in the restriction group, at the level allow.

Code
tests_outside_test_module
Severity
allow
Since
1.70.0

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

Checks for direct implementations of ToString. to_string_trait_impl

This trait is automatically implemented for any type which implements the Display trait. As such, ToString shouldn’t be implemented directly: Display should be implemented instead, and you get the ToString implementation for free. Clippy puts this lint in the style group, at the level warn.

Code
to_string_trait_impl
Severity
warn
Since
1.78.0

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

Checks if the first paragraph in the documentation of items listed in the module page is too long. too_long_first_doc_paragraph

Documentation will show the first paragraph of the docstring in the summary page of a module. Having a nice, short summary in the first paragraph is part of writing good docs. Clippy puts this lint in the nursery group, at the level allow.

Code
too_long_first_doc_paragraph
Severity
allow
Since
1.82.0

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

Checks for function arguments and let bindings denoted as ref. toplevel_ref_arg

The ref declaration makes the function take an owned value, but turns the argument into a reference (which means that the value is destroyed when exiting the function). This adds not much value: either take a reference type, or take an owned value and create references in the body. Clippy puts this lint in the style group, at the level warn.

Code
toplevel_ref_arg
Severity
warn

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

Displays a warning when a struct with a trailing zero-sized array is declared without a repr attribute. trailing_empty_array

Zero-sized arrays aren’t very useful in Rust itself, so such a struct is likely being created to pass to C code or in some other situation where control over memory layout matters (for example, in conjunction with manual allocation to make it easy to compute the offset of the array). Either way, #[repr(C)] (or another repr attribute) is needed. Clippy puts this lint in the nursery group, at the level allow.

Code
trailing_empty_array
Severity
allow
Since
1.58.0

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

Checks for functions taking arguments by reference, where the argument type is Copy and small enough to be more efficient to always pass by value. trivially_copy_pass_by_ref

In many calling conventions instances of structs will be passed through registers if they fit into two or less general purpose registers. Clippy puts this lint in the pedantic group, at the level allow.

Code
trivially_copy_pass_by_ref
Severity
allow

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

Checks for usage of Err(x)?. try_err

The ? operator is designed to allow calls that can fail to be easily chained. For example, foo()?.bar() or foo(bar()?). Because Err(x)? can’t be used that way (it will always return), it is more clear to write return Err(x). Clippy puts this lint in the restriction group, at the level allow.

Code
try_err
Severity
allow
Since
1.38.0

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

Looks for calls to .type_id() on a Box<dyn _>. type_id_on_box

This almost certainly does not do what the user expects and can lead to subtle bugs. Calling .type_id() on a Box<dyn Trait> returns a fixed TypeId of the Box itself, rather than returning the TypeId of the underlying type behind the trait object. Clippy puts this lint in the suspicious group, at the level warn.

Code
type_id_on_box
Severity
warn
Since
1.73.0

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

Checks for unsafe blocks and impls without a // SAFETY: comment explaining why the unsafe operations performed inside the block are safe. undocumented_unsafe_blocks

Undocumented unsafe blocks and impls can make it difficult to read and maintain code. Writing out the safety justification may help in discovering unsoundness or bugs. Clippy puts this lint in the restriction group, at the level allow.

Code
undocumented_unsafe_blocks
Severity
allow
Since
1.58.0

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

It detects references to uninhabited types, such as ! and warns when those are either dereferenced or returned from a function. uninhabited_references

Dereferencing a reference to an uninhabited type would create an instance of such a type, which cannot exist. This constitutes undefined behaviour. Such a reference could have been created by unsafe code. Clippy puts this lint in the nursery group, at the level allow.

Code
uninhabited_references
Severity
allow
Since
1.76.0

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

Checks for set_len() call that creates Vec with uninitialized elements. uninit_vec

This is commonly caused by calling set_len() right after allocating or reserving a buffer with new(), default(), with_capacity(), or reserve(). It creates a Vec with uninitialized data, which leads to undefined behavior with most safe operations. Notably, uninitialized Vec<u8> must not be used with generic Read. Clippy puts this lint in the correctness group, at the level deny.

Code
uninit_vec
Severity
deny
Since
1.58.0

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

Checks for comparisons to unit. unit_cmp

This includes all binary comparisons (like == and <) and asserts. Unit is always equal to itself, and thus is just a clumsily written constant. Mostly this happens when someone accidentally adds semicolons at the end of the operands. Clippy puts this lint in the correctness group, at the level deny.

Code
unit_cmp
Severity
deny

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

Checks for functions that expect closures of type Fn(…) -> Ord where the implemented closure returns the unit type. unit_return_expecting_ord

The lint also suggests to remove the semi-colon at the end of the statement if present. Likely, returning the unit type is unintentional, and could simply be caused by an extra semi-colon. Since () implements Ord it doesn’t cause a compilation error. This is the same reasoning behind the unit_cmp lint. Clippy puts this lint in the correctness group, at the level deny.

Code
unit_return_expecting_ord
Severity
deny
Since
1.47.0

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

Checks for calls to TryInto::try_into and TryFrom::try_from when their infallible counterparts could be used. unnecessary_fallible_conversions

In those cases, the TryInto and TryFrom trait implementation is a blanket impl that forwards to Into or From, which always succeeds. The returned Result<_, Infallible> requires error handling to get the contained value even though the conversion can never fail. Clippy puts this lint in the style group, at the level warn.

Code
unnecessary_fallible_conversions
Severity
warn
Since
1.75.0

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

Checks for filter_map calls that could be replaced by filter or map. unnecessary_filter_map

More specifically it checks if the closure provided is only performing one of the filter or map operations and suggests the appropriate option. Complexity. The intent is also clearer if only a single operation is being performed. Clippy puts this lint in the complexity group, at the level warn.

Code
unnecessary_filter_map
Severity
warn
Since
1.31.0

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

Checks for find_map calls that could be replaced by find or map. unnecessary_find_map

More specifically it checks if the closure provided is only performing one of the find or map operations and suggests the appropriate option. Complexity. The intent is also clearer if only a single operation is being performed. Clippy puts this lint in the complexity group, at the level warn.

Code
unnecessary_find_map
Severity
warn
Since
1.61.0

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

Detects functions that are written to return &str that could return &'static str but instead return a &'a str. unnecessary_literal_bound

This leaves the caller unable to use the &str as &'static str, causing unnecessary allocations or confusion. This is also most likely what you meant to write. Clippy puts this lint in the pedantic group, at the level allow.

Code
unnecessary_literal_bound
Severity
allow
Since
1.84.0

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

Converts some constructs mapping an Enum value for equality comparison. unnecessary_map_or

Calls such as opt.map_or(false, |val| val == 5) are needlessly long and cumbersome, and can be reduced to, for example, opt == Some(5) assuming opt implements PartialEq. Also, calls such as opt.map_or(true, |val| val == 5) can be reduced to opt.is_none_or(|val| val == 5). This lint offers readability and conciseness improvements. Clippy puts this lint in the style group, at the level warn.

Code
unnecessary_map_or
Severity
warn
Since
1.84.0

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

Checks for calls to unwrap_unchecked when an _unchecked variant of the function exists. unnecessary_unwrap_unchecked

Calling the non-unchecked variant may result in checking that is then discarded if unwrap_unchecked is called directly afterwards, whereas the unchecked variant most likely avoids performing the check completely. Clippy puts this lint in the complexity group, at the level warn.

Code
unnecessary_unwrap_unchecked
Severity
warn
Since
1.98.0

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

Checks for functions that are declared async but have no .awaits inside of them. unused_async

Async functions with no async code create overhead, both mentally and computationally. Callers of async methods either need to be calling from an async function themselves or run it on an executor, both of which causes runtime overhead and hassle for the caller. Clippy puts this lint in the pedantic group, at the level allow.

Code
unused_async
Severity
allow
Since
1.54.0

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

Checks for trait method implementations that are declared async but have no .awaits inside of them. unused_async_trait_impl

Async functions with no async code create computational overhead. Even though the trait requires the method to return a future, returning a core::future::ready with the result is more efficient as it reduces the number of states in the Future state machine by at least one. Clippy puts this lint in the pedantic group, at the level allow.

Code
unused_async_trait_impl
Severity
allow
Since
1.98.0

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

Checks for unused written/read amount. unused_io_amount

io::Write::write(_vectored) and io::Read::read(_vectored) are not guaranteed to process the entire buffer. They return how many bytes were processed, which might be smaller than a given buffer’s length. If you don’t need to deal with partial-write/read, use write_all/read_exact instead. Clippy puts this lint in the correctness group, at the level deny.

Code
unused_io_amount
Severity
deny

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

Checks for use Trait where the Trait is only used for its methods and not referenced by a path directly. unused_trait_names

Traits imported that aren’t used directly can be imported anonymously with use Trait as _. It is more explicit, avoids polluting the current scope with unused names and can be useful to show which imports are required for traits. Clippy puts this lint in the restriction group, at the level allow.

Code
unused_trait_names
Severity
allow
Since
1.83.0

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

Checks for usages of the following functions with an argument that constructs a default value (e.g., Default::default or String::new). unwrap_or_default

Readability. Using unwrap_or_default in place of unwrap_or/unwrap_or_else, or or_default in place of or_insert/or_insert_with, is simpler and more concise. Clippy puts this lint in the style group, at the level warn.

Code
unwrap_or_default
Severity
warn
Since
1.56.0

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

Checks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options. unwrap_used

It is better to handle the None or Err case, or at least call .expect(_) with a more helpful message. Still, for a lot of quick-and-dirty code, unwrap is a good choice, which is why this lint is Allow by default. Clippy puts this lint in the restriction group, at the level allow.

Code
unwrap_used
Severity
allow
Since
1.45.0

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