1 use clippy_utils::ty::{has_iter_method, implements_trait};
2 use clippy_utils::{get_parent_expr, is_integer_const, path_to_local, path_to_local_id, sugg};
3 use if_chain::if_chain;
4 use rustc_ast::ast::{LitIntType, LitKind};
5 use rustc_errors::Applicability;
6 use rustc_hir::intravisit::{walk_expr, walk_local, walk_pat, walk_stmt, Visitor};
7 use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, HirId, HirIdMap, Local, Mutability, Pat, PatKind, Stmt};
8 use rustc_hir_analysis::hir_ty_to_ty;
9 use rustc_lint::LateContext;
10 use rustc_middle::hir::nested_filter;
11 use rustc_middle::ty::{self, Ty};
12 use rustc_span::source_map::Spanned;
13 use rustc_span::symbol::{sym, Symbol};
14 use std::iter::Iterator;
16 #[derive(Debug, PartialEq, Eq)]
17 enum IncrementVisitorVarState {
18 Initial, // Not examined yet
19 IncrOnce, // Incremented exactly once, may be a loop counter
23 /// Scan a for loop for variables that are incremented exactly once and not used after that.
24 pub(super) struct IncrementVisitor<'a, 'tcx> {
25 cx: &'a LateContext<'tcx>, // context reference
26 states: HirIdMap<IncrementVisitorVarState>, // incremented variables
27 depth: u32, // depth of conditional expressions
30 impl<'a, 'tcx> IncrementVisitor<'a, 'tcx> {
31 pub(super) fn new(cx: &'a LateContext<'tcx>) -> Self {
34 states: HirIdMap::default(),
39 pub(super) fn into_results(self) -> impl Iterator<Item = HirId> {
40 self.states.into_iter().filter_map(|(id, state)| {
41 if state == IncrementVisitorVarState::IncrOnce {
50 impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
51 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
52 // If node is a variable
53 if let Some(def_id) = path_to_local(expr) {
54 if let Some(parent) = get_parent_expr(self.cx, expr) {
55 let state = self.states.entry(def_id).or_insert(IncrementVisitorVarState::Initial);
56 if *state == IncrementVisitorVarState::IncrOnce {
57 *state = IncrementVisitorVarState::DontWarn;
62 ExprKind::AssignOp(op, lhs, rhs) => {
63 if lhs.hir_id == expr.hir_id {
64 *state = if op.node == BinOpKind::Add
65 && is_integer_const(self.cx, rhs, 1)
66 && *state == IncrementVisitorVarState::Initial
69 IncrementVisitorVarState::IncrOnce
71 // Assigned some other value or assigned multiple times
72 IncrementVisitorVarState::DontWarn
76 ExprKind::Assign(lhs, _, _) if lhs.hir_id == expr.hir_id => {
77 *state = IncrementVisitorVarState::DontWarn;
79 ExprKind::AddrOf(BorrowKind::Ref, mutability, _) if mutability == Mutability::Mut => {
80 *state = IncrementVisitorVarState::DontWarn;
86 walk_expr(self, expr);
87 } else if is_loop(expr) || is_conditional(expr) {
89 walk_expr(self, expr);
91 } else if let ExprKind::Continue(_) = expr.kind {
92 // If we see a `continue` block, then we increment depth so that the IncrementVisitor
93 // state will be set to DontWarn if we see the variable being modified anywhere afterwards.
96 walk_expr(self, expr);
101 enum InitializeVisitorState<'hir> {
102 Initial, // Not examined yet
103 Declared(Symbol, Option<Ty<'hir>>), // Declared but not (yet) initialized
106 ty: Option<Ty<'hir>>,
107 initializer: &'hir Expr<'hir>,
112 /// Checks whether a variable is initialized at the start of a loop and not modified
113 /// and used after the loop.
114 pub(super) struct InitializeVisitor<'a, 'tcx> {
115 cx: &'a LateContext<'tcx>, // context reference
116 end_expr: &'tcx Expr<'tcx>, // the for loop. Stop scanning here.
118 state: InitializeVisitorState<'tcx>,
119 depth: u32, // depth of conditional expressions
123 impl<'a, 'tcx> InitializeVisitor<'a, 'tcx> {
124 pub(super) fn new(cx: &'a LateContext<'tcx>, end_expr: &'tcx Expr<'tcx>, var_id: HirId) -> Self {
129 state: InitializeVisitorState::Initial,
135 pub(super) fn get_result(&self) -> Option<(Symbol, Option<Ty<'tcx>>, &'tcx Expr<'tcx>)> {
136 if let InitializeVisitorState::Initialized { name, ty, initializer } = self.state {
137 Some((name, ty, initializer))
144 impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
145 type NestedFilter = nested_filter::OnlyBodies;
147 fn visit_local(&mut self, l: &'tcx Local<'_>) {
148 // Look for declarations of the variable
150 if l.pat.hir_id == self.var_id;
151 if let PatKind::Binding(.., ident, _) = l.pat.kind;
153 let ty = l.ty.map(|ty| hir_ty_to_ty(self.cx.tcx, ty));
155 self.state = l.init.map_or(InitializeVisitorState::Declared(ident.name, ty), |init| {
156 InitializeVisitorState::Initialized {
168 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
169 if matches!(self.state, InitializeVisitorState::DontWarn) {
172 if expr.hir_id == self.end_expr.hir_id {
173 self.past_loop = true;
176 // No need to visit expressions before the variable is
178 if matches!(self.state, InitializeVisitorState::Initial) {
182 // If node is the desired variable, see how it's used
183 if path_to_local_id(expr, self.var_id) {
185 self.state = InitializeVisitorState::DontWarn;
189 if let Some(parent) = get_parent_expr(self.cx, expr) {
191 ExprKind::AssignOp(_, lhs, _) if lhs.hir_id == expr.hir_id => {
192 self.state = InitializeVisitorState::DontWarn;
194 ExprKind::Assign(lhs, rhs, _) if lhs.hir_id == expr.hir_id => {
195 self.state = if self.depth == 0 {
197 InitializeVisitorState::Declared(name, mut ty) => {
199 if let ExprKind::Lit(Spanned {
200 node: LitKind::Int(_, LitIntType::Unsuffixed),
206 ty = self.cx.typeck_results().expr_ty_opt(rhs);
210 InitializeVisitorState::Initialized {
216 InitializeVisitorState::Initialized { ty, name, .. } => {
217 InitializeVisitorState::Initialized {
223 _ => InitializeVisitorState::DontWarn,
226 InitializeVisitorState::DontWarn
229 ExprKind::AddrOf(BorrowKind::Ref, mutability, _) if mutability == Mutability::Mut => {
230 self.state = InitializeVisitorState::DontWarn;
236 walk_expr(self, expr);
237 } else if !self.past_loop && is_loop(expr) {
238 self.state = InitializeVisitorState::DontWarn;
239 } else if is_conditional(expr) {
241 walk_expr(self, expr);
244 walk_expr(self, expr);
248 fn nested_visit_map(&mut self) -> Self::Map {
253 fn is_loop(expr: &Expr<'_>) -> bool {
254 matches!(expr.kind, ExprKind::Loop(..))
257 fn is_conditional(expr: &Expr<'_>) -> bool {
258 matches!(expr.kind, ExprKind::If(..) | ExprKind::Match(..))
261 #[derive(PartialEq, Eq)]
262 pub(super) enum Nesting {
263 Unknown, // no nesting detected yet
264 RuledOut, // the iterator is initialized or assigned within scope
265 LookFurther, // no nesting detected, no further walk required
268 use self::Nesting::{LookFurther, RuledOut, Unknown};
270 pub(super) struct LoopNestVisitor {
271 pub(super) hir_id: HirId,
272 pub(super) iterator: HirId,
273 pub(super) nesting: Nesting,
276 impl<'tcx> Visitor<'tcx> for LoopNestVisitor {
277 fn visit_stmt(&mut self, stmt: &'tcx Stmt<'_>) {
278 if stmt.hir_id == self.hir_id {
279 self.nesting = LookFurther;
280 } else if self.nesting == Unknown {
281 walk_stmt(self, stmt);
285 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
286 if self.nesting != Unknown {
289 if expr.hir_id == self.hir_id {
290 self.nesting = LookFurther;
294 ExprKind::Assign(path, _, _) | ExprKind::AssignOp(_, path, _) => {
295 if path_to_local_id(path, self.iterator) {
296 self.nesting = RuledOut;
299 _ => walk_expr(self, expr),
303 fn visit_pat(&mut self, pat: &'tcx Pat<'_>) {
304 if self.nesting != Unknown {
307 if let PatKind::Binding(_, id, ..) = pat.kind {
308 if id == self.iterator {
309 self.nesting = RuledOut;
317 /// If `arg` was the argument to a `for` loop, return the "cleanest" way of writing the
318 /// actual `Iterator` that the loop uses.
319 pub(super) fn make_iterator_snippet(cx: &LateContext<'_>, arg: &Expr<'_>, applic_ref: &mut Applicability) -> String {
320 let impls_iterator = cx.tcx.get_diagnostic_item(sym::Iterator).map_or(false, |id| {
321 implements_trait(cx, cx.typeck_results().expr_ty(arg), id, &[])
326 sugg::Sugg::hir_with_applicability(cx, arg, "_", applic_ref).maybe_par()
329 // (&x).into_iter() ==> x.iter()
330 // (&mut x).into_iter() ==> x.iter_mut()
331 let arg_ty = cx.typeck_results().expr_ty_adjusted(arg);
332 match &arg_ty.kind() {
333 ty::Ref(_, inner_ty, mutbl) if has_iter_method(cx, *inner_ty).is_some() => {
334 let method_name = match mutbl {
335 Mutability::Mut => "iter_mut",
336 Mutability::Not => "iter",
338 let caller = match &arg.kind {
339 ExprKind::AddrOf(BorrowKind::Ref, _, arg_inner) => arg_inner,
343 "{}.{method_name}()",
344 sugg::Sugg::hir_with_applicability(cx, caller, "_", applic_ref).maybe_par(),
349 sugg::Sugg::hir_with_applicability(cx, arg, "_", applic_ref).maybe_par()