2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::ty::{has_iter_method, implements_trait};
|
|
|
|
use clippy_utils::{get_parent_expr, is_integer_const, path_to_local, path_to_local_id, sugg};
|
2021-03-12 08:30:50 -06:00
|
|
|
use if_chain::if_chain;
|
2021-12-06 05:33:31 -06:00
|
|
|
use rustc_ast::ast::{LitIntType, LitKind};
|
2021-03-12 08:30:50 -06:00
|
|
|
use rustc_errors::Applicability;
|
2022-01-15 16:07:52 -06:00
|
|
|
use rustc_hir::intravisit::{walk_expr, walk_local, walk_pat, walk_stmt, Visitor};
|
2021-12-06 05:33:31 -06:00
|
|
|
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, HirId, HirIdMap, Local, Mutability, Pat, PatKind, Stmt};
|
2021-03-12 08:30:50 -06:00
|
|
|
use rustc_lint::LateContext;
|
2022-01-15 16:07:52 -06:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2022-02-10 11:40:06 -06:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2021-12-06 05:33:31 -06:00
|
|
|
use rustc_span::source_map::Spanned;
|
2021-03-25 13:29:11 -05:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2021-12-06 05:33:31 -06:00
|
|
|
use rustc_typeck::hir_ty_to_ty;
|
2021-03-12 08:30:50 -06:00
|
|
|
use std::iter::Iterator;
|
|
|
|
|
2022-05-21 06:24:00 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2021-03-12 08:30:50 -06:00
|
|
|
enum IncrementVisitorVarState {
|
|
|
|
Initial, // Not examined yet
|
|
|
|
IncrOnce, // Incremented exactly once, may be a loop counter
|
|
|
|
DontWarn,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scan a for loop for variables that are incremented exactly once and not used after that.
|
|
|
|
pub(super) struct IncrementVisitor<'a, 'tcx> {
|
2021-04-08 10:50:13 -05:00
|
|
|
cx: &'a LateContext<'tcx>, // context reference
|
|
|
|
states: HirIdMap<IncrementVisitorVarState>, // incremented variables
|
|
|
|
depth: u32, // depth of conditional expressions
|
2021-03-12 08:30:50 -06:00
|
|
|
done: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> IncrementVisitor<'a, 'tcx> {
|
|
|
|
pub(super) fn new(cx: &'a LateContext<'tcx>) -> Self {
|
|
|
|
Self {
|
|
|
|
cx,
|
2021-04-08 10:50:13 -05:00
|
|
|
states: HirIdMap::default(),
|
2021-03-12 08:30:50 -06:00
|
|
|
depth: 0,
|
|
|
|
done: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn into_results(self) -> impl Iterator<Item = HirId> {
|
|
|
|
self.states.into_iter().filter_map(|(id, state)| {
|
|
|
|
if state == IncrementVisitorVarState::IncrOnce {
|
|
|
|
Some(id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
|
|
|
if self.done {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If node is a variable
|
|
|
|
if let Some(def_id) = path_to_local(expr) {
|
|
|
|
if let Some(parent) = get_parent_expr(self.cx, expr) {
|
|
|
|
let state = self.states.entry(def_id).or_insert(IncrementVisitorVarState::Initial);
|
|
|
|
if *state == IncrementVisitorVarState::IncrOnce {
|
|
|
|
*state = IncrementVisitorVarState::DontWarn;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match parent.kind {
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::AssignOp(op, lhs, rhs) => {
|
2021-03-12 08:30:50 -06:00
|
|
|
if lhs.hir_id == expr.hir_id {
|
|
|
|
*state = if op.node == BinOpKind::Add
|
|
|
|
&& is_integer_const(self.cx, rhs, 1)
|
|
|
|
&& *state == IncrementVisitorVarState::Initial
|
|
|
|
&& self.depth == 0
|
|
|
|
{
|
|
|
|
IncrementVisitorVarState::IncrOnce
|
|
|
|
} else {
|
|
|
|
// Assigned some other value or assigned multiple times
|
|
|
|
IncrementVisitorVarState::DontWarn
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::Assign(lhs, _, _) if lhs.hir_id == expr.hir_id => {
|
2021-06-03 01:41:37 -05:00
|
|
|
*state = IncrementVisitorVarState::DontWarn;
|
2021-03-12 08:30:50 -06:00
|
|
|
},
|
|
|
|
ExprKind::AddrOf(BorrowKind::Ref, mutability, _) if mutability == Mutability::Mut => {
|
2021-06-03 01:41:37 -05:00
|
|
|
*state = IncrementVisitorVarState::DontWarn;
|
2021-03-12 08:30:50 -06:00
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
walk_expr(self, expr);
|
|
|
|
} else if is_loop(expr) || is_conditional(expr) {
|
|
|
|
self.depth += 1;
|
|
|
|
walk_expr(self, expr);
|
|
|
|
self.depth -= 1;
|
|
|
|
} else if let ExprKind::Continue(_) = expr.kind {
|
|
|
|
self.done = true;
|
|
|
|
} else {
|
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum InitializeVisitorState<'hir> {
|
2021-12-06 05:33:31 -06:00
|
|
|
Initial, // Not examined yet
|
|
|
|
Declared(Symbol, Option<Ty<'hir>>), // Declared but not (yet) initialized
|
2021-03-12 08:30:50 -06:00
|
|
|
Initialized {
|
|
|
|
name: Symbol,
|
2021-12-06 05:33:31 -06:00
|
|
|
ty: Option<Ty<'hir>>,
|
2021-03-12 08:30:50 -06:00
|
|
|
initializer: &'hir Expr<'hir>,
|
|
|
|
},
|
|
|
|
DontWarn,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks whether a variable is initialized at the start of a loop and not modified
|
|
|
|
/// and used after the loop.
|
|
|
|
pub(super) struct InitializeVisitor<'a, 'tcx> {
|
|
|
|
cx: &'a LateContext<'tcx>, // context reference
|
|
|
|
end_expr: &'tcx Expr<'tcx>, // the for loop. Stop scanning here.
|
|
|
|
var_id: HirId,
|
|
|
|
state: InitializeVisitorState<'tcx>,
|
|
|
|
depth: u32, // depth of conditional expressions
|
|
|
|
past_loop: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> InitializeVisitor<'a, 'tcx> {
|
|
|
|
pub(super) fn new(cx: &'a LateContext<'tcx>, end_expr: &'tcx Expr<'tcx>, var_id: HirId) -> Self {
|
|
|
|
Self {
|
|
|
|
cx,
|
|
|
|
end_expr,
|
|
|
|
var_id,
|
|
|
|
state: InitializeVisitorState::Initial,
|
|
|
|
depth: 0,
|
|
|
|
past_loop: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 05:33:31 -06:00
|
|
|
pub(super) fn get_result(&self) -> Option<(Symbol, Option<Ty<'tcx>>, &'tcx Expr<'tcx>)> {
|
|
|
|
if let InitializeVisitorState::Initialized { name, ty, initializer } = self.state {
|
|
|
|
Some((name, ty, initializer))
|
2021-03-12 08:30:50 -06:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
|
2022-01-15 16:07:52 -06:00
|
|
|
type NestedFilter = nested_filter::OnlyBodies;
|
2021-03-12 08:30:50 -06:00
|
|
|
|
2021-12-06 05:33:31 -06:00
|
|
|
fn visit_local(&mut self, l: &'tcx Local<'_>) {
|
2021-03-12 08:30:50 -06:00
|
|
|
// Look for declarations of the variable
|
|
|
|
if_chain! {
|
2021-12-06 05:33:31 -06:00
|
|
|
if l.pat.hir_id == self.var_id;
|
|
|
|
if let PatKind::Binding(.., ident, _) = l.pat.kind;
|
2021-03-12 08:30:50 -06:00
|
|
|
then {
|
2021-12-06 05:33:31 -06:00
|
|
|
let ty = l.ty.map(|ty| hir_ty_to_ty(self.cx.tcx, ty));
|
|
|
|
|
|
|
|
self.state = l.init.map_or(InitializeVisitorState::Declared(ident.name, ty), |init| {
|
2021-03-12 08:30:50 -06:00
|
|
|
InitializeVisitorState::Initialized {
|
|
|
|
initializer: init,
|
2021-12-06 05:33:31 -06:00
|
|
|
ty,
|
2021-03-12 08:30:50 -06:00
|
|
|
name: ident.name,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-06 05:33:31 -06:00
|
|
|
|
|
|
|
walk_local(self, l);
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
|
|
|
if matches!(self.state, InitializeVisitorState::DontWarn) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if expr.hir_id == self.end_expr.hir_id {
|
|
|
|
self.past_loop = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// No need to visit expressions before the variable is
|
|
|
|
// declared
|
|
|
|
if matches!(self.state, InitializeVisitorState::Initial) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If node is the desired variable, see how it's used
|
|
|
|
if path_to_local_id(expr, self.var_id) {
|
|
|
|
if self.past_loop {
|
|
|
|
self.state = InitializeVisitorState::DontWarn;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(parent) = get_parent_expr(self.cx, expr) {
|
|
|
|
match parent.kind {
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::AssignOp(_, lhs, _) if lhs.hir_id == expr.hir_id => {
|
2021-03-12 08:30:50 -06:00
|
|
|
self.state = InitializeVisitorState::DontWarn;
|
|
|
|
},
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::Assign(lhs, rhs, _) if lhs.hir_id == expr.hir_id => {
|
2021-12-06 05:33:31 -06:00
|
|
|
self.state = if self.depth == 0 {
|
|
|
|
match self.state {
|
|
|
|
InitializeVisitorState::Declared(name, mut ty) => {
|
|
|
|
if ty.is_none() {
|
|
|
|
if let ExprKind::Lit(Spanned {
|
|
|
|
node: LitKind::Int(_, LitIntType::Unsuffixed),
|
|
|
|
..
|
|
|
|
}) = rhs.kind
|
|
|
|
{
|
|
|
|
ty = None;
|
|
|
|
} else {
|
|
|
|
ty = self.cx.typeck_results().expr_ty_opt(rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InitializeVisitorState::Initialized {
|
|
|
|
initializer: rhs,
|
|
|
|
ty,
|
|
|
|
name,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
InitializeVisitorState::Initialized { ty, name, .. } => {
|
|
|
|
InitializeVisitorState::Initialized {
|
|
|
|
initializer: rhs,
|
|
|
|
ty,
|
|
|
|
name,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => InitializeVisitorState::DontWarn,
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
2021-12-06 05:33:31 -06:00
|
|
|
} else {
|
|
|
|
InitializeVisitorState::DontWarn
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
ExprKind::AddrOf(BorrowKind::Ref, mutability, _) if mutability == Mutability::Mut => {
|
2021-06-03 01:41:37 -05:00
|
|
|
self.state = InitializeVisitorState::DontWarn;
|
2021-03-12 08:30:50 -06:00
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
walk_expr(self, expr);
|
|
|
|
} else if !self.past_loop && is_loop(expr) {
|
|
|
|
self.state = InitializeVisitorState::DontWarn;
|
|
|
|
} else if is_conditional(expr) {
|
|
|
|
self.depth += 1;
|
|
|
|
walk_expr(self, expr);
|
|
|
|
self.depth -= 1;
|
|
|
|
} else {
|
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:07:52 -06:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.cx.tcx.hir()
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_loop(expr: &Expr<'_>) -> bool {
|
|
|
|
matches!(expr.kind, ExprKind::Loop(..))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_conditional(expr: &Expr<'_>) -> bool {
|
|
|
|
matches!(expr.kind, ExprKind::If(..) | ExprKind::Match(..))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
pub(super) enum Nesting {
|
|
|
|
Unknown, // no nesting detected yet
|
|
|
|
RuledOut, // the iterator is initialized or assigned within scope
|
|
|
|
LookFurther, // no nesting detected, no further walk required
|
|
|
|
}
|
|
|
|
|
|
|
|
use self::Nesting::{LookFurther, RuledOut, Unknown};
|
|
|
|
|
|
|
|
pub(super) struct LoopNestVisitor {
|
|
|
|
pub(super) hir_id: HirId,
|
|
|
|
pub(super) iterator: HirId,
|
|
|
|
pub(super) nesting: Nesting,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Visitor<'tcx> for LoopNestVisitor {
|
|
|
|
fn visit_stmt(&mut self, stmt: &'tcx Stmt<'_>) {
|
|
|
|
if stmt.hir_id == self.hir_id {
|
|
|
|
self.nesting = LookFurther;
|
|
|
|
} else if self.nesting == Unknown {
|
|
|
|
walk_stmt(self, stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
|
|
|
if self.nesting != Unknown {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if expr.hir_id == self.hir_id {
|
|
|
|
self.nesting = LookFurther;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match expr.kind {
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::Assign(path, _, _) | ExprKind::AssignOp(_, path, _) => {
|
2021-03-12 08:30:50 -06:00
|
|
|
if path_to_local_id(path, self.iterator) {
|
|
|
|
self.nesting = RuledOut;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => walk_expr(self, expr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_pat(&mut self, pat: &'tcx Pat<'_>) {
|
|
|
|
if self.nesting != Unknown {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if let PatKind::Binding(_, id, ..) = pat.kind {
|
|
|
|
if id == self.iterator {
|
|
|
|
self.nesting = RuledOut;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-06-03 01:41:37 -05:00
|
|
|
walk_pat(self, pat);
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If `arg` was the argument to a `for` loop, return the "cleanest" way of writing the
|
|
|
|
/// actual `Iterator` that the loop uses.
|
|
|
|
pub(super) fn make_iterator_snippet(cx: &LateContext<'_>, arg: &Expr<'_>, applic_ref: &mut Applicability) -> String {
|
2021-03-25 13:29:11 -05:00
|
|
|
let impls_iterator = cx.tcx.get_diagnostic_item(sym::Iterator).map_or(false, |id| {
|
2021-03-12 08:30:50 -06:00
|
|
|
implements_trait(cx, cx.typeck_results().expr_ty(arg), id, &[])
|
|
|
|
});
|
|
|
|
if impls_iterator {
|
|
|
|
format!(
|
|
|
|
"{}",
|
|
|
|
sugg::Sugg::hir_with_applicability(cx, arg, "_", applic_ref).maybe_par()
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// (&x).into_iter() ==> x.iter()
|
|
|
|
// (&mut x).into_iter() ==> x.iter_mut()
|
2022-02-10 11:40:06 -06:00
|
|
|
let arg_ty = cx.typeck_results().expr_ty_adjusted(arg);
|
|
|
|
match &arg_ty.kind() {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-24 21:13:38 -06:00
|
|
|
ty::Ref(_, inner_ty, mutbl) if has_iter_method(cx, *inner_ty).is_some() => {
|
2022-02-10 11:40:06 -06:00
|
|
|
let method_name = match mutbl {
|
2021-03-12 08:30:50 -06:00
|
|
|
Mutability::Mut => "iter_mut",
|
|
|
|
Mutability::Not => "iter",
|
|
|
|
};
|
2022-02-10 11:40:06 -06:00
|
|
|
let caller = match &arg.kind {
|
|
|
|
ExprKind::AddrOf(BorrowKind::Ref, _, arg_inner) => arg_inner,
|
|
|
|
_ => arg,
|
|
|
|
};
|
2021-03-12 08:30:50 -06:00
|
|
|
format!(
|
|
|
|
"{}.{}()",
|
2022-02-10 11:40:06 -06:00
|
|
|
sugg::Sugg::hir_with_applicability(cx, caller, "_", applic_ref).maybe_par(),
|
|
|
|
method_name,
|
2021-03-12 08:30:50 -06:00
|
|
|
)
|
2021-11-04 07:52:36 -05:00
|
|
|
},
|
2021-03-12 08:30:50 -06:00
|
|
|
_ => format!(
|
|
|
|
"{}.into_iter()",
|
|
|
|
sugg::Sugg::hir_with_applicability(cx, arg, "_", applic_ref).maybe_par()
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|