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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use crate::rule_prelude::*;
use ast::*;
use SyntaxKind::*;

declare_lint! {
    /**
    Disallow for loops which update their counter in the wrong direction.

    A for loop with a counter may update its value in the wrong direction. that is to say, if i made
    a counter with a value of `0`, if the for statement checked if `counter < 10` and the update went `counter--`,
    that loop would be infinite. This is because `counter` will never be smaller than `10` because `counter--` always
    yields a value smaller than 10. A for loop which does this is almost always a bug because it is either
    unreachable or infinite.

    ## Incorrect Code Examples

    ```js
    for (var i = 0; i < 10; i--) {
        /* infinite loop */
    }
    ```

    ```js
    for (var i = 10; i >= 20; i++) {
        /* unreachable */
    }
    ```

    ## Correct Code Examples

    ```js
    for (var i = 0; i < 10; i++) {

    }
    ```
    */
    #[derive(Default)]
    ForDirection,
    errors,
    "for-direction"
}

#[typetag::serde]
impl CstRule for ForDirection {
    fn check_node(&self, node: &SyntaxNode, ctx: &mut RuleCtx) -> Option<()> {
        if let Some(test) = node
            .try_to::<ForStmt>()
            .and_then(|f| f.test())
            .and_then(|test| test.expr())
        {
            let for_stmt = node.to::<ForStmt>();
            if for_stmt.update().is_some()
                && test.syntax().try_to::<BinExpr>()?.lhs()?.syntax().kind() == NAME_REF
            {
                let test_bin = test.syntax().to::<BinExpr>();
                if test_bin.rhs().is_none() || for_stmt.init().is_none() {
                    return None;
                }

                let counter = test_bin.lhs().unwrap().syntax().to::<NameRef>();
                let op = test_bin.op()?;

                let wrong_direction = if op == BinOp::LessThan || op == BinOp::LessThanOrEqual {
                    -1
                } else if op == BinOp::GreaterThan || op == BinOp::GreaterThanOrEqual {
                    1
                } else {
                    return None;
                };

                if let Some(direction) = update_direction(&for_stmt, &counter) {
                    if direction == wrong_direction {
                        throw_err(for_stmt, &counter, ctx);
                    }
                }
            }
        }
        None
    }
}

fn update_direction(for_stmt: &ForStmt, counter: &NameRef) -> Option<i8> {
    let update = for_stmt.update()?.syntax().first_child()?;
    match update.kind() {
        UNARY_EXPR => {
            let expr = update.to::<UnaryExpr>();
            if expr.expr()?.syntax().try_to::<NameRef>()?.syntax().text() == counter.syntax().text()
            {
                let op = expr.op().unwrap();
                Some(if op == UnaryOp::Increment { 1 } else { -1 })
            } else {
                None
            }
        }
        ASSIGN_EXPR => assign_direction(update.to(), counter),
        _ => None,
    }
}

fn assign_direction(assign: AssignExpr, counter: &NameRef) -> Option<i8> {
    if assign.lhs()?.syntax().text() == counter.syntax().text() {
        match assign.op()? {
            AssignOp::AddAssign => maybe_negate_direction(assign.rhs()?, 1),
            AssignOp::SubtractAssign => maybe_negate_direction(assign.rhs()?, -1),
            _ => Some(0),
        }
    } else {
        None
    }
}

fn maybe_negate_direction(rhs: Expr, direction: i8) -> Option<i8> {
    Some(match rhs {
        Expr::UnaryExpr(unexpr) => {
            if unexpr.op()? == UnaryOp::Minus {
                -direction
            } else {
                direction
            }
        }
        Expr::NameRef(_) => 0,
        _ => direction,
    })
}

// TODO: we can say if the loop is unreachable once we have number parsing
fn throw_err(for_stmt: ForStmt, counter: &NameRef, ctx: &mut RuleCtx) {
    let bin = for_stmt
        .test()
        .unwrap()
        .syntax()
        .first_child()
        .unwrap()
        .to::<BinExpr>();
    let lhs = bin.lhs().unwrap().syntax().trimmed_text();
    let rhs = bin.rhs().unwrap().syntax().clone();
    let op = bin.op().unwrap();

    if let Some(lit) = rhs
        .try_to::<Literal>()
        .filter(|literal| literal.is_number())
    {
        if try_offer_context(&for_stmt, counter, op, lit, ctx).is_some() {
            return;
        }
    }

    let err = ctx
        .err(
            "for-direction",
            "For loop is updating the counter in the wrong direction",
        )
        .secondary(
            for_stmt.test().unwrap().range(),
            format!(
                "this test is checking if `{}` is {} `{}`...",
                lhs,
                lt_gt_name(op),
                rhs
            ),
        )
        .primary(
            for_stmt.update().unwrap().range(),
            format!(
                "...but `{}` is updating in the same direction",
                for_stmt.update().unwrap().syntax().trimmed_text()
            ),
        );

    ctx.add_err(err);
}

fn lt_gt_name(op: BinOp) -> &'static str {
    match op {
        BinOp::LessThan => "less than",
        BinOp::LessThanOrEqual => "less than or equal to",
        BinOp::GreaterThan => "greater than",
        BinOp::GreaterThanOrEqual => "greater than or equal to",
        _ => unreachable!(),
    }
}

/// try to offer even more context around the error if we know the initial numeric value of the counter
fn try_offer_context(
    for_stmt: &ForStmt,
    counter: &NameRef,
    op: BinOp,
    checked_value: Literal,
    ctx: &mut RuleCtx,
) -> Option<()> {
    let init = for_stmt.init()?;

    let initial_value = match init.inner().unwrap() {
        ForHead::Decl(decl) => {
            let decl = decl.declared().find(|declarator| {
                declarator.pattern().map_or(false, |pat| {
                    if let Pattern::SinglePattern(single) = pat {
                        single.syntax().text() == counter.syntax().text()
                    } else {
                        false
                    }
                })
            })?;
            decl.value()?
        }
        ForHead::Expr(Expr::AssignExpr(assign)) => {
            assign.lhs().and_then(|lhs| {
                if let PatternOrExpr::Expr(Expr::NameRef(name)) = lhs {
                    Some(name).filter(|name| name.syntax().text() == counter.syntax().text())
                } else {
                    None
                }
            })?;
            assign.rhs()?
        }
        _ => return None,
    };

    let mut err = ctx.err(
        "for-direction",
        "For loop is updating the counter in the wrong direction",
    );

    if let Some(LiteralKind::Number(num)) = initial_value
        .syntax()
        .try_to::<Literal>()
        .map(|lit| lit.kind())
    {
        if is_initially_unreachable(num, checked_value.as_number().unwrap(), op) {
            err = err
                .secondary(
                    init.syntax().trimmed_range(),
                    format!(
                        "{} is first declared as `{}`...",
                        counter.syntax().text(),
                        initial_value.syntax().text()
                    ),
                )
                .secondary(
                    for_stmt.test().unwrap().range(),
                    format!(
                        "...which makes this test unreachable because `{}` is not {} `{}`...",
                        initial_value.syntax().text(),
                        lt_gt_name(op),
                        checked_value.syntax().text()
                    ),
                )
                .primary(
                    for_stmt.update().unwrap().range(),
                    "...and this update will never make it true",
                );
        } else {
            err = err
                .secondary(
                    init.syntax(),
                    format!(
                        "{} is first declared as `{}`...",
                        counter.syntax().text(),
                        initial_value.syntax().text()
                    ),
                )
                .secondary(
                    for_stmt.test().unwrap().range(),
                    format!(
                        "...which makes this test always true because `{}` is always {} `{}`...",
                        initial_value.syntax().text(),
                        lt_gt_name(op),
                        checked_value.syntax().text()
                    ),
                )
                .primary(
                    for_stmt.update().unwrap().range(),
                    "...and this update will never make the condition false",
                );
        }
        ctx.add_err(err);
        return Some(());
    }
    None
}

fn is_initially_unreachable(initial_value: f64, checked_value: f64, op: BinOp) -> bool {
    match op {
        BinOp::LessThan => initial_value >= checked_value,
        BinOp::LessThanOrEqual => initial_value > checked_value,
        BinOp::GreaterThan => initial_value <= checked_value,
        BinOp::GreaterThanOrEqual => initial_value < checked_value,
        _ => unreachable!(),
    }
}

rule_tests! {
    ForDirection::default(),
    err: {
        "for (var i = 0; i < 10; i--) {}",
        "for(let i = 0; i < 2; i--) {}",
        "for(let i = 0; i <= 2; i += -1) {}",
        "for(let i = 2; i >= 0; i -= -1) {}",
        "for(let i = 0; i < 2; i -= 1) {}",
        "for(let i = 2; i > 2; i++) {}",
        "for(let i = 2; i > 2; i += 1) {}",
        "for(let i = 5n; i < 2; i--) {}"
    },
    ok: {
        "for (var i = 0; i < 10; i++) {}",
        "for(let i = 2; i > 2; i -= 1) {}",
        "for(let i = 2; i >= 0; i -= 1) {}",
        "for(let i = 2; i > 2; i += -1) {}",
        "for(let i = 2; i >= 0; i += -1) {}",
        "for(let i = 0; i < 3;) {}",
        "for(let i = 5; i < 2; i |= 2) {}",
        "for(let i = 5n; i < 2n; i &= 2) {}"
    }
}