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
explain_lint! {
    ///The homogenous assignment lint detects and warns on mixed use of assignment operators.
    ///
    ///For example, the following program uses both the equality and assign-define operators to
    ///assign variables:
    ///
    ///```text
    ///a = 1
    ///b := 1
    ///```
    ///
    ///This can be misleading or confusing, as these two operators are syntactically different (and
    ///semantically different in canonical mathematics notation), but are treated the same in slide.
    ///
    ///For this reason, it is suggested that exclusively `=` or `:=` are used for assignments in
    ///slide programs.
    L0004: HomogenousAssignmentLinter
}

use crate::linter::LintRule;

use crate::diagnostics::Diagnostic;
use crate::grammar::*;
use visit::StmtVisitor;

pub struct HomogenousAssignmentLinter<'a> {
    source: &'a str,
    /// The assignment op kind we expect to see across the program, set to the first assignment op
    /// we see.
    asgn_op: Option<AssignmentOp>,
    diagnostics: Vec<Diagnostic>,
}

impl<'a> HomogenousAssignmentLinter<'a> {
    pub fn new(source: &'a str) -> Self {
        Self {
            source,
            asgn_op: None,
            diagnostics: vec![],
        }
    }
}

impl<'a> StmtVisitor<'a> for HomogenousAssignmentLinter<'a> {
    fn visit_asgn_op(&mut self, asgn_op: &'a AssignmentOp) {
        match (self.asgn_op, asgn_op) {
            (Some(AssignmentOp::Equal(expected)), AssignmentOp::AssignDefine(actual))
            | (Some(AssignmentOp::AssignDefine(expected)), AssignmentOp::Equal(actual)) => {
                self.diagnostics.push(
                    Diagnostic::span_warn(
                        *actual,
                        "Mixed use of assignment operators",
                        Self::CODE,
                        format!(r#"expected "{}" here"#, expected.over(self.source)),
                    )
                    .with_spanned_note(
                        expected,
                        format!(
                            r#"first use of "{}" as an assignment operator here"#,
                            expected.over(self.source),
                        ),
                    ),
                )
            }
            (None, actual) => self.asgn_op = Some(*actual),
            _ => (),
        }
    }
}

impl<'a> LintRule<'a, StmtList> for HomogenousAssignmentLinter<'a> {
    fn lint(stmt_list: &StmtList, source: &'a str) -> Vec<Diagnostic> {
        let mut linter = Self::new(&source);
        linter.visit_stmt_list(stmt_list);
        linter.diagnostics
    }
}