.filter(..).next()
RS-W1023.filter(..).next()
is equivalent to .find(..)
. Prefer .find(..)
for
readability.
.clone()
in assignment RS-W1070Consider using .clone_from()
instead of assigning the result of .clone()
.
clone_from()
may perform better in some cases because some structs may have custom implementations of clone_from()
, which improve performance by avoiding needless allocations.
mem::replace
RS-W1113The std::mem
module provides the take()
method to acquire the
current value and replace it with the default value of the same type.
Prefer using this over mem::replace
with T::default()
.
.clamp(min, max)
, instead of .max(min).min(max)
or .min(max).max(min)
RS-W1069Chained Ord::min
and Ord::max
functions can be replaced with Ord::clamp
, which is also a more readable solution.
None
RS-W1108Checking if a value is None
via a direct comparison is usually inspired
from other programming languages (e.g. foo is None
in Python). In Rust,
the Eq
trait is used to perform the comparison, which is unneeded.
The Option
type provides the is_none()
and is_some()
methods which
return a boolean, allowing for more idoimatic comparison.
stream_position(..)
RS-W1109The Seek
trait provides the Seek::stream_position(&mut self)
method, which is functionally
identical to Self::seek(&mut self, SeekFrom::Current(0))
. Consider avoiding the reimplementation.
A function with high cyclomatic complexity can be hard to understand and maintain. Cyclomatic complexity is a software metric that measures the number of independent paths through a function. A higher cyclomatic complexity indicates that the function has more decision points and is more complex.
mem::replace
RS-W1112Option
provides the take()
method for acquiring its current
value (Some(..)
or None
) and replacing it with None
. Prefer
using this over mem::replace
with None
.
std::ptr::null()
RS-W1078Casting 0 as *const
is the same as std::ptr::null()
,
which is more idiomatic and readable. Similarly 0 as *mut
is same as std::ptr::null_mut()
.
then_some(..).unwrap_or(..)
RS-C1015Using then_some(..).unwrap_or(..)
obfuscates the comparison itself
and reduces code readability. It can be better written as an if-else
block.
OnceCell
wrapper around Mutex
RS-W1135When using static Mutex
in Rust, it was common practice to wrap them inside a OnceCell
for safe initialization. Since Rust 1.63, this is no longer necessary as global static Mutex
's can be initialized without any wrapper. Therefore, using a OnceCell
with a Mutex
is redundant and adds unnecessary complexity to the code.
If your code uses a OnceCell
to wrap a global static Mutex
, you can simplify it by removing the OnceCell
wrapper and initializing the Mutex
directly.
Lint attributes have no effect on crate imports. Neither can lint-attributes affect module-space attributes of the imported items.
Expressions of the form x == true
, !y == false
are unnecessary. Consider using the variable directly.
Seek::rewind
RS-W1046Calling Seek::seek
with SeekFrom::Start(0)
as the position is equivalent to
Seek::rewind
. Consider using Seek::rewind
to improve readibility.
Rc::new("..")
RS-W1062Prefer Rc::from("..")
over Rc::new("..").
Rc<&str>` is a pointer to a
pointer. This adds another level of indirection without any benefit whatsoever.
u8
RS-W1080Casting values from a larger type to a smaller type is error-prone. This can be
avoided when casting char
literals to u8
by using the byte literal
directly.
Empty tuple fields in structs or enum variants do not affect the program in a meaningful way other than making the variant or struct identifier a constructor type rather than a value type. Consider revisiting this definition.
T::BITS
RS-W1132All integer types provide the size of the type in bits through T::BITS
.
Consider using this instead of manually calling std::mem::size_of::<T>()
and multiplying the result by 8.
vec.first()
RS-W1116The Vec
type provides the first()
and first_mut()
methods to retrieve
the first element of the vector. Similarly, the VecDeque
type provides the
front()
and front_mut()
methods.
Prefer using these instead of get(0)
and get_mut(0)
.
print!
in implementation of Debug
RS-W1133The fmt
method provided by the Debug
trait uses a formatter to
generate the output string. It does not directly write to stdout or
stderr. Using print!
or eprint!
in this implementation leads to
incorrect functionality. Instead, use the standard suite of debug
representations provided by the Formatter trait (debug_struct
,
debug_tuple
, debug_list
, debug_set
, debug_map
). If you wish
to use a custom representation for debugging, you can use the write!
macro.