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
use super::*;
use std::convert::Infallible;
use full_moon::{
ast::{self, punctuated::Punctuated, Ast},
node::Node,
tokenizer::{Symbol, TokenType},
visitors::Visitor,
};
pub struct UnbalancedAssignmentsLint;
impl Rule for UnbalancedAssignmentsLint {
type Config = ();
type Error = Infallible;
fn new(_: Self::Config) -> Result<Self, Self::Error> {
Ok(UnbalancedAssignmentsLint)
}
fn pass(&self, ast: &Ast, _: &Context) -> Vec<Diagnostic> {
let mut visitor = UnbalancedAssignmentsVisitor {
assignments: Vec::new(),
};
visitor.visit_ast(&ast);
visitor
.assignments
.drain(..)
.map(|assignment| {
if assignment.more {
Diagnostic::new(
"unbalanced_assignments",
"too many values on the right side of the assignment".to_owned(),
Label::new(assignment.range),
)
} else {
let secondary_labels = match assignment.first_call {
Some(range) => vec![Label::new_with_message(
range,
"help: if this function returns more than one value, \
the only first return value is actually used"
.to_owned(),
)],
None => Vec::new(),
};
Diagnostic::new_complete(
"unbalanced_assignments",
"values on right side don't match up to the left side of the assignment"
.to_owned(),
Label::new(assignment.range),
Vec::new(),
secondary_labels,
)
}
})
.collect()
}
fn severity(&self) -> Severity {
Severity::Warning
}
fn rule_type(&self) -> RuleType {
RuleType::Complexity
}
}
struct UnbalancedAssignmentsVisitor {
assignments: Vec<UnbalancedAssignment>,
}
fn expression_is_call(expression: &ast::Expression) -> bool {
match expression {
ast::Expression::Parentheses { expression, .. } => expression_is_call(expression),
ast::Expression::Value { value, binop } => {
if binop.is_none() {
if let ast::Value::FunctionCall(_) = &**value {
true
} else {
false
}
} else {
false
}
}
_ => false,
}
}
fn expression_is_nil(expression: &ast::Expression) -> bool {
match expression {
ast::Expression::Parentheses { expression, .. } => expression_is_call(expression),
ast::Expression::Value { value, binop } => {
if binop.is_none() {
if let ast::Value::Symbol(symbol) = &**value {
*symbol.token_type()
== TokenType::Symbol {
symbol: Symbol::Nil,
}
} else {
false
}
} else {
false
}
}
_ => false,
}
}
fn range<N: Node>(node: N) -> (u32, u32) {
let (start, end) = node.range().unwrap();
(start.bytes() as u32, end.bytes() as u32)
}
impl UnbalancedAssignmentsVisitor {
fn lint_assignment(&mut self, lhs: usize, rhs: &Punctuated<ast::Expression>) {
if rhs.is_empty() {
return;
}
let last_rhs = rhs.iter().last().unwrap();
if rhs.len() > lhs {
self.assignments.push(UnbalancedAssignment {
more: true,
range: (
rhs.iter()
.nth(lhs)
.unwrap()
.start_position()
.unwrap()
.bytes() as u32,
last_rhs.end_position().unwrap().bytes() as u32,
),
..UnbalancedAssignment::default()
});
} else if rhs.len() < lhs && !expression_is_call(last_rhs) && !expression_is_nil(last_rhs) {
self.assignments.push(UnbalancedAssignment {
first_call: rhs.iter().find(|e| expression_is_call(e)).map(range),
range: range(rhs),
..UnbalancedAssignment::default()
});
}
}
}
impl Visitor<'_> for UnbalancedAssignmentsVisitor {
fn visit_assignment(&mut self, assignment: &ast::Assignment) {
self.lint_assignment(assignment.var_list().len(), assignment.expr_list());
}
fn visit_local_assignment(&mut self, assignment: &ast::LocalAssignment) {
self.lint_assignment(assignment.name_list().len(), assignment.expr_list());
}
}
#[derive(Clone, Copy, Default)]
struct UnbalancedAssignment {
first_call: Option<(u32, u32)>,
more: bool,
range: (u32, u32),
}
#[cfg(test)]
mod tests {
use super::{super::test_util::test_lint, *};
#[test]
fn test_unbalanced_assignments() {
test_lint(
UnbalancedAssignmentsLint::new(()).unwrap(),
"unbalanced_assignments",
"unbalanced_assignments",
);
}
}