2021-10-07 09:21:30 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_note;
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::source::snippet;
|
2021-10-07 09:21:30 +00:00
|
|
|
use clippy_utils::visitors::is_local_used;
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
use rustc_hir::def::Res;
|
|
|
|
use rustc_hir::def_id::LocalDefId;
|
|
|
|
use rustc_hir::hir_id::ItemLocalId;
|
2022-06-04 11:34:07 +00:00
|
|
|
use rustc_hir::{Block, Body, BodyOwnerKind, Expr, ExprKind, HirId, Let, Node, Pat, PatKind, QPath, UnOp};
|
2021-10-07 09:21:30 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
|
|
|
use rustc_span::{Span, Symbol};
|
2015-08-21 15:11:34 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for bindings that shadow other bindings already in
|
2019-03-05 16:50:33 +00:00
|
|
|
/// scope, while just changing reference level or mutability.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Not much, in fact it's a very common pattern in Rust
|
2019-03-05 16:50:33 +00:00
|
|
|
/// code. Still, some may opt to avoid it in their code base, they can set this
|
|
|
|
/// lint to `Warn`.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
2019-08-03 19:24:50 +00:00
|
|
|
/// # let x = 1;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let x = &x;
|
2022-06-16 15:39:06 +00:00
|
|
|
/// ```
|
2020-06-09 14:36:01 +00:00
|
|
|
///
|
2022-06-16 15:39:06 +00:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// # let x = 1;
|
2020-06-09 14:36:01 +00:00
|
|
|
/// let y = &x; // use different variable name
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-08-06 08:18:36 +00:00
|
|
|
pub SHADOW_SAME,
|
2018-03-28 13:24:26 +00:00
|
|
|
restriction,
|
2019-01-31 01:15:29 +00:00
|
|
|
"rebinding a name to itself, e.g., `let mut x = &mut x`"
|
2016-02-05 23:13:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for bindings that shadow other bindings already in
|
2019-03-05 16:50:33 +00:00
|
|
|
/// scope, while reusing the original value.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Not too much, in fact it's a common pattern in Rust
|
2019-03-05 16:50:33 +00:00
|
|
|
/// code. Still, some argue that name shadowing like this hurts readability,
|
|
|
|
/// because a value may be bound to different things depending on position in
|
|
|
|
/// the code.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
2019-08-03 19:24:50 +00:00
|
|
|
/// let x = 2;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let x = x + 1;
|
|
|
|
/// ```
|
|
|
|
/// use different variable name:
|
|
|
|
/// ```rust
|
2019-08-03 19:24:50 +00:00
|
|
|
/// let x = 2;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let y = x + 1;
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2018-11-27 20:49:09 +00:00
|
|
|
pub SHADOW_REUSE,
|
|
|
|
restriction,
|
2019-01-31 01:15:29 +00:00
|
|
|
"rebinding a name to an expression that re-uses the original value, e.g., `let x = x + 1`"
|
2016-02-05 23:13:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for bindings that shadow other bindings already in
|
2021-08-22 12:46:15 +00:00
|
|
|
/// scope, either without an initialization or with one that does not even use
|
2019-03-05 16:50:33 +00:00
|
|
|
/// the original value.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Name shadowing can hurt readability, especially in
|
2019-03-05 16:50:33 +00:00
|
|
|
/// large code bases, because it is easy to lose track of the active binding at
|
|
|
|
/// any place in the code. This can be alleviated by either giving more specific
|
|
|
|
/// names to bindings or introducing more scopes to contain the bindings.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
2019-08-03 16:42:05 +00:00
|
|
|
/// # let y = 1;
|
|
|
|
/// # let z = 2;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let x = y;
|
|
|
|
/// let x = z; // shadows the earlier binding
|
2022-06-16 15:39:06 +00:00
|
|
|
/// ```
|
2020-06-09 14:36:01 +00:00
|
|
|
///
|
2022-06-16 15:39:06 +00:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// # let y = 1;
|
|
|
|
/// # let z = 2;
|
|
|
|
/// let x = y;
|
2020-06-09 14:36:01 +00:00
|
|
|
/// let w = z; // use different variable name
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-08-06 08:18:36 +00:00
|
|
|
pub SHADOW_UNRELATED,
|
2021-10-07 09:21:30 +00:00
|
|
|
restriction,
|
2016-08-06 08:18:36 +00:00
|
|
|
"rebinding a name without even using the original value"
|
2016-02-05 23:13:29 +00:00
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
|
2021-10-07 09:21:30 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub(crate) struct Shadow {
|
2022-07-18 07:39:37 +00:00
|
|
|
bindings: Vec<(FxHashMap<Symbol, Vec<ItemLocalId>>, LocalDefId)>,
|
2021-10-07 09:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(Shadow => [SHADOW_SAME, SHADOW_REUSE, SHADOW_UNRELATED]);
|
2015-08-21 15:11:34 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for Shadow {
|
2021-10-07 09:21:30 +00:00
|
|
|
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
|
2023-07-17 08:19:29 +00:00
|
|
|
let PatKind::Binding(_, id, ident, _) = pat.kind else {
|
|
|
|
return;
|
|
|
|
};
|
2021-12-06 11:33:31 +00:00
|
|
|
|
2023-05-05 15:45:49 +00:00
|
|
|
if pat.span.desugaring_kind().is_some() || pat.span.from_expansion() {
|
2021-12-06 11:33:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:21:30 +00:00
|
|
|
if ident.span.from_expansion() || ident.span.is_dummy() {
|
2016-01-04 04:26:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
|
2021-12-06 11:33:31 +00:00
|
|
|
let HirId { owner, local_id } = id;
|
2021-10-07 09:21:30 +00:00
|
|
|
// get (or insert) the list of items for this owner and symbol
|
2022-07-18 07:39:37 +00:00
|
|
|
let (ref mut data, scope_owner) = *self.bindings.last_mut().unwrap();
|
2021-10-07 09:21:30 +00:00
|
|
|
let items_with_name = data.entry(ident.name).or_default();
|
|
|
|
|
|
|
|
// check other bindings with the same name, most recently seen first
|
|
|
|
for &prev in items_with_name.iter().rev() {
|
|
|
|
if prev == local_id {
|
|
|
|
// repeated binding in an `Or` pattern
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-18 07:39:37 +00:00
|
|
|
if is_shadow(cx, scope_owner, prev, local_id) {
|
2021-10-07 09:21:30 +00:00
|
|
|
let prev_hir_id = HirId { owner, local_id: prev };
|
|
|
|
lint_shadow(cx, pat, prev_hir_id, ident.span);
|
|
|
|
// only lint against the "nearest" shadowed binding
|
|
|
|
break;
|
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
2021-10-07 09:21:30 +00:00
|
|
|
// store the binding
|
|
|
|
items_with_name.push(local_id);
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 09:21:30 +00:00
|
|
|
fn check_body(&mut self, cx: &LateContext<'_>, body: &Body<'_>) {
|
|
|
|
let hir = cx.tcx.hir();
|
2022-07-18 07:39:37 +00:00
|
|
|
let owner_id = hir.body_owner_def_id(body.id());
|
|
|
|
if !matches!(hir.body_owner_kind(owner_id), BodyOwnerKind::Closure) {
|
|
|
|
self.bindings.push((FxHashMap::default(), owner_id));
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:21:30 +00:00
|
|
|
fn check_body_post(&mut self, cx: &LateContext<'_>, body: &Body<'_>) {
|
|
|
|
let hir = cx.tcx.hir();
|
2022-06-04 11:34:07 +00:00
|
|
|
if !matches!(
|
|
|
|
hir.body_owner_kind(hir.body_owner_def_id(body.id())),
|
|
|
|
BodyOwnerKind::Closure
|
|
|
|
) {
|
2021-10-07 09:21:30 +00:00
|
|
|
self.bindings.pop();
|
|
|
|
}
|
2019-01-20 10:49:45 +00:00
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 11:34:07 +00:00
|
|
|
fn is_shadow(cx: &LateContext<'_>, owner: LocalDefId, first: ItemLocalId, second: ItemLocalId) -> bool {
|
|
|
|
let scope_tree = cx.tcx.region_scope_tree(owner.to_def_id());
|
|
|
|
if let Some(first_scope) = scope_tree.var_scope(first) {
|
|
|
|
if let Some(second_scope) = scope_tree.var_scope(second) {
|
|
|
|
return scope_tree.is_subscope_of(second_scope, first_scope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
2015-08-25 18:11:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 09:21:30 +00:00
|
|
|
fn lint_shadow(cx: &LateContext<'_>, pat: &Pat<'_>, shadowed: HirId, span: Span) {
|
|
|
|
let (lint, msg) = match find_init(cx, pat.hir_id) {
|
|
|
|
Some(expr) if is_self_shadow(cx, pat, expr, shadowed) => {
|
|
|
|
let msg = format!(
|
|
|
|
"`{}` is shadowed by itself in `{}`",
|
|
|
|
snippet(cx, pat.span, "_"),
|
|
|
|
snippet(cx, expr.span, "..")
|
|
|
|
);
|
|
|
|
(SHADOW_SAME, msg)
|
2022-06-04 11:34:07 +00:00
|
|
|
},
|
2021-10-07 09:21:30 +00:00
|
|
|
Some(expr) if is_local_used(cx, expr, shadowed) => {
|
2021-10-21 11:11:36 +00:00
|
|
|
let msg = format!("`{}` is shadowed", snippet(cx, pat.span, "_"));
|
2021-10-07 09:21:30 +00:00
|
|
|
(SHADOW_REUSE, msg)
|
2022-06-04 11:34:07 +00:00
|
|
|
},
|
2021-10-07 09:21:30 +00:00
|
|
|
_ => {
|
2022-06-04 11:34:07 +00:00
|
|
|
let msg = format!("`{}` shadows a previous, unrelated binding", snippet(cx, pat.span, "_"));
|
2021-10-07 09:21:30 +00:00
|
|
|
(SHADOW_UNRELATED, msg)
|
2022-06-04 11:34:07 +00:00
|
|
|
},
|
2021-10-07 09:21:30 +00:00
|
|
|
};
|
|
|
|
span_lint_and_note(
|
|
|
|
cx,
|
|
|
|
lint,
|
|
|
|
span,
|
|
|
|
&msg,
|
|
|
|
Some(cx.tcx.hir().span(shadowed)),
|
|
|
|
"previous binding is here",
|
|
|
|
);
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 09:21:30 +00:00
|
|
|
/// Returns true if the expression is a simple transformation of a local binding such as `&x`
|
|
|
|
fn is_self_shadow(cx: &LateContext<'_>, pat: &Pat<'_>, mut expr: &Expr<'_>, hir_id: HirId) -> bool {
|
|
|
|
let hir = cx.tcx.hir();
|
|
|
|
let is_direct_binding = hir
|
|
|
|
.parent_iter(pat.hir_id)
|
|
|
|
.map_while(|(_id, node)| match node {
|
|
|
|
Node::Pat(pat) => Some(pat),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.all(|pat| matches!(pat.kind, PatKind::Ref(..) | PatKind::Or(_)));
|
|
|
|
if !is_direct_binding {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
expr = match expr.kind {
|
2023-03-14 17:18:26 +00:00
|
|
|
ExprKind::AddrOf(_, _, e)
|
2022-06-04 11:34:07 +00:00
|
|
|
| ExprKind::Block(
|
|
|
|
&Block {
|
|
|
|
stmts: [],
|
|
|
|
expr: Some(e),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
_,
|
|
|
|
)
|
2021-10-07 09:21:30 +00:00
|
|
|
| ExprKind::Unary(UnOp::Deref, e) => e,
|
|
|
|
ExprKind::Path(QPath::Resolved(None, path)) => break path.res == Res::Local(hir_id),
|
|
|
|
_ => break false,
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 14:10:43 +00:00
|
|
|
/// Finds the "init" expression for a pattern: `let <pat> = <init>;` (or `if let`) or
|
2021-10-07 09:21:30 +00:00
|
|
|
/// `match <init> { .., <pat> => .., .. }`
|
|
|
|
fn find_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> {
|
|
|
|
for (_, node) in cx.tcx.hir().parent_iter(hir_id) {
|
|
|
|
let init = match node {
|
|
|
|
Node::Arm(_) | Node::Pat(_) => continue,
|
|
|
|
Node::Expr(expr) => match expr.kind {
|
2021-12-30 14:10:43 +00:00
|
|
|
ExprKind::Match(e, _, _) | ExprKind::Let(&Let { init: e, .. }) => Some(e),
|
2021-10-07 09:21:30 +00:00
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
Node::Local(local) => local.init,
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
return init;
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
2021-10-07 09:21:30 +00:00
|
|
|
None
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|