Programming Search⌘K

Rust lints, page 3 of 5

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

Checks for type parameters which are positioned inconsistently between a type definition and impl block. mismatching_type_param_order

Specifically, a parameter in an impl block which has the same name as a parameter in the type def, but is in a different place. Type parameters are determined by their position rather than name. Naming type parameters inconsistently may cause you to refer to the wrong type parameter. Clippy puts this lint in the pedantic group, at the level allow.

Code
mismatching_type_param_order
Severity
allow
Since
1.63.0

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

Checks assertions without a custom panic message. missing_assert_message

Without a good custom message, it’d be hard to understand what went wrong when the assertion fails. A good custom message should be more about why the failure of the assertion is problematic and not what is failed because the assertion already conveys that. Clippy puts this lint in the restriction group, at the level allow.

Code
missing_assert_message
Severity
allow
Since
1.70.0

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

Checks for repeated slice indexing without asserting beforehand that the length is greater than the largest index used to index into the slice. missing_asserts_for_indexing

In the general case where the compiler does not have a lot of information about the length of a slice, indexing it repeatedly will generate a bounds check for every single index. Clippy puts this lint in the restriction group, at the level allow.

Code
missing_asserts_for_indexing
Severity
allow
Since
1.74.0

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

Suggests to use const in thread_local! macro if possible. missing_const_for_thread_local

The thread_local! macro wraps static declarations and makes them thread-local. It supports using a const keyword that may be used for declarations that can be evaluated as a constant expression. This can enable a more efficient thread local implementation that can avoid lazy initialization. For types that do not need to be dropped, this can enable an even more efficient implementation that does not need to track any additional state. Clippy puts this lint in the perf group, at the level warn.

Code
missing_const_for_thread_local
Severity
warn
Since
1.77.0

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

Checks for imports that do not rename the item as specified in the enforced-import-renames config option. missing_enforced_import_renames

Consistency is important; if a project has defined import renames, then they should be followed. More practically, some item names are too vague outside of their defining scope, in which case this can enforce a more meaningful naming. Clippy puts this lint in the style group, at the level warn.

Code
missing_enforced_import_renames
Severity
warn
Since
1.55.0

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

It lints if an exported function, method, trait method with default impl, or trait method impl is not #[inline]. missing_inline_in_public_items

When a function is not marked #[inline], it is not a “small” candidate for automatic inlining, and LTO is not in use, then it is not possible for the function to be inlined into the code of any crate other than the one in which it is defined. Depending on the role of the function and the relationship of the crates, this could significantly reduce performance. Clippy puts this lint in the restriction group, at the level allow.

Code
missing_inline_in_public_items
Severity
allow

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

Checks for empty spin loops missing_spin_loop

The loop body should have something like thread::park() or at least std::hint::spin_loop() to avoid needlessly burning cycles and conserve energy. Perhaps even better use an actual lock, if possible. Clippy puts this lint in the perf group, at the level warn.

Code
missing_spin_loop
Severity
warn
Since
1.61.0

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

Checks for a read and a write to the same variable where whether the read occurs before or after the write depends on the evaluation order of sub-expressions. mixed_read_write_in_expression

While [the evaluation order of sub-expressions] is fully specified in Rust, it still may be confusing to read an expression where the evaluation order affects its behavior. Clippy puts this lint in the restriction group, at the level allow.

Code
mixed_read_write_in_expression
Severity
allow

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

Checks for modules that have the same name as their parent module module_inception

A typical beginner mistake is to have mod foo; and again mod foo { .. } in foo.rs. The expectation is that items inside the inner mod foo { .. } are then available through foo::x, but they are only available through foo::foo::x. If this is done on purpose, it would be better to choose a more representative module name. Clippy puts this lint in the style group, at the level warn.

Code
module_inception
Severity
warn

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

Checks for modulo arithmetic. modulo_arithmetic

The results of modulo (%) operation might differ depending on the language, when negative numbers are involved. If you interop with different languages it might be beneficial to double check all places that use modulo arithmetic. Clippy puts this lint in the restriction group, at the level allow.

Code
modulo_arithmetic
Severity
allow
Since
1.42.0

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

Checks for getting the remainder of integer division by one or minus one. modulo_one

The result for a divisor of one can only ever be zero; for minus one it can cause panic/overflow (if the left operand is the minimal value of the respective integer type) or results in zero. No one will write such code deliberately, unless trying to win an Underhanded Rust Contest. Even for that contest, it’s probably a bad idea. Use something more underhanded. Clippy puts this lint in the correctness group, at the level deny.

Code
modulo_one
Severity
deny

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

Checks for nested assignments. multi_assignments

While this is in most cases already a type mismatch, the result of an assignment being () can throw off people coming from languages like python or C, where such assignments return a copy of the assigned value. Clippy puts this lint in the suspicious group, at the level warn.

Code
multi_assignments
Severity
warn
Since
1.65.0

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

Checks for unsafe blocks that contain more than one unsafe operation. multiple_unsafe_ops_per_block

Combined with undocumented_unsafe_blocks, this lint ensures that each unsafe operation must be independently justified. Combined with unused_unsafe, this lint also ensures elimination of unnecessary unsafe blocks through refactoring. Clippy puts this lint in the restriction group, at the level allow.

Code
multiple_unsafe_ops_per_block
Severity
allow
Since
1.69.0

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

This lint checks for functions that take immutable references and return mutable ones. mut_from_ref

This will not trigger if no unsafe code exists as there are multiple safe functions which will do this transformation Creating a mutable reference which can be repeatably derived from an immutable reference is unsound as it allows creating multiple live mutable references to the same object. Clippy puts this lint in the correctness group, at the level deny.

Code
mut_from_ref
Severity
deny

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

Checks for &mut Mutex::lock calls mut_mutex_lock

Mutex::lock is less efficient than calling Mutex::get_mut. In addition you also have a statically guarantee that the mutex isn’t locked, instead of just a runtime guarantee. Clippy puts this lint in the style group, at the level warn.

Code
mut_mutex_lock
Severity
warn
Since
1.49.0

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

Checks for usage of bitwise and/or operators between booleans, where performance may be improved by using a lazy and. needless_bitwise_bool

The bitwise operators do not support short-circuiting, so it may hinder code performance. Additionally, boolean logic “masked” as bitwise logic is not caught by lints like unnecessary_fold Clippy puts this lint in the pedantic group, at the level allow.

Code
needless_bitwise_bool
Severity
allow
Since
1.54.0

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

The lint checks for if-statements appearing in loops that contain a continue statement in either their main blocks or their else-blocks, when omitting the else-block possibly with some rearrangement of code can make the code easier to understand. needless_continue

The lint also checks if the last statement in the loop is a continue Having explicit else blocks for if statements containing continue in their THEN branch adds unnecessary branching and nesting to the code. Having an else block containing just continue can also be better written by grouping the statements following the whole if statement within the THEN block and omitting the else block completely. Clippy puts this lint in the pedantic group, at the level allow.

Code
needless_continue
Severity
allow

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

Checks for usage of for_each that would be more simply written as a for loop. needless_for_each

for_each may be used after applying iterator transformers like filter for better readability and performance. It may also be used to fit a simple operation on one line. But when none of these apply, a simple for loop is more idiomatic. Clippy puts this lint in the pedantic group, at the level allow.

Code
needless_for_each
Severity
allow
Since
1.53.0

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

Checks for bindings (constants, statics, or let bindings) that are defined with one numeric type but are consistently cast to a different type in all usages. needless_type_cast

If a binding is always cast to a different type when used, it would be clearer and more efficient to define it with the target type from the start. Clippy puts this lint in the nursery group, at the level allow.

Code
needless_type_cast
Severity
allow
Since
1.94.0

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

Checks for the usage of negated comparison operators on types which only implement PartialOrd (e.g., f64). neg_cmp_op_on_partial_ord

These operators make it easy to forget that the underlying types actually allow not only three potential Orderings (Less, Equal, Greater) but also a fourth one (Uncomparable). This is especially easy to miss if the operator based comparison result is negated. Clippy puts this lint in the complexity group, at the level warn.

Code
neg_cmp_op_on_partial_ord
Severity
warn

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

Checks for Rust ABI functions with the #[no_mangle] attribute. no_mangle_with_rust_abi

The Rust ABI is not stable, but in many simple cases matches enough with the C ABI that it is possible to forget to add extern "C" to a function called from C. Changes to the Rust ABI can break this at any point. Clippy puts this lint in the pedantic group, at the level allow.

Code
no_mangle_with_rust_abi
Severity
allow
Since
1.69.0

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

Checks for non-ASCII characters in string and char literals. non_ascii_literal

Yeah, we know, the 90’s called and wanted their charset back. Even so, there still are editors and other programs out there that don’t work well with Unicode. So if the code is meant to be used internationally, on multiple operating systems, or has other portability requirements, activating this lint could be useful. Clippy puts this lint in the restriction group, at the level allow.

Code
non_ascii_literal
Severity
allow

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

Checks for non-canonical implementations of Clone when Copy is already implemented. non_canonical_clone_impl

If both Clone and Copy are implemented, they must agree. This can done by dereferencing self in Clone’s implementation, which will avoid any possibility of the implementations becoming out of sync. Clippy puts this lint in the suspicious group, at the level warn.

Code
non_canonical_clone_impl
Severity
warn
Since
1.72.0

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

Checks for non-canonical implementations of PartialOrd when Ord is already implemented. non_canonical_partial_ord_impl

If both PartialOrd and Ord are implemented, they must agree. This is commonly done by wrapping the result of cmp in Some for partial_cmp. Not doing this may silently introduce an error upon refactoring. Clippy puts this lint in the suspicious group, at the level warn.

Code
non_canonical_partial_ord_impl
Severity
warn
Since
1.73.0

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

This lint warns about a Send implementation for a type that contains fields that are not safe to be sent across threads. non_send_fields_in_send_ty

It tries to detect fields that can cause a soundness issue when sent to another thread (e.g., Rc) while allowing !Send fields that are expected to exist in a Send type, such as raw pointers. Sending the struct to another thread effectively sends all of its fields, and the fields that do not implement Send can lead to soundness bugs such as data races when accessed in a thread that is different from the thread that created it. Clippy puts this lint in the nursery group, at the level allow.

Code
non_send_fields_in_send_ty
Severity
allow
Since
1.57.0

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

Checks for conversions from NonZero types to regular integer types, and suggests using NonZero types for the target as well. non_zero_suggestions

Converting from NonZero types to regular integer types and then back to NonZero types is less efficient and loses the type-safety guarantees provided by NonZero types. Using NonZero types consistently can lead to more optimized code and prevent certain classes of errors related to zero values. Clippy puts this lint in the restriction group, at the level allow.

Code
non_zero_suggestions
Severity
allow
Since
1.83.0

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

Checks for \0 escapes in string and byte literals that look like octal character escapes in C. octal_escapes

C and other languages support octal character escapes in strings, where a backslash is followed by up to three octal digits. For example, \033 stands for the ASCII character 27 (ESC). Rust does not support this notation, but has the escape code \0 which stands for a null byte/character, and any following digits do not form part of the escape sequence. Therefore, \033 is not a compiler error but the result may be surprising. Clippy puts this lint in the suspicious group, at the level warn.

Code
octal_escapes
Severity
warn
Since
1.59.0

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

Lints usage of if let Some(v) = ... { y } else { x } and match .. { Some(v) => y, None/_ => x } which are more idiomatically done with Option::map_or (if the else bit is a pure expression) or Option::map_or_else (if the else bit is an impure expression). option_if_let_else

Using the dedicated functions of the Option type is clearer and more concise than an if let expression. Clippy puts this lint in the nursery group, at the level allow.

Code
option_if_let_else
Severity
allow
Since
1.47.0

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

Checks for calls to .or(foo(..)), .unwrap_or(foo(..)), .or_insert(foo(..)) etc., and suggests to use .or_else(|| foo(..)), .unwrap_or_else(|| foo(..)), .unwrap_or_default() or .or_default() etc. instead. or_fun_call

The function will always be called. This is only bad if it allocates or does some non-trivial amount of work. Clippy puts this lint in the nursery group, at the level allow.

Code
or_fun_call
Severity
allow

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

Detects needlessly owned Cow types. owned_cow

The borrowed types are usually more flexible, in that e.g. a Cow<'_, str> can accept both &str and String while Cow<'_, String> can only accept &String and String. In particular, &str is more general, because it allows for string literals while &String can only be borrowed from a heap-owned String). Clippy puts this lint in the style group, at the level warn.

Code
owned_cow
Severity
warn
Since
1.87.0

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

Checks for binary comparisons to a literal Option::None. partialeq_to_none

A programmer checking if some foo is None via a comparison foo == None is usually inspired from other programming languages (e.g. foo is None in Python). Checking if a value of type Option<T> is (not) equal to None in that way relies on T: PartialEq to do the comparison, which is unneeded. Clippy puts this lint in the style group, at the level warn.

Code
partialeq_to_none
Severity
warn
Since
1.65.0

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

Detects pointer format as well as Debug formatting of raw pointers or function pointers or any types that have a derived Debug impl that recursively contains them. pointer_format

The addresses are only useful in very specific contexts, and certain projects may want to keep addresses of certain data structures or functions from prying hacker eyes as an additional line of security. Clippy puts this lint in the restriction group, at the level allow.

Code
pointer_format
Severity
allow
Since
1.89.0

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

Checks for operations where precedence may be unclear and suggests to add parentheses. precedence

It catches a mixed usage of arithmetic and bit shifting/combining operators, as well as method calls applied to closures. Not everyone knows the precedence of those operators by heart, so expressions like these may trip others trying to reason about the code. Clippy puts this lint in the complexity group, at the level warn.

Code
precedence
Severity
warn

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

This lint checks for function arguments of type &String, &Vec, &PathBuf, and Cow<_>. ptr_arg

It will also suggest you replace .clone() calls with the appropriate .to_owned()/to_string() calls. Requiring the argument to be of the specific type makes the function less useful for no benefit; slices in the form of &[T] or &str usually suffice and can be obtained from other types, too. Clippy puts this lint in the style group, at the level warn.

Code
ptr_arg
Severity
warn

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

Checks for as casts between raw pointers that don’t change their constness, namely *const T to *const U and *mut T to *mut U. ptr_as_ptr

Though as casts between raw pointers are not terrible, pointer::cast is safer because it cannot accidentally change the pointer’s mutability, nor cast the pointer to other types like usize. Clippy puts this lint in the pedantic group, at the level allow.

Code
ptr_as_ptr
Severity
allow
Since
1.51.0

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

Checks for as casts between raw pointers that change their constness, namely *const T to *mut T and *mut T to *const T. ptr_cast_constness

Though as casts between raw pointers are not terrible, pointer::cast_mut and pointer::cast_const are safer because they cannot accidentally cast the pointer to another type. Or, when null pointers are involved, null() and null_mut() can be used directly. Clippy puts this lint in the pedantic group, at the level allow.

Code
ptr_cast_constness
Severity
allow
Since
1.72.0

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

Checks for Rc<Mutex<T>>. rc_mutex

Rc is used in single thread and Mutex is used in multi thread. Consider using Rc<RefCell<T>> in single thread or Arc<Mutex<T>> in multi thread. Clippy puts this lint in the restriction group, at the level allow.

Code
rc_mutex
Severity
allow
Since
1.55.0

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

Looks for calls to [Stdin::read_line] to read a line from the standard input into a string, then later attempting to use that string for an operation that will never work for strings with a trailing newline character in it (e.g. parsing into a i32). read_line_without_trim

The operation will always fail at runtime no matter what the user enters, thus making it a useless operation. Clippy puts this lint in the correctness group, at the level deny.

Code
read_line_without_trim
Severity
deny
Since
1.73.0

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

Checks for format trait implementations (e.g. recursive_format_impl

Display) with a recursive call to itself which uses self as a parameter. This is typically done indirectly with the write! macro or with to_string(). This will lead to infinite recursion and a stack overflow. Clippy puts this lint in the correctness group, at the level deny.

Code
recursive_format_impl
Severity
deny
Since
1.48.0

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

Checks for [all @ ..] patterns. redundant_at_rest_pattern

In all cases, all works fine and can often make code simpler, as you possibly won’t need to convert from say a Vec to a slice by dereferencing. Clippy puts this lint in the complexity group, at the level warn.

Code
redundant_at_rest_pattern
Severity
warn
Since
1.72.0

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

Checks for test functions (functions annotated with #[test]) that are prefixed with test_ which is redundant. redundant_test_prefix

This is redundant because test functions are already annotated with #[test]. Moreover, it clutters the output of cargo test since test functions are expanded as module::tests::test_use_case in the output. Without the redundant prefix, the output becomes module::tests::use_case, which is more readable. Clippy puts this lint in the restriction group, at the level allow.

Code
redundant_test_prefix
Severity
allow
Since
1.88.0

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