2023-07-31 16:53:53 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
|
|
|
use clippy_utils::is_from_proc_macro;
|
|
|
|
use clippy_utils::ty::needs_ordered_drop;
|
2023-08-11 07:05:13 -05:00
|
|
|
use rustc_ast::Mutability;
|
2023-07-31 16:53:53 -05:00
|
|
|
use rustc_hir::def::Res;
|
2023-10-06 10:35:45 -05:00
|
|
|
use rustc_hir::{BindingAnnotation, ByRef, ExprKind, HirId, Local, Node, Pat, PatKind, QPath};
|
2024-02-08 13:24:42 -06:00
|
|
|
use rustc_hir_typeck::expr_use_visitor::PlaceBase;
|
2023-07-31 16:53:53 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
2023-10-06 10:35:45 -05:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2024-02-08 13:24:42 -06:00
|
|
|
use rustc_middle::ty::UpvarCapture;
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::declare_lint_pass;
|
2023-07-31 16:53:53 -05:00
|
|
|
use rustc_span::symbol::Ident;
|
2023-08-11 07:05:13 -05:00
|
|
|
use rustc_span::DesugaringKind;
|
2023-07-31 16:53:53 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// Checks for redundant redefinitions of local bindings.
|
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Redundant redefinitions of local bindings do not change behavior and are likely to be unintended.
|
|
|
|
///
|
|
|
|
/// Note that although these bindings do not affect your code's meaning, they _may_ affect `rustc`'s stack allocation.
|
|
|
|
///
|
|
|
|
/// ### Example
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2023-07-31 16:53:53 -05:00
|
|
|
/// let a = 0;
|
|
|
|
/// let a = a;
|
|
|
|
///
|
|
|
|
/// fn foo(b: i32) {
|
|
|
|
/// let b = b;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2023-07-31 16:53:53 -05:00
|
|
|
/// let a = 0;
|
|
|
|
/// // no redefinition with the same name
|
|
|
|
///
|
|
|
|
/// fn foo(b: i32) {
|
|
|
|
/// // no redefinition with the same name
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-10-06 10:35:45 -05:00
|
|
|
#[clippy::version = "1.73.0"]
|
2023-07-31 16:53:53 -05:00
|
|
|
pub REDUNDANT_LOCALS,
|
|
|
|
correctness,
|
|
|
|
"redundant redefinition of a local binding"
|
|
|
|
}
|
|
|
|
declare_lint_pass!(RedundantLocals => [REDUNDANT_LOCALS]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
|
|
|
|
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
|
2023-11-16 12:13:24 -06:00
|
|
|
if !local.span.is_desugaring(DesugaringKind::Async)
|
2023-07-31 16:53:53 -05:00
|
|
|
// the pattern is a single by-value binding
|
2023-11-16 12:13:24 -06:00
|
|
|
&& let PatKind::Binding(BindingAnnotation(ByRef::No, mutability), _, ident, None) = local.pat.kind
|
2023-07-31 16:53:53 -05:00
|
|
|
// the binding is not type-ascribed
|
2023-11-16 12:13:24 -06:00
|
|
|
&& local.ty.is_none()
|
2023-07-31 16:53:53 -05:00
|
|
|
// the expression is a resolved path
|
2023-11-16 12:13:24 -06:00
|
|
|
&& let Some(expr) = local.init
|
|
|
|
&& let ExprKind::Path(qpath @ QPath::Resolved(None, path)) = expr.kind
|
2023-07-31 16:53:53 -05:00
|
|
|
// the path is a single segment equal to the local's name
|
2023-11-16 12:13:24 -06:00
|
|
|
&& let [last_segment] = path.segments
|
|
|
|
&& last_segment.ident == ident
|
2023-07-31 16:53:53 -05:00
|
|
|
// resolve the path to its defining binding pattern
|
2023-11-16 12:13:24 -06:00
|
|
|
&& let Res::Local(binding_id) = cx.qpath_res(&qpath, expr.hir_id)
|
2023-12-01 07:28:34 -06:00
|
|
|
&& let Node::Pat(binding_pat) = cx.tcx.hir_node(binding_id)
|
2023-07-31 16:53:53 -05:00
|
|
|
// the previous binding has the same mutability
|
2023-11-16 12:13:24 -06:00
|
|
|
&& find_binding(binding_pat, ident).is_some_and(|bind| bind.1 == mutability)
|
2023-08-11 07:05:13 -05:00
|
|
|
// the local does not change the effect of assignments to the binding. see #11290
|
2023-11-16 12:13:24 -06:00
|
|
|
&& !affects_assignments(cx, mutability, binding_id, local.hir_id)
|
2023-07-31 16:53:53 -05:00
|
|
|
// the local does not affect the code's drop behavior
|
2023-11-16 12:13:24 -06:00
|
|
|
&& !needs_ordered_drop(cx, cx.typeck_results().expr_ty(expr))
|
2023-07-31 16:53:53 -05:00
|
|
|
// the local is user-controlled
|
2023-11-16 12:13:24 -06:00
|
|
|
&& !in_external_macro(cx.sess(), local.span)
|
|
|
|
&& !is_from_proc_macro(cx, expr)
|
2024-02-08 13:24:42 -06:00
|
|
|
&& !is_by_value_closure_capture(cx, local.hir_id, binding_id)
|
2023-11-16 12:13:24 -06:00
|
|
|
{
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
REDUNDANT_LOCALS,
|
|
|
|
local.span,
|
|
|
|
&format!("redundant redefinition of a binding `{ident}`"),
|
|
|
|
Some(binding_pat.span),
|
|
|
|
&format!("`{ident}` is initially defined here"),
|
|
|
|
);
|
2023-07-31 16:53:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 13:24:42 -06:00
|
|
|
/// Checks if the enclosing body is a closure and if the given local is captured by value.
|
|
|
|
///
|
|
|
|
/// In those cases, the redefinition may be necessary to force a move:
|
|
|
|
/// ```
|
|
|
|
/// fn assert_static<T: 'static>(_: T) {}
|
|
|
|
///
|
|
|
|
/// let v = String::new();
|
|
|
|
/// let closure = || {
|
|
|
|
/// let v = v; // <- removing this redefinition makes `closure` no longer `'static`
|
|
|
|
/// dbg!(&v);
|
|
|
|
/// };
|
|
|
|
/// assert_static(closure);
|
|
|
|
/// ```
|
|
|
|
fn is_by_value_closure_capture(cx: &LateContext<'_>, redefinition: HirId, root_variable: HirId) -> bool {
|
|
|
|
let closure_def_id = cx.tcx.hir().enclosing_body_owner(redefinition);
|
|
|
|
|
|
|
|
cx.tcx.is_closure_or_coroutine(closure_def_id.to_def_id())
|
|
|
|
&& cx.tcx.closure_captures(closure_def_id).iter().any(|c| {
|
|
|
|
matches!(c.info.capture_kind, UpvarCapture::ByValue)
|
|
|
|
&& matches!(c.place.base, PlaceBase::Upvar(upvar) if upvar.var_path.hir_id == root_variable)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-31 16:53:53 -05:00
|
|
|
/// Find the annotation of a binding introduced by a pattern, or `None` if it's not introduced.
|
|
|
|
fn find_binding(pat: &Pat<'_>, name: Ident) -> Option<BindingAnnotation> {
|
|
|
|
let mut ret = None;
|
|
|
|
|
|
|
|
pat.each_binding_or_first(&mut |annotation, _, _, ident| {
|
|
|
|
if ident == name {
|
|
|
|
ret = Some(annotation);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2023-08-11 07:05:13 -05:00
|
|
|
/// Check if a rebinding of a local changes the effect of assignments to the binding.
|
|
|
|
fn affects_assignments(cx: &LateContext<'_>, mutability: Mutability, bind: HirId, rebind: HirId) -> bool {
|
|
|
|
let hir = cx.tcx.hir();
|
|
|
|
|
|
|
|
// the binding is mutable and the rebinding is in a different scope than the original binding
|
|
|
|
mutability == Mutability::Mut && hir.get_enclosing_scope(bind) != hir.get_enclosing_scope(rebind)
|
|
|
|
}
|