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
use rustc::lint::*;
use syntax::ast::*;
use syntax::ext::quote::rt::Span;
use utils::span_note_and_lint;
/// **What it does:** Checks for
/// - () being assigned to a variable
/// - () being passed to a function
///
/// **Why is this bad?** It is extremely unlikely that a user intended to
/// assign '()' to valiable. Instead,
/// Unit is what a block evaluates to when it returns nothing. This is
/// typically caused by a trailing
/// unintended semicolon.
///
/// **Known problems:** None.
///
/// **Example:**
/// * `let x = {"foo" ;}` when the user almost certainly intended `let x
/// ={"foo"}`
declare_lint! {
pub UNIT_EXPR,
Warn,
"unintended assignment or use of a unit typed value"
}
#[derive(Copy, Clone)]
pub struct UnitExpr;
impl LintPass for UnitExpr {
fn get_lints(&self) -> LintArray {
lint_array!(UNIT_EXPR)
}
}
impl EarlyLintPass for UnitExpr {
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
if let ExprKind::Assign(ref _left, ref right) = expr.node {
if let Some(span) = is_unit_expr(right) {
span_note_and_lint(
cx,
UNIT_EXPR,
expr.span,
"This expression evaluates to the Unit type ()",
span,
"Consider removing the trailing semicolon",
);
}
}
if let ExprKind::MethodCall(ref _left, ref args) = expr.node {
for arg in args {
if let Some(span) = is_unit_expr(arg) {
span_note_and_lint(
cx,
UNIT_EXPR,
expr.span,
"This expression evaluates to the Unit type ()",
span,
"Consider removing the trailing semicolon",
);
}
}
}
if let ExprKind::Call(_, ref args) = expr.node {
for arg in args {
if let Some(span) = is_unit_expr(arg) {
span_note_and_lint(
cx,
UNIT_EXPR,
expr.span,
"This expression evaluates to the Unit type ()",
span,
"Consider removing the trailing semicolon",
);
}
}
}
}
fn check_stmt(&mut self, cx: &EarlyContext, stmt: &Stmt) {
if let StmtKind::Local(ref local) = stmt.node {
if local.pat.node == PatKind::Wild {
return;
}
if let Some(ref expr) = local.init {
if let Some(span) = is_unit_expr(expr) {
span_note_and_lint(
cx,
UNIT_EXPR,
expr.span,
"This expression evaluates to the Unit type ()",
span,
"Consider removing the trailing semicolon",
);
}
}
}
}
}
fn is_unit_expr(expr: &Expr) -> Option<Span> {
match expr.node {
ExprKind::Block(ref block) => if check_last_stmt_in_block(block) {
Some(block.stmts[block.stmts.len() - 1].span)
} else {
None
},
ExprKind::If(_, ref then, ref else_) => {
let check_then = check_last_stmt_in_block(then);
if let Some(ref else_) = *else_ {
let check_else = is_unit_expr(else_);
if let Some(ref expr_else) = check_else {
return Some(*expr_else);
}
}
if check_then {
Some(expr.span)
} else {
None
}
},
ExprKind::Match(ref _pattern, ref arms) => {
for arm in arms {
if let Some(expr) = is_unit_expr(&arm.body) {
return Some(expr);
}
}
None
},
_ => None,
}
}
fn check_last_stmt_in_block(block: &Block) -> bool {
let final_stmt = &block.stmts[block.stmts.len() - 1];
// Made a choice here to risk false positives on divergent macro invocations
// like `panic!()`
match final_stmt.node {
StmtKind::Expr(_) => false,
StmtKind::Semi(ref expr) => match expr.node {
ExprKind::Break(_, _) | ExprKind::Continue(_) | ExprKind::Ret(_) => false,
_ => true,
},
_ => true,
}
}