summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/loops/mut_range_bound.rs
blob: fce2d54639cbf22af7ae546a0153ec48793b7978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use super::MUT_RANGE_BOUND;
use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::{get_enclosing_block, higher, path_to_local};
use if_chain::if_chain;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{BindingAnnotation, Expr, ExprKind, HirId, Node, PatKind};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::LateContext;
use rustc_middle::{mir::FakeReadCause, ty};
use rustc_span::source_map::Span;
use rustc_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};

pub(super) fn check(cx: &LateContext<'_>, arg: &Expr<'_>, body: &Expr<'_>) {
    if_chain! {
        if let Some(higher::Range {
            start: Some(start),
            end: Some(end),
            ..
        }) = higher::Range::hir(arg);
        let (mut_id_start, mut_id_end) = (check_for_mutability(cx, start), check_for_mutability(cx, end));
        if mut_id_start.is_some() || mut_id_end.is_some();
        then {
            let (span_low, span_high) = check_for_mutation(cx, body, mut_id_start, mut_id_end);
            mut_warn_with_span(cx, span_low);
            mut_warn_with_span(cx, span_high);
        }
    }
}

fn mut_warn_with_span(cx: &LateContext<'_>, span: Option<Span>) {
    if let Some(sp) = span {
        span_lint_and_note(
            cx,
            MUT_RANGE_BOUND,
            sp,
            "attempt to mutate range bound within loop",
            None,
            "the range of the loop is unchanged",
        );
    }
}

fn check_for_mutability(cx: &LateContext<'_>, bound: &Expr<'_>) -> Option<HirId> {
    if_chain! {
        if let Some(hir_id) = path_to_local(bound);
        if let Node::Pat(pat) = cx.tcx.hir().get(hir_id);
        if let PatKind::Binding(BindingAnnotation::MUT, ..) = pat.kind;
        then {
            return Some(hir_id);
        }
    }
    None
}

fn check_for_mutation<'tcx>(
    cx: &LateContext<'tcx>,
    body: &Expr<'_>,
    bound_id_start: Option<HirId>,
    bound_id_end: Option<HirId>,
) -> (Option<Span>, Option<Span>) {
    let mut delegate = MutatePairDelegate {
        cx,
        hir_id_low: bound_id_start,
        hir_id_high: bound_id_end,
        span_low: None,
        span_high: None,
    };
    cx.tcx.infer_ctxt().enter(|infcx| {
        ExprUseVisitor::new(
            &mut delegate,
            &infcx,
            body.hir_id.owner,
            cx.param_env,
            cx.typeck_results(),
        )
        .walk_expr(body);
    });

    delegate.mutation_span()
}

struct MutatePairDelegate<'a, 'tcx> {
    cx: &'a LateContext<'tcx>,
    hir_id_low: Option<HirId>,
    hir_id_high: Option<HirId>,
    span_low: Option<Span>,
    span_high: Option<Span>,
}

impl<'tcx> Delegate<'tcx> for MutatePairDelegate<'_, 'tcx> {
    fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}

    fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, diag_expr_id: HirId, bk: ty::BorrowKind) {
        if bk == ty::BorrowKind::MutBorrow {
            if let PlaceBase::Local(id) = cmt.place.base {
                if Some(id) == self.hir_id_low && !BreakAfterExprVisitor::is_found(self.cx, diag_expr_id) {
                    self.span_low = Some(self.cx.tcx.hir().span(diag_expr_id));
                }
                if Some(id) == self.hir_id_high && !BreakAfterExprVisitor::is_found(self.cx, diag_expr_id) {
                    self.span_high = Some(self.cx.tcx.hir().span(diag_expr_id));
                }
            }
        }
    }

    fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>, diag_expr_id: HirId) {
        if let PlaceBase::Local(id) = cmt.place.base {
            if Some(id) == self.hir_id_low && !BreakAfterExprVisitor::is_found(self.cx, diag_expr_id) {
                self.span_low = Some(self.cx.tcx.hir().span(diag_expr_id));
            }
            if Some(id) == self.hir_id_high && !BreakAfterExprVisitor::is_found(self.cx, diag_expr_id) {
                self.span_high = Some(self.cx.tcx.hir().span(diag_expr_id));
            }
        }
    }

    fn fake_read(&mut self, _: &rustc_typeck::expr_use_visitor::PlaceWithHirId<'tcx>, _: FakeReadCause, _: HirId) {}
}

impl MutatePairDelegate<'_, '_> {
    fn mutation_span(&self) -> (Option<Span>, Option<Span>) {
        (self.span_low, self.span_high)
    }
}

struct BreakAfterExprVisitor {
    hir_id: HirId,
    past_expr: bool,
    past_candidate: bool,
    break_after_expr: bool,
}

impl BreakAfterExprVisitor {
    pub fn is_found(cx: &LateContext<'_>, hir_id: HirId) -> bool {
        let mut visitor = BreakAfterExprVisitor {
            hir_id,
            past_expr: false,
            past_candidate: false,
            break_after_expr: false,
        };

        get_enclosing_block(cx, hir_id).map_or(false, |block| {
            visitor.visit_block(block);
            visitor.break_after_expr
        })
    }
}

impl<'tcx> intravisit::Visitor<'tcx> for BreakAfterExprVisitor {
    fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
        if self.past_candidate {
            return;
        }

        if expr.hir_id == self.hir_id {
            self.past_expr = true;
        } else if self.past_expr {
            if matches!(&expr.kind, ExprKind::Break(..)) {
                self.break_after_expr = true;
            }

            self.past_candidate = true;
        } else {
            intravisit::walk_expr(self, expr);
        }
    }
}