valua-lint 0.1.0

Static analysis lints for the valua transpiler. Public crate; stable from 1.0.
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::collections::HashMap;

use valua_ast::{
    Assign, Attribute, Block, Call, Expression, FunctionBody, LocalDecl, Statement, TableField,
};
use valua_diagnostics::{Diagnostic, Span};

use crate::Lint;

/// E0301 — detects reassignment of `<const>`-attributed local variables.
pub struct ConstMutation;

impl Lint for ConstMutation {
    fn check(&self, block: &Block) -> Vec<Diagnostic> {
        let mut diags = Vec::new();
        let mut scopes: Vec<ScopeFrame> = Vec::new();
        check_block(block, &mut scopes, &mut diags);
        diags
    }
}

// `Some(span)` → name is `<const>`, declared at `span`.
// `None`       → name is in scope but NOT const (shadows any outer const).
type ScopeFrame = HashMap<String, Option<Span>>;

fn check_block(block: &Block, scopes: &mut Vec<ScopeFrame>, diags: &mut Vec<Diagnostic>) {
    scopes.push(HashMap::new());
    for stmt in &block.stmts {
        check_stmt(stmt, scopes, diags);
    }
    scopes.pop();
}

fn check_function_body(
    func: &FunctionBody,
    scopes: &mut Vec<ScopeFrame>,
    diags: &mut Vec<Diagnostic>,
) {
    scopes.push(HashMap::new());
    // Parameters shadow any outer const of the same name.
    if let Some(scope) = scopes.last_mut() {
        for param in &func.params {
            scope.insert(param.name.clone(), None);
        }
    }
    for stmt in &func.body.stmts {
        check_stmt(stmt, scopes, diags);
    }
    scopes.pop();
}

fn check_stmt(stmt: &Statement, scopes: &mut Vec<ScopeFrame>, diags: &mut Vec<Diagnostic>) {
    match stmt {
        Statement::LocalDecl(decl) => {
            // Walk RHS expressions for closures BEFORE registering names so that
            // `local x <const> = 1; local f = function() x = 2 end` can see outer x.
            for val in &decl.values {
                check_expr_closures(val, scopes, diags);
            }
            register_locals(decl, scopes);
        }
        Statement::Assign(assign) => {
            for val in &assign.values {
                check_expr_closures(val, scopes, diags);
            }
            check_assign(assign, scopes, diags);
        }
        Statement::ExprStmt(e) => check_expr_closures(e, scopes, diags),
        Statement::Return(r) => {
            for val in &r.values {
                check_expr_closures(val, scopes, diags);
            }
        }
        Statement::Do(b) => check_block(&b.body, scopes, diags),
        Statement::While(w) => {
            check_expr_closures(&w.condition, scopes, diags);
            check_block(&w.body, scopes, diags);
        }
        Statement::Repeat(r) => {
            check_block(&r.body, scopes, diags);
            check_expr_closures(&r.condition, scopes, diags);
        }
        Statement::If(i) => {
            check_expr_closures(&i.condition, scopes, diags);
            check_block(&i.then_block, scopes, diags);
            for elseif in &i.elseif_clauses {
                check_expr_closures(&elseif.condition, scopes, diags);
                check_block(&elseif.body, scopes, diags);
            }
            if let Some(else_block) = &i.else_block {
                check_block(else_block, scopes, diags);
            }
        }
        Statement::NumericFor(f) => {
            check_expr_closures(&f.start, scopes, diags);
            check_expr_closures(&f.limit, scopes, diags);
            if let Some(step) = &f.step {
                check_expr_closures(step, scopes, diags);
            }
            check_block(&f.body, scopes, diags);
        }
        Statement::GenericFor(f) => {
            for iter in &f.iterators {
                check_expr_closures(iter, scopes, diags);
            }
            check_block(&f.body, scopes, diags);
        }
        Statement::FunctionDecl(f) => check_function_body(&f.func, scopes, diags),
        Statement::LocalFunctionDecl(f) => check_function_body(&f.func, scopes, diags),
        _ => {}
    }
}

fn check_expr_closures(
    expr: &Expression,
    scopes: &mut Vec<ScopeFrame>,
    diags: &mut Vec<Diagnostic>,
) {
    match expr {
        Expression::Function(f) => check_function_body(f, scopes, diags),
        Expression::Call(call) => match call {
            Call::Call { func, args, .. } => {
                check_expr_closures(func, scopes, diags);
                for arg in args {
                    check_expr_closures(arg, scopes, diags);
                }
            }
            Call::MethodCall { obj, args, .. } => {
                check_expr_closures(obj, scopes, diags);
                for arg in args {
                    check_expr_closures(arg, scopes, diags);
                }
            }
        },
        Expression::Table(t) => {
            for field in &t.fields {
                match field {
                    TableField::ExprKey { key, value, .. } => {
                        check_expr_closures(key, scopes, diags);
                        check_expr_closures(value, scopes, diags);
                    }
                    TableField::NameKey { value, .. } => {
                        check_expr_closures(value, scopes, diags);
                    }
                    TableField::Positional(value) => {
                        check_expr_closures(value, scopes, diags);
                    }
                }
            }
        }
        Expression::BinOp(lhs, _, rhs, _) => {
            check_expr_closures(lhs, scopes, diags);
            check_expr_closures(rhs, scopes, diags);
        }
        Expression::UnOp(_, operand, _) => check_expr_closures(operand, scopes, diags),
        Expression::Index(base, _, _) => check_expr_closures(base, scopes, diags),
        Expression::IndexExpr(base, key, _) => {
            check_expr_closures(base, scopes, diags);
            check_expr_closures(key, scopes, diags);
        }
        _ => {}
    }
}

fn register_locals(decl: &LocalDecl, scopes: &mut [ScopeFrame]) {
    if let Some(scope) = scopes.last_mut() {
        for name in &decl.names {
            let entry = if name.attribute == Some(Attribute::Const) {
                Some(name.span)
            } else {
                None
            };
            scope.insert(name.name.clone(), entry);
        }
    }
}

fn check_assign(assign: &Assign, scopes: &[ScopeFrame], diags: &mut Vec<Diagnostic>) {
    for target in &assign.targets {
        if let Expression::Name(name, mutation_span) = target {
            if let Some(decl_span) = find_const(scopes, name) {
                diags.push(
                    Diagnostic::error(
                        format!("assignment to const variable `{name}`"),
                        *mutation_span,
                    )
                    .with_code("E0301")
                    .with_secondary_label(decl_span, "declared as const here")
                    .with_note(format!(
                        "`{name}` was declared with <const>; Lua 5.4 treats this as a compile-time constant"
                    ))
                    .with_suggestion(
                        "Remove the assignment or remove the <const> attribute from the declaration",
                    ),
                );
            }
        }
    }
}

fn find_const(scopes: &[ScopeFrame], name: &str) -> Option<Span> {
    for scope in scopes.iter().rev() {
        match scope.get(name) {
            Some(Some(span)) => return Some(*span),
            Some(None) => return None, // inner non-const shadows outer const
            None => {}
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use valua_diagnostics::{CollectingReporter, Reporter};

    use super::ConstMutation;
    use crate::Lint;

    fn parse(src: &str) -> valua_ast::Block {
        valua_parser::parse(src).expect("parse failed")
    }

    fn diags(src: &str) -> Vec<valua_diagnostics::Diagnostic> {
        ConstMutation.check(&parse(src))
    }

    fn codes(src: &str) -> Vec<&'static str> {
        diags(src).into_iter().filter_map(|d| d.code).collect()
    }

    // ── Basic detection ──────────────────────────────────────────────────────

    #[test]
    fn cm_detects_simple_mutation() {
        assert_eq!(codes("local x <const> = 1\nx = 2"), vec!["E0301"]);
    }

    #[test]
    fn cm_no_false_positive_plain_local() {
        assert!(codes("local x = 1\nx = 2").is_empty());
    }

    #[test]
    fn cm_no_false_positive_read_only() {
        assert!(codes("local x <const> = 1\nlocal y = x + 1").is_empty());
    }

    #[test]
    fn cm_multiple_mutations_same_const() {
        let result = codes("local x <const> = 1\nx = 2\nx = 3");
        assert_eq!(result, vec!["E0301", "E0301"]);
    }

    // ── Scope nesting ────────────────────────────────────────────────────────

    #[test]
    fn cm_mutation_in_do_block() {
        assert_eq!(
            codes("local x <const> = 1\ndo\n  x = 2\nend"),
            vec!["E0301"]
        );
    }

    #[test]
    fn cm_inner_non_const_shadows_outer_const() {
        // Bug 1 regression: inner `local x` must shadow outer `<const> x` → 0 diags
        assert!(codes("local x <const> = 1\ndo\n  local x = 2\n  x = 3\nend").is_empty());
    }

    #[test]
    fn cm_shadow_ends_at_block_boundary() {
        // After `do...end`, outer const is visible again → 1 diag for `x = 4`
        let result = codes("local x <const> = 1\ndo\n  local x = 2\n  x = 3\nend\nx = 4");
        assert_eq!(result, vec!["E0301"]);
    }

    #[test]
    fn cm_deeply_nested_do_blocks_detect_mutation() {
        let src = "local x <const> = 1\ndo\n  do\n    do\n      x = 2\n    end\n  end\nend";
        assert_eq!(codes(src), vec!["E0301"]);
    }

    #[test]
    fn cm_deeply_nested_shadow_stops_at_boundary() {
        // Non-const shadow at innermost level; after block exits, outer const is back
        let src = "local x <const> = 1\ndo\n  do\n    local x = 2\n    x = 3\n  end\n  x = 4\nend";
        // `x = 3` is shadowed → no diag; `x = 4` sees outer const → E0301
        assert_eq!(codes(src), vec!["E0301"]);
    }

    // ── Closures in values ────────────────────────────────────────────────────

    #[test]
    fn cm_mutation_in_anonymous_function_value() {
        // Bug 2 regression: closure captures outer const → should detect mutation
        let src = "local x <const> = 1\nlocal f = function() x = 2 end";
        assert_eq!(codes(src), vec!["E0301"]);
    }

    #[test]
    fn cm_mutation_in_named_function_decl() {
        let src = "local x <const> = 1\nfunction foo() x = 2 end";
        assert_eq!(codes(src), vec!["E0301"]);
    }

    #[test]
    fn cm_mutation_in_local_function_decl() {
        let src = "local x <const> = 1\nlocal function foo() x = 2 end";
        assert_eq!(codes(src), vec!["E0301"]);
    }

    // ── Parameter shadows ────────────────────────────────────────────────────

    #[test]
    fn cm_function_param_shadows_outer_const() {
        // Bug 3 regression: param `x` shadows outer const `x` → 0 diags inside body
        let src = "local x <const> = 1\nlocal function f(x) x = 2 end";
        assert!(codes(src).is_empty());
    }

    #[test]
    fn cm_function_param_does_not_affect_outer_scope() {
        // Outer const still visible and flagged after function declaration
        let src = "local x <const> = 1\nlocal function f(x) x = 2 end\nx = 3";
        assert_eq!(codes(src), vec!["E0301"]);
    }

    // ── Close attribute ──────────────────────────────────────────────────────

    #[test]
    fn cm_close_attribute_not_flagged() {
        // `<close>` is NOT `<const>` — mutations are allowed
        let src = "local x <close> = io.open('f')\nx = nil";
        assert!(codes(src).is_empty());
    }

    // ── Span verification ────────────────────────────────────────────────────

    #[test]
    fn cm_mutation_span_on_correct_line() {
        let src = "local x <const> = 1\nx = 2";
        let diagnostics = diags(src);
        assert_eq!(diagnostics.len(), 1);
        let d = &diagnostics[0];
        assert_eq!(d.code, Some("E0301"));
        assert_eq!(d.span.line, 2, "mutation span should be on line 2");
    }

    #[test]
    fn cm_secondary_label_on_declaration_line() {
        let src = "local x <const> = 1\nx = 2";
        let diagnostics = diags(src);
        assert_eq!(diagnostics.len(), 1);
        let d = &diagnostics[0];
        assert_eq!(d.secondary_labels.len(), 1);
        assert_eq!(
            d.secondary_labels[0].0.line, 1,
            "secondary label should point to line 1 (declaration)"
        );
    }

    // ── If/while/repeat/for bodies ───────────────────────────────────────────

    #[test]
    fn cm_mutation_in_while_body() {
        let src = "local x <const> = 1\nwhile true do\n  x = 2\nend";
        assert_eq!(codes(src), vec!["E0301"]);
    }

    #[test]
    fn cm_mutation_in_if_then() {
        let src = "local x <const> = 1\nif true then\n  x = 2\nend";
        assert_eq!(codes(src), vec!["E0301"]);
    }

    #[test]
    fn cm_mutation_in_numeric_for_body() {
        let src = "local x <const> = 1\nfor i = 1, 10 do\n  x = 2\nend";
        assert_eq!(codes(src), vec!["E0301"]);
    }

    // ── CollectingReporter integration ───────────────────────────────────────

    #[test]
    fn cm_collecting_reporter_captures_e0301() {
        let src = "local x <const> = 1\nx = 2";
        let diagnostics = ConstMutation.check(&parse(src));
        let mut reporter = CollectingReporter::default();
        for d in &diagnostics {
            reporter.report(d, src, "test.lua");
        }
        assert!(reporter.has_errors());
        assert_eq!(reporter.diagnostics.len(), 1);
        assert_eq!(reporter.diagnostics[0].code, Some("E0301"));
    }

    #[test]
    fn cm_collecting_reporter_clean_code() {
        let src = "local x <const> = 1\nreturn x";
        let diagnostics = ConstMutation.check(&parse(src));
        let mut reporter = CollectingReporter::default();
        for d in &diagnostics {
            reporter.report(d, src, "test.lua");
        }
        assert!(!reporter.has_errors());
    }
}