2021-01-10 17:51:43 -06:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_note;
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::source::snippet;
|
2021-01-10 17:51:43 -06: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;
|
|
|
|
use rustc_hir::{Block, Body, BodyOwnerKind, Expr, ExprKind, HirId, Node, Pat, PatKind, QPath, UnOp};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
|
|
|
use rustc_span::{Span, Symbol};
|
2015-08-21 10:11:34 -05:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for bindings that shadow other bindings already in
|
2019-03-05 10:50:33 -06:00
|
|
|
/// scope, while just changing reference level or mutability.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Not much, in fact it's a very common pattern in Rust
|
2019-03-05 10:50:33 -06:00
|
|
|
/// code. Still, some may opt to avoid it in their code base, they can set this
|
|
|
|
/// lint to `Warn`.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2019-08-03 14:24:50 -05:00
|
|
|
/// # let x = 1;
|
2020-06-09 09:36:01 -05:00
|
|
|
/// // Bad
|
2019-03-05 10:50:33 -06:00
|
|
|
/// let x = &x;
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let y = &x; // use different variable name
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-08-06 03:18:36 -05:00
|
|
|
pub SHADOW_SAME,
|
2018-03-28 08:24:26 -05:00
|
|
|
restriction,
|
2019-01-30 19:15:29 -06:00
|
|
|
"rebinding a name to itself, e.g., `let mut x = &mut x`"
|
2016-02-05 17:13:29 -06:00
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for bindings that shadow other bindings already in
|
2019-03-05 10:50:33 -06:00
|
|
|
/// scope, while reusing the original value.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Not too much, in fact it's a common pattern in Rust
|
2019-03-05 10:50:33 -06: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 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2019-08-03 14:24:50 -05:00
|
|
|
/// let x = 2;
|
2019-03-05 10:50:33 -06:00
|
|
|
/// let x = x + 1;
|
|
|
|
/// ```
|
|
|
|
/// use different variable name:
|
|
|
|
/// ```rust
|
2019-08-03 14:24:50 -05:00
|
|
|
/// let x = 2;
|
2019-03-05 10:50:33 -06:00
|
|
|
/// let y = x + 1;
|
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2018-11-27 14:49:09 -06:00
|
|
|
pub SHADOW_REUSE,
|
|
|
|
restriction,
|
2019-01-30 19:15:29 -06:00
|
|
|
"rebinding a name to an expression that re-uses the original value, e.g., `let x = x + 1`"
|
2016-02-05 17:13:29 -06:00
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for bindings that shadow other bindings already in
|
2021-08-22 07:46:15 -05:00
|
|
|
/// scope, either without an initialization or with one that does not even use
|
2019-03-05 10:50:33 -06:00
|
|
|
/// the original value.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Name shadowing can hurt readability, especially in
|
2019-03-05 10:50:33 -06: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 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2019-08-03 11:42:05 -05:00
|
|
|
/// # let y = 1;
|
|
|
|
/// # let z = 2;
|
2019-03-05 10:50:33 -06:00
|
|
|
/// let x = y;
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
|
|
|
/// // Bad
|
2019-03-05 10:50:33 -06:00
|
|
|
/// let x = z; // shadows the earlier binding
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let w = z; // use different variable name
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-08-06 03:18:36 -05:00
|
|
|
pub SHADOW_UNRELATED,
|
2021-09-25 17:14:34 -05:00
|
|
|
restriction,
|
2016-08-06 03:18:36 -05:00
|
|
|
"rebinding a name without even using the original value"
|
2016-02-05 17:13:29 -06:00
|
|
|
}
|
2015-08-21 10:11:34 -05:00
|
|
|
|
2021-01-10 17:51:43 -06:00
|
|
|
#[derive(Default)]
|
|
|
|
pub(crate) struct Shadow {
|
|
|
|
bindings: Vec<FxHashMap<Symbol, Vec<ItemLocalId>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(Shadow => [SHADOW_SAME, SHADOW_REUSE, SHADOW_UNRELATED]);
|
2015-08-21 10:11:34 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for Shadow {
|
2021-01-10 17:51:43 -06:00
|
|
|
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
|
|
|
|
let (id, ident) = match pat.kind {
|
|
|
|
PatKind::Binding(_, hir_id, ident, _) => (hir_id, ident),
|
|
|
|
_ => return,
|
|
|
|
};
|
2021-11-19 02:46:34 -06:00
|
|
|
|
|
|
|
if pat.span.desugaring_kind().is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-10 17:51:43 -06:00
|
|
|
if ident.span.from_expansion() || ident.span.is_dummy() {
|
2016-01-03 22:26:12 -06:00
|
|
|
return;
|
|
|
|
}
|
2021-01-10 17:51:43 -06:00
|
|
|
|
2021-11-19 02:46:34 -06:00
|
|
|
let HirId { owner, local_id } = id;
|
2021-01-10 17:51:43 -06:00
|
|
|
// get (or insert) the list of items for this owner and symbol
|
|
|
|
let data = self.bindings.last_mut().unwrap();
|
|
|
|
let items_with_name = data.entry(ident.name).or_default();
|
2015-08-21 10:11:34 -05:00
|
|
|
|
2021-01-10 17:51:43 -06:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_shadow(cx, owner, prev, local_id) {
|
|
|
|
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 10:11:34 -05:00
|
|
|
}
|
2021-01-10 17:51:43 -06:00
|
|
|
// store the binding
|
|
|
|
items_with_name.push(local_id);
|
2015-08-21 10:11:34 -05:00
|
|
|
}
|
|
|
|
|
2021-01-10 17:51:43 -06:00
|
|
|
fn check_body(&mut self, cx: &LateContext<'_>, body: &Body<'_>) {
|
|
|
|
let hir = cx.tcx.hir();
|
|
|
|
if !matches!(hir.body_owner_kind(hir.body_owner(body.id())), BodyOwnerKind::Closure) {
|
|
|
|
self.bindings.push(FxHashMap::default());
|
2015-08-21 10:11:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 17:51:43 -06:00
|
|
|
fn check_body_post(&mut self, cx: &LateContext<'_>, body: &Body<'_>) {
|
|
|
|
let hir = cx.tcx.hir();
|
|
|
|
if !matches!(hir.body_owner_kind(hir.body_owner(body.id())), BodyOwnerKind::Closure) {
|
|
|
|
self.bindings.pop();
|
|
|
|
}
|
2019-01-20 04:49:45 -06:00
|
|
|
}
|
2015-08-21 10:11:34 -05:00
|
|
|
}
|
|
|
|
|
2021-01-10 17:51:43 -06: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());
|
|
|
|
let first_scope = scope_tree.var_scope(first);
|
|
|
|
let second_scope = scope_tree.var_scope(second);
|
|
|
|
scope_tree.is_subscope_of(second_scope, first_scope)
|
2015-08-25 13:11:03 -05:00
|
|
|
}
|
|
|
|
|
2021-01-10 17:51:43 -06: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)
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2021-01-10 17:51:43 -06:00
|
|
|
Some(expr) if is_local_used(cx, expr, shadowed) => {
|
2021-10-07 03:39:13 -05:00
|
|
|
let msg = format!("`{}` is shadowed", snippet(cx, pat.span, "_"));
|
2021-01-10 17:51:43 -06:00
|
|
|
(SHADOW_REUSE, msg)
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2021-01-10 17:51:43 -06:00
|
|
|
_ => {
|
|
|
|
let msg = format!("`{}` shadows a previous, unrelated binding", snippet(cx, pat.span, "_"));
|
|
|
|
(SHADOW_UNRELATED, msg)
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2021-01-10 17:51:43 -06:00
|
|
|
};
|
|
|
|
span_lint_and_note(
|
|
|
|
cx,
|
|
|
|
lint,
|
|
|
|
span,
|
|
|
|
&msg,
|
|
|
|
Some(cx.tcx.hir().span(shadowed)),
|
|
|
|
"previous binding is here",
|
|
|
|
);
|
2015-08-21 10:11:34 -05:00
|
|
|
}
|
|
|
|
|
2021-01-10 17:51:43 -06: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 {
|
|
|
|
ExprKind::Box(e)
|
|
|
|
| ExprKind::AddrOf(_, _, e)
|
|
|
|
| ExprKind::Block(
|
|
|
|
&Block {
|
|
|
|
stmts: [],
|
|
|
|
expr: Some(e),
|
|
|
|
..
|
2017-08-09 02:30:56 -05:00
|
|
|
},
|
2021-01-10 17:51:43 -06: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 10:11:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 17:51:43 -06:00
|
|
|
/// Finds the "init" expression for a pattern: `let <pat> = <init>;` or
|
|
|
|
/// `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 {
|
|
|
|
ExprKind::Match(e, _, _) => Some(e),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
Node::Local(local) => local.init,
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
return init;
|
2015-08-21 10:11:34 -05:00
|
|
|
}
|
2021-01-10 17:51:43 -06:00
|
|
|
None
|
2015-08-21 10:11:34 -05:00
|
|
|
}
|