Crate clippy_utils Copy item path
Summary Source pub use self::attrs ::*;
ast_utils Utilities for manipulating and extracting information from rustc_ast::ast
. attrs check_proc_macro 🔒 This module handles checking if the span given is from a proc-macro or not. comparisons Utility functions for comparison operators. consts A simple const eval API, for use on arbitrary HIR expressions. diagnostics Clippy wrappers around rustc’s diagnostic functions. eager_or_lazy Utilities for evaluating whether eagerly evaluated expressions can be made lazy and vice versa. higher This module contains functions that retrieve specific elements. hir_utils 🔒 macros mir msrvs numeric_literal paths This module contains paths to types and functions Clippy needs to know
about. ptr qualify_min_const_fn source Utils for extracting, inspecting or transforming source code str_utils sugg Contains utility functions to generate suggestions. sym_helper ty Util methods for rustc_middle::ty
usage visitors extract_msrv_attr maybe_path 🔒 op_utils 🔒 sym Convenience wrapper around rustc’s Symbol::intern
ContainsName ExprUseCtxt The context an expressions value is used in. HirEqInterExpr SpanlessEq 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. SpanlessHash 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. CaptureKind How a local is captured by a closure DefinedTy A type definition as it would be viewed from within a function. ExprUseNode The node which consumes a value. RequiresSemi BINOP_TRAITS Binary operation traits like LangItem::Add
OP_ASSIGN_TRAITS Operator-Assign traits like LangItem::AddAssign
TEST_ITEM_NAMES_CACHE 🔒 MaybePath any_parent_has_attr binary_expr_needs_parentheses Returns true if the given expr
is binary expression that needs to be wrapped in parentheses. binop_traits Converts BinOpKind::Add
to (LangItem::Add, LangItem::AddAssign)
, for example both Checks if the two Option
s are both None
or some equal values as per
eq_fn
. can_move_expr_to_closure Checks if the expression can be moved into a closure as is. This will return a list of captures
if so, otherwise, None
. can_move_expr_to_closure_no_visit Checks if the top level expression can be moved into a closure as is.
Currently checks for: can_mut_borrow_both Checks if two expressions can be mutably borrowed simultaneously
and they aren’t dependent on borrowing same thing twice capture_local_usage Given an expression referencing a local, determines how it would be captured in a closure. clip clip unused bytes contains_name Checks if an Expr
contains a certain name. contains_return Returns true
if expr
contains a return expression count_eq Counts how many elements of the slices are equal as per eq_fn
. def_path_def_ids Resolves a def path like std::vec::Vec
to its DefId
s, see def_path_res
. def_path_res Resolves a def path like std::vec::Vec
. def_path_res_with_base Resolves a def path like vec::Vec
with the base std
. eq_expr_value Checks if two expressions evaluate to the same value, and don’t contain any side effects. expr_custom_deref_adjustment Gets the mutability of the custom deref adjustment, if any. expr_or_init If the given expression is a local binding, find the initializer expression.
If that initializer expression is another local binding, find its initializer again. expr_requires_coercion Returns true if the specified expr
requires coercion,
meaning that it either has a coercion or propagates a coercion from one of its sub expressions. expr_use_ctxt Gets the context an expression’s value is used in. find_binding_init Finds the initializer expression for a local binding. Returns None
if the binding is mutable. find_crates Finds the crates called name
, may be multiple due to multiple major versions. find_primitive_impls 🔒 fn_def_id Returns the DefId
of the callee if the given expression is a function or method call. fn_def_id_with_node_args Returns the DefId
of the callee if the given expression is a function or method call,
as well as its node args. fn_has_unsatisfiable_preds Check if it’s even possible to satisfy the where
clause for the item. fulfill_or_allowed Returns true
if the lint is #[allow]
ed or #[expect]
ed at any of the ids
, fulfilling all
of the expectations in ids
get_async_fn_body Peels away all the compiler generated code surrounding the body of an async function, get_enclosing_block Gets the enclosing block, if any. get_enclosing_loop_or_multi_call_closure Gets the loop or closure enclosing the given expression, if any. get_expr_use_or_unification_node 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. get_item_name Gets the name of the item the expression is in, if available. get_parent_as_impl Gets the parent node if it’s an impl block. get_parent_expr Gets the parent expression, if any –- this is useful to constrain a lint. get_parent_expr_for_hir This retrieves the parent for the given HirId
if it’s an expression. This is useful for
constraint lints get_path_from_caller_to_method_type Produces a path from a local caller to the type of the called method. Suitable for user
output/suggestions. get_path_to_callee 🔒 Produce a path from some local caller to the callee. Suitable for user output/suggestions. get_path_to_ty 🔒 get_trait_def_id Convenience function to get the DefId
of a trait by path.
It could be a trait or trait alias. has_attr has_repr_attr hash_expr hash_stmt if_sequence 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 }
. in_automatically_derived Checks if the given HIR node is inside an impl
block with the automatically_derived
attribute. inherits_cfg Checks if the item of any of its parents has #[cfg(...)]
attribute applied. int_bits is_adjusted Returns true
if the given Expr
has been coerced before. is_async_fn Checks if the given function kind is an async function. is_block_like Returns true if the given expr
is a block or resembled as a block,
such as if
, loop
, match
expressions etc. is_body_identity_function 🔒 Checks if a function’s body represents the identity function. Looks for bodies of the form: is_bool is_cfg_test Checks if id
has a #[cfg(test)]
attribute applied is_ctor_or_promotable_const_function Checks if an expression is constructing a tuple-like enum variant or struct is_def_id_trait_method Checks if the def_id
belongs to a function that is part of a trait impl. is_default_equivalent Returns true if the expr is equal to Default::default()
of its type when evaluated. is_default_equivalent_call Returns true if the expr is equal to Default::default
when evaluated. is_default_equivalent_ctor 🔒 Returns true if the def_id
associated with the path
is recognized as a “default-equivalent”
constructor from the std library is_default_equivalent_from 🔒 is_diag_item_method Checks if a method is defined in an impl of a diagnostic item is_diag_trait_item Checks if a method is in a diagnostic item trait is_diagnostic_item_or_ctor Checks if the DefId
matches the given diagnostic item or it’s constructor. is_direct_expn_of 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 in is_else_clause Checks if the given expression is the else clause of either an if
or if let
expression. is_else_clause_in_let_else Checks if the given expression is the else clause of a let else
expression is_entrypoint_fn Returns true
if the provided def_id
is an entrypoint to a program. is_enum_variant_ctor Checks if {ctor_call_id}(...)
is {enum_item}::{variant_name}(...)
. is_expn_of Returns the pre-expansion span if this comes from an expansion of the
macro name
.
See also is_direct_expn_of
. is_expr_final_block_expr Checks if the expression is the final expression returned from a block. is_expr_identity_function Checks if an expression represents the identity function
Only examines closures and std::convert::identity
is_expr_path_def_path If the expression is a path, resolves it to a DefId
and checks if it matches the given path. is_expr_temporary_value Checks if the expression is a temporary value. is_expr_untyped_identity_function This is the same as is_expr_identity_function
, but does not consider closures
with type annotations for its bindings (or similar) as identity functions: is_expr_used_or_unified Checks if the result of an expression is used, or it’s type is unified with another branch. is_float_literal Checks whether the given expression is a constant literal of the given value. is_from_proc_macro Checks if the item likely came from a proc-macro. is_hir_ty_cfg_dependant is_in_cfg_test Checks if any parent node of HirId
has #[cfg(test)]
attribute applied is_in_const_context Checks if we are currently in a const context (e.g. const fn
, static
/const
initializer). is_in_panic_handler Returns true
if the expression is in the program’s #[panic_handler]
. is_in_test Checks if the node is in a #[test]
function or has any parent node marked #[cfg(test)]
is_in_test_function Checks if the function containing the given HirId
is a #[test]
function is_inherent_method_call Checks if the given method call expression calls an inherent method. is_inside_always_const_context Returns true
if the given HirId
is inside an always constant context. is_inside_let_else Checks if the given expression is a part of let else
returns true
for both the init
and the else
part is_integer_const Checks whether the given expression is a constant integer of the given value.
unlike is_integer_literal
, this version does const folding is_integer_literal Checks whether the given expression is a constant literal of the given value. is_lang_item_or_ctor Checks if the DefId
matches the given LangItem
or it’s constructor. is_lint_allowed Returns true
if the lint is allowed in the current context. This is useful for
skipping long running code when it’s unnecessary is_must_use_func_call is_mutable Returns true
if expr
designates a mutable static, a mutable local binding, or an expression
that can be owned. is_never_expr Check if the expression return !
, a type coerced from !
, or could return !
if the final
expression were turned into a statement. is_no_core_crate is_no_std_crate is_none_arm is_parent_stmt Returns true if the specified HirId
is the top-level expression of a statement or the only
expression in a block. is_path_diagnostic_item If 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. is_path_lang_item If maybe_path
is a path node which resolves to an item, resolves it to a DefId
and checks if
it matches the given lang item. is_range_full Checks whether the given Expr
is a range equivalent to a RangeFull
. is_receiver_of_method_call Returns true if the specified expression is in a receiver position. is_refutable Returns true
if a pattern is refutable. is_res_lang_ctor Checks if a Res
refers to a constructor of a LangItem
For example, use this to check whether a function call or a pattern is Some(..)
. is_self is_self_ty is_slice_of_primitives Returns Option<String>
where String is a textual representation of the type encapsulated in
the slice iff the given expression is a slice of primitives. is_span_if Checks if the span actually refers to an if expression is_span_match Checks if the span actually refers to a match expression is_trait_impl_item Check if parent of a hir node is a trait implementation block.
For example, f
in is_trait_item Checks if the given expression is a path referring an item on the trait
that is marked with the given diagnostic item. is_trait_method Checks if the method call given in expr
belongs to the given trait. is_try Checks if a given expression is a match expression expanded from the ?
operator or the try
macro. is_ty_alias Checks if the given QPath
belongs to a type alias. is_unit_expr is_wild Checks if given pattern is a wildcard (_
) item_children_by_name 🔒 iter_input_pats last_path_segment leaks_droppable_temporary_with_limited_lifetime Returns true if expr
creates any temporary whose type references a non-static lifetime and has
a significant drop and does not consume it. local_is_initialized Checks if the given local has an initializer or is from something other than a let
statement local_item_children_by_name 🔒 match_any_def_paths Checks if the given DefId
matches any of the paths. Returns the index of matching path, if
any. match_def_path Checks if the given DefId
matches the path. match_libc_symbol Checks if the given DefId
matches the libc
item. match_path THIS METHOD IS DEPRECATED. Matches a Path
against a slice of segment string literals. match_qpath THIS METHOD IS DEPRECATED. Matches a QPath
against a slice of segment string literals. match_trait_method Checks if the method call given in expr
belongs to the given trait.
This is a deprecated function, consider using is_trait_method
. maybe_get_relative_path 🔒 Tries to produce a relative path from from
to to
; if such a path would contain more than
max_super
super
items, produces an absolute path instead. Both from
and to
should be in
the local crate. method_calls 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. method_chain_args Matches an Expr
against a chain of methods, and return the matched Expr
s. non_local_item_children_by_name 🔒 nth_arg Convenience function to get the nth argument type of a function. over Checks if two slices are equal as per eq_fn
. pat_and_expr_can_be_question_mark Returns whether the given let pattern and else body can be turned into a question mark pat_is_wild Returns true
if the pattern is a PatWild
, or is an ident prefixed with _
that is not locally used. path_def_id If maybe_path
is a path node which resolves to an item, retrieves the item ID path_res If maybe_path
is a path node, resolves it, otherwise returns Res::Err
path_to_local If the expression is a path to a local, returns the canonical HirId
of the local. path_to_local_id 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. peel_blocks Removes blocks around an expression, only if the block contains just one expression
and no statements. Unsafe blocks are not removed. peel_blocks_with_stmt 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. peel_hir_expr_refs Peels off all references on the expression. Returns the underlying expression and the number of
references removed. peel_hir_expr_unary Peels off all unary operators of an expression. Returns the underlying expression and the number
of operators removed. peel_hir_expr_while Peels of expressions while the given closure returns Some
. peel_hir_pat_refs Peels off all references on the pattern. Returns the underlying pattern and the number of
references removed. peel_hir_ty_options Peel Option<…>
from hir_ty
as long as the HIR name is Option
and it corresponds to the
core::Option<_>
type. peel_hir_ty_refs Peels off all references on the type. Returns the underlying type and the number of references
removed. peel_middle_ty_refs Peels off all references on the type. Returns the underlying type and the number of references
removed. peel_n_hir_expr_refs Peels off up to the given number of references on the expression. Returns the underlying
expression and the number of references removed. peel_ref_operators 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. projection_stack 🔒 This method will return tuple of projection stack and root of the expression,
used in can_mut_borrow_both
. qpath_generic_tys recurse_or_patterns If the pattern is an or
pattern, call the function once for each sub pattern. Otherwise, call
the function once on the given pattern. return_ty Convenience function to get the return type of a function. search_same Returns list of all pairs (a, b)
where eq(a, b) == true
and a
is before b
in exprs
for all a
and b
in
exprs
sext Turn a constant int byte representation into an i128 span_contains_comment Checks whether a given span has any comment token
This checks for all types of comment: line “//”, block “/**”, doc “///” “//!” span_extract_comment Returns all the comments a given span contains span_extract_comments Returns all the comments a given span contains. span_find_starting_semi std_or_core strip_pat_refs tokenize_with_text Tokenizes the input while keeping the text associated with each token. trait_ref_of_method Gets the hir::TraitRef
of the trait the given method is implemented for. unsext clip unused bytes walk_to_expr_usage Walks up the HIR tree from the given expression in an attempt to find where the value is
consumed. with_test_item_names 🔒 MethodArguments Arguments of a method: the receiver and all the additional arguments.