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
// Unimplemented methods will report having side effects for safety reasons.

use full_moon::ast;

pub trait HasSideEffects {
    fn has_side_effects(&self) -> bool;
}

impl HasSideEffects for ast::Expression {
    fn has_side_effects(&self) -> bool {
        #[cfg_attr(
            feature = "force_exhaustive_checks",
            deny(non_exhaustive_omitted_patterns)
        )]
        match self {
            ast::Expression::BinaryOperator { lhs, rhs, .. } => {
                lhs.has_side_effects() || rhs.has_side_effects()
            }
            ast::Expression::Parentheses { expression, .. }
            | ast::Expression::UnaryOperator { expression, .. } => expression.has_side_effects(),
            ast::Expression::Function(_)
            | ast::Expression::Number(_)
            | ast::Expression::String(_)
            | ast::Expression::Symbol(_) => false,
            ast::Expression::FunctionCall(_) => true,
            ast::Expression::TableConstructor(table_constructor) => table_constructor
                .fields()
                .into_iter()
                .any(|field| match field {
                    ast::Field::ExpressionKey { key, value, .. } => {
                        key.has_side_effects() || value.has_side_effects()
                    }

                    ast::Field::NameKey { value, .. } => value.has_side_effects(),

                    ast::Field::NoKey(expression) => expression.has_side_effects(),

                    _ => true,
                }),
            ast::Expression::Var(var) => var.has_side_effects(),

            #[cfg(feature = "roblox")]
            ast::Expression::IfExpression(if_expression) => {
                if if_expression.if_expression().has_side_effects()
                    || if_expression.condition().has_side_effects()
                    || if_expression.else_expression().has_side_effects()
                {
                    return true;
                }

                if let Some(else_if_expressions) = if_expression.else_if_expressions() {
                    for else_if_expression in else_if_expressions {
                        if else_if_expression.condition().has_side_effects()
                            || else_if_expression.expression().has_side_effects()
                        {
                            return true;
                        }
                    }
                }

                false
            }

            #[cfg(feature = "roblox")]
            ast::Expression::InterpolatedString(interpolated_string) => {
                for expression in interpolated_string.expressions() {
                    if expression.has_side_effects() {
                        return true;
                    }
                }

                false
            }

            #[cfg(feature = "roblox")]
            ast::Expression::TypeAssertion { expression, .. } => expression.has_side_effects(),

            _ => true,
        }
    }
}

impl HasSideEffects for ast::Prefix {
    fn has_side_effects(&self) -> bool {
        #[cfg_attr(
            feature = "force_exhaustive_checks",
            deny(non_exhaustive_omitted_patterns)
        )]
        match self {
            ast::Prefix::Expression(expression) => expression.has_side_effects(),
            ast::Prefix::Name(_) => false,
            _ => true,
        }
    }
}

impl HasSideEffects for ast::Suffix {
    fn has_side_effects(&self) -> bool {
        #[cfg_attr(
            feature = "force_exhaustive_checks",
            deny(non_exhaustive_omitted_patterns)
        )]
        match self {
            ast::Suffix::Call(_) => true,
            ast::Suffix::Index(_) => false,
            _ => true,
        }
    }
}

impl HasSideEffects for ast::Var {
    fn has_side_effects(&self) -> bool {
        #[cfg_attr(
            feature = "force_exhaustive_checks",
            deny(non_exhaustive_omitted_patterns)
        )]
        match self {
            ast::Var::Expression(var_expr) => var_expr.has_side_effects(),
            ast::Var::Name(_) => false,
            _ => true,
        }
    }
}

impl HasSideEffects for ast::VarExpression {
    fn has_side_effects(&self) -> bool {
        self.prefix().has_side_effects() || self.suffixes().any(HasSideEffects::has_side_effects)
    }
}