Crate clippy_utils
source ·Re-exports
pub use self::attrs::*;Modules
Utilities for manipulating and extracting information from
rustc_ast::ast.This module handles checking if the span given is from a proc-macro or not.
Utility functions about comparison operators.
Clippy wrappers around rustc’s diagnostic functions.
Utilities for evaluating whether eagerly evaluated expressions can be made lazy and vice versa.
This module contains functions that retrieve specific elements.
This module contains paths to types and functions Clippy needs to know
about.
Utils for extracting, inspecting or transforming source code
Contains utility functions to generate suggestions.
Util methods for
rustc_middle::tyMacros
Structs
Type used to check whether two ast are the same. This is different from the
operator
== on ast types as this operator would compare true equality with
ID and span.Type used to hash an ast element. This is different from the
Hash trait
on ast types as this
trait would consider IDs and spans.Enums
How a local is captured by a closure
Statics
Binary operation traits like
LangItem::AddOperator-Assign traits like
LangItem::AddAssignTraits
Functions
Converts
BinOpKind::Add to (LangItem::Add, LangItem::AddAssign), for exampleChecks if the two
Options are both None or some equal values as per
eq_fn.Checks if the expression can be moved into a closure as is. This will return a list of captures
if so, otherwise,
None.Checks if the top level expression can be moved into a closure as is.
Currently checks for:
Checks if two expressions can be mutably borrowed simultaneously
and they aren’t dependent on borrowing same thing twice
Given an expression referencing a local, determines how it would be captured in a closure.
Note as this will walk up to parent expressions until the capture can be determined it should
only be used while making a closure somewhere a value is consumed. e.g. a block, match arm, or
function argument (other than a receiver).
clip unused bytes
Checks if an
Expr contains a certain name.Returns
true if expr contains a return expressionCounts how many elements of the slices are equal as per
eq_fn.Resolves a def path like
std::vec::Vec.
This function is expensive and should be used sparingly.Checks if two expressions evaluate to the same value, and don’t contain any side effects.
Gets the mutability of the custom deref adjustment, if any.
If the given expression is a local binding, find the initializer expression.
If that initializer expression is another local binding, find its initializer again.
This process repeats as long as possible (but usually no more than once). Initializer
expressions with adjustments are ignored. If this is not desired, use
find_binding_init
instead.Finds the initializer expression for a local binding. Returns
None if the binding is mutable.
By only considering immutable bindings, we guarantee that the returned expression represents the
value of the binding wherever it is referenced.Returns the
DefId of the callee if the given expression is a function or method call.Check if it’s even possible to satisfy the
where clause for the item.Peels away all the compiler generated code surrounding the body of an async function,
Gets the loop or closure enclosing the given expression, if any.
Gets the node where an expression is either used, or it’s type is unified with another branch.
Returns both the node and the
HirId of the closest child node.Gets the name of the item the expression is in, if available.
Gets the parent node if it’s an impl block.
Gets the parent expression, if any –- this is useful to constrain a lint.
This retrieves the parent for the given
HirId if it’s an expression. This is useful for
constraint lintsGets the parent node, if any.
Convenience function to get the
DefId of a trait by path.
It could be a trait or trait alias.Returns the list of condition expressions and the list of blocks in a
sequence of
if/else.
E.g., this returns ([a, b], [c, d, e]) for the expression
if a { c } else if b { d } else { e }.Returns
true if the given NodeId is inside a constant contextReturns
true if the given Expr has been coerced before.Checks if the given function kind is an async function.
Checks if an expression is constructing a tuple-like enum variant or struct
Returns true if the expr is equal to
Default::default() of it’s type when evaluated.
It doesn’t cover all cases, for example indirect function calls (some of std
functions are supported) but it is the best we have.Return true if the expr is equal to
Default::default when evaluated.Returns true if the
def_id associated with the path is recognized as a “default-equivalent”
constructor from the std libraryChecks if a method is defined in an impl of a diagnostic item
Checks if a method is in a diagnostic item trait
Returns the pre-expansion span if the span directly comes from an expansion
of the macro
name.
The difference with is_expn_of is that inChecks if the given expression is the else clause of either an
if or if let expression.Returns
true if the provided def_id is an entrypoint to a program.Returns the pre-expansion span if this comes from an expansion of the
macro
name.
See also is_direct_expn_of.Checks if the expression is the final expression returned from a block.
Checks if an expression represents the identity function
Only examines closures and
std::convert::identityIf the expression is a path, resolves it to a
DefId and checks if it matches the given path.Checks if the result of an expression is used, or it’s type is unified with another branch.
Checks if the item likely came from a proc-macro.
Checks if the item containing the given
HirId has #[cfg(test)] attribute appliedReturns
true if the expression is in the program’s #[panic_handler].Checks if the function containing the given
HirId is a #[test] functionChecks whether the given expression is a constant integer of the given value.
unlike
is_integer_literal, this version does const foldingChecks whether the given expression is a constant literal of the given value.
Checks if a
QPath resolves to a constructor of a LangItem.
For example, use this to check whether a function call or a pattern is Some(..).Returns
true if the lint is allowed in the current context. This is useful for
skipping long running code when it’s unnecessaryIf
maybe_path is a path node which resolves to an item, resolves it to a DefId and checks if
it matches the given diagnostic item.Returns
true if a pattern is refutable.Returns Option where String is a textual representation of the type encapsulated in the
slice iff the given expression is a slice of primitives (as defined in the
is_recursively_primitive_type function) and None otherwise.Checks if the span actually refers to an if expression
Checks if the span actually refers to a match expression
Checks whether item either has
test attribute applied, or
is a module with test in its name.Check if parent of a hir node is a trait implementation block.
For example,
f inChecks if the given expression is a path referring an item on the trait
that is marked with the given diagnostic item.
Checks if the method call given in
expr belongs to the given trait.Checks if a given expression is a match expression expanded from the
?
operator or the try macro.Checks if given pattern is a wildcard (
_)Extends the span to the beginning of the spans line, incl. whitespaces.
Checks if the given
DefId matches any of the paths. Returns the index of matching path, if
any.Checks if the given
DefId matches the path.Matches a function call with the given path and returns the arguments.
Checks if the given
DefId matches the libc item.THIS METHOD IS DEPRECATED and will eventually be removed since it does not match against the
entire path or resolved
DefId. Prefer using match_def_path. Consider getting a DefId from
QPath::Resolved.1.res.opt_def_id().THIS METHOD IS DEPRECATED and will eventually be removed since it does not match against the
entire path or resolved
DefId. Prefer using match_def_path. Consider getting a DefId from
QPath::Resolved.1.res.opt_def_id().Checks if the method call given in
expr belongs to the given trait.
This is a deprecated function, consider using is_trait_method.Returns the method names and argument list of nested method call expressions that make up
expr. method/span lists are sorted with the most recent call first.Matches an
Expr against a chain of methods, and return the matched Exprs.Convenience function to get the nth argument type of a function.
Checks if two slices are equal as per
eq_fn.If
maybe_path is a path node which resolves to an item, retrieves the item IDIf
maybe_path is a path node, resolves it, otherwise returns Res::ErrIf the expression is a path to a local, returns the canonical
HirId of the local.Returns true if the expression is a path to a local with the specified
HirId.
Use this function to see if an expression matches a function argument or a match binding.Removes blocks around an expression, only if the block contains just one expression
and no statements. Unsafe blocks are not removed.
Removes blocks around an expression, only if the block contains just one expression
or just one expression statement with a semicolon. Unsafe blocks are not removed.
Peels off all references on the expression. Returns the underlying expression and the number of
references removed.
Peels of expressions while the given closure returns
Some.Peels off all references on the pattern. Returns the underlying pattern and the number of
references removed.
Peels off all references on the type. Returns the underlying type and the number of references
removed.
Peels off up to the given number of references on the expression. Returns the underlying
expression and the number of references removed.
Removes
AddrOf operators (&) or deref operators (*), but only if a reference type is
dereferenced. An overloaded deref such as Vec to slice would not be removed.This method will return tuple of projection stack and root of the expression,
used in
can_mut_borrow_both.If the pattern is an
or pattern, call the function once for each sub pattern. Otherwise, call
the function once on the given pattern.Convenience function to get the return type of a function.
returns list of all pairs (a, b) from
exprs such that eq(a, b)
hash must be comformed with eqTurn a constant int byte representation into an i128
Checks whether a given span has any comment token
This checks for all types of comment: line “//”, block “/**”, doc “///” “//!”
Gets the
hir::TraitRef of the trait the given method is implemented for.clip unused bytes
Walks the HIR tree from the given expression, up to the node where the value produced by the
expression is consumed. Calls the function for every node encountered this way until it returns
Some.Type Definitions
Arguments of a method: the receiver and all the additional arguments.