Skip to content

26 files changed

+183
-231
lines changed
 

‎clippy_lints/src/attrs.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,9 @@ fn is_relevant_trait(tcx: TyCtxt<'_, '_, '_>, item: &TraitItem) -> bool {
376376
fn is_relevant_block(tcx: TyCtxt<'_, '_, '_>, tables: &ty::TypeckTables<'_>, block: &Block) -> bool {
377377
if let Some(stmt) = block.stmts.first() {
378378
match &stmt.node {
379-
StmtKind::Decl(_, _) => true,
380-
StmtKind::Expr(expr, _) | StmtKind::Semi(expr, _) => is_relevant_expr(tcx, tables, expr),
379+
StmtKind::Local(_) => true,
380+
StmtKind::Expr(expr) | StmtKind::Semi(expr) => is_relevant_expr(tcx, tables, expr),
381+
_ => false,
381382
}
382383
} else {
383384
block.expr.as_ref().map_or(false, |e| is_relevant_expr(tcx, tables, e))

‎clippy_lints/src/consts.rs

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::convert::TryInto;
1414
use std::hash::{Hash, Hasher};
1515
use syntax::ast::{FloatTy, LitKind};
1616
use syntax::ptr::P;
17+
use syntax_pos::symbol::Symbol;
1718

1819
/// A `LitKind`-like enum to fold constant `Expr`s into.
1920
#[derive(Debug, Clone)]
@@ -38,6 +39,8 @@ pub enum Constant {
3839
Repeat(Box<Constant>, u64),
3940
/// a tuple of constants
4041
Tuple(Vec<Constant>),
42+
/// a literal with syntax error
43+
Err(Symbol),
4144
}
4245

4346
impl PartialEq for Constant {
@@ -103,6 +106,9 @@ impl Hash for Constant {
103106
c.hash(state);
104107
l.hash(state);
105108
},
109+
Constant::Err(ref s) => {
110+
s.hash(state);
111+
},
106112
}
107113
}
108114
}
@@ -155,6 +161,7 @@ pub fn lit_to_constant<'tcx>(lit: &LitKind, ty: Ty<'tcx>) -> Constant {
155161
_ => bug!(),
156162
},
157163
LitKind::Bool(b) => Constant::Bool(b),
164+
LitKind::Err(s) => Constant::Err(s),
158165
}
159166
}
160167

‎clippy_lints/src/escape.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,16 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
120120
if let Categorization::Rvalue(..) = cmt.cat {
121121
let id = map.hir_to_node_id(cmt.hir_id);
122122
if let Some(Node::Stmt(st)) = map.find(map.get_parent_node(id)) {
123-
if let StmtKind::Decl(ref decl, _) = st.node {
124-
if let DeclKind::Local(ref loc) = decl.node {
125-
if let Some(ref ex) = loc.init {
126-
if let ExprKind::Box(..) = ex.node {
127-
if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
128-
// let x = box (...)
129-
self.set.insert(consume_pat.id);
130-
}
131-
// TODO Box::new
132-
// TODO vec![]
133-
// TODO "foo".to_owned() and friends
123+
if let StmtKind::Local(ref loc) = st.node {
124+
if let Some(ref ex) = loc.init {
125+
if let ExprKind::Box(..) = ex.node {
126+
if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
127+
// let x = box (...)
128+
self.set.insert(consume_pat.id);
134129
}
130+
// TODO Box::new
131+
// TODO vec![]
132+
// TODO "foo".to_owned() and friends
135133
}
136134
}
137135
}

‎clippy_lints/src/eval_order_dependence.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EvalOrderDependence {
8989
}
9090
fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx Stmt) {
9191
match stmt.node {
92-
StmtKind::Expr(ref e, _) | StmtKind::Semi(ref e, _) => DivergenceVisitor { cx }.maybe_walk_expr(e),
93-
StmtKind::Decl(ref d, _) => {
94-
if let DeclKind::Local(ref local) = d.node {
95-
if let Local { init: Some(ref e), .. } = **local {
96-
DivergenceVisitor { cx }.visit_expr(e);
97-
}
92+
StmtKind::Local(ref local) => {
93+
if let Local { init: Some(ref e), .. } = **local {
94+
DivergenceVisitor { cx }.visit_expr(e);
9895
}
9996
},
97+
StmtKind::Expr(ref e) | StmtKind::Semi(ref e) => DivergenceVisitor { cx }.maybe_walk_expr(e),
98+
StmtKind::Item(..) => {},
10099
}
101100
}
102101
}
@@ -269,18 +268,14 @@ fn check_expr<'a, 'tcx>(vis: &mut ReadVisitor<'a, 'tcx>, expr: &'tcx Expr) -> St
269268

270269
fn check_stmt<'a, 'tcx>(vis: &mut ReadVisitor<'a, 'tcx>, stmt: &'tcx Stmt) -> StopEarly {
271270
match stmt.node {
272-
StmtKind::Expr(ref expr, _) | StmtKind::Semi(ref expr, _) => check_expr(vis, expr),
273-
StmtKind::Decl(ref decl, _) => {
274-
// If the declaration is of a local variable, check its initializer
275-
// expression if it has one. Otherwise, keep going.
276-
let local = match decl.node {
277-
DeclKind::Local(ref local) => Some(local),
278-
_ => None,
279-
};
280-
local
281-
.and_then(|local| local.init.as_ref())
282-
.map_or(StopEarly::KeepGoing, |expr| check_expr(vis, expr))
283-
},
271+
StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => check_expr(vis, expr),
272+
// If the declaration is of a local variable, check its initializer
273+
// expression if it has one. Otherwise, keep going.
274+
StmtKind::Local(ref local) => local
275+
.init
276+
.as_ref()
277+
.map_or(StopEarly::KeepGoing, |expr| check_expr(vis, expr)),
278+
_ => StopEarly::KeepGoing,
284279
}
285280
}
286281

‎clippy_lints/src/let_if_seq.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
6868
while let Some(stmt) = it.next() {
6969
if_chain! {
7070
if let Some(expr) = it.peek();
71-
if let hir::StmtKind::Decl(ref decl, _) = stmt.node;
72-
if let hir::DeclKind::Local(ref decl) = decl.node;
73-
if let hir::PatKind::Binding(mode, canonical_id, ident, None) = decl.pat.node;
74-
if let hir::StmtKind::Expr(ref if_, _) = expr.node;
71+
if let hir::StmtKind::Local(ref local) = stmt.node;
72+
if let hir::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.node;
73+
if let hir::StmtKind::Expr(ref if_) = expr.node;
7574
if let hir::ExprKind::If(ref cond, ref then, ref else_) = if_.node;
7675
if !used_in_expr(cx, canonical_id, cond);
7776
if let hir::ExprKind::Block(ref then, _) = then.node;
@@ -84,15 +83,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
8483
if let hir::ExprKind::Block(ref else_, _) = else_.node {
8584
if let Some(default) = check_assign(cx, canonical_id, else_) {
8685
(else_.stmts.len() > 1, default)
87-
} else if let Some(ref default) = decl.init {
86+
} else if let Some(ref default) = local.init {
8887
(true, &**default)
8988
} else {
9089
continue;
9190
}
9291
} else {
9392
continue;
9493
}
95-
} else if let Some(ref default) = decl.init {
94+
} else if let Some(ref default) = local.init {
9695
(false, &**default)
9796
} else {
9897
continue;
@@ -169,7 +168,7 @@ fn check_assign<'a, 'tcx>(
169168
if_chain! {
170169
if block.expr.is_none();
171170
if let Some(expr) = block.stmts.iter().last();
172-
if let hir::StmtKind::Semi(ref expr, _) = expr.node;
171+
if let hir::StmtKind::Semi(ref expr) = expr.node;
173172
if let hir::ExprKind::Assign(ref var, ref value) = expr.node;
174173
if let hir::ExprKind::Path(ref qpath) = var.node;
175174
if let Def::Local(local_id) = cx.tables.qpath_def(qpath, var.hir_id);

‎clippy_lints/src/loops.rs

+14-24
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use if_chain::if_chain;
33
use itertools::Itertools;
44
use rustc::hir::def::Def;
55
use rustc::hir::def_id;
6-
use rustc::hir::intravisit::{walk_block, walk_decl, walk_expr, walk_pat, walk_stmt, NestedVisitorMap, Visitor};
6+
use rustc::hir::intravisit::{walk_block, walk_expr, walk_pat, walk_stmt, NestedVisitorMap, Visitor};
77
use rustc::hir::*;
88
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
99
use rustc::middle::region;
@@ -597,7 +597,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
597597
}
598598

599599
fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx Stmt) {
600-
if let StmtKind::Semi(ref expr, _) = stmt.node {
600+
if let StmtKind::Semi(ref expr) = stmt.node {
601601
if let ExprKind::MethodCall(ref method, _, ref args) = expr.node {
602602
if args.len() == 1 && method.ident.name == "collect" && match_trait_method(cx, expr, &paths::ITERATOR) {
603603
span_lint(
@@ -668,13 +668,7 @@ fn never_loop_block(block: &Block, main_loop_id: NodeId) -> NeverLoopResult {
668668
fn stmt_to_expr(stmt: &Stmt) -> Option<&Expr> {
669669
match stmt.node {
670670
StmtKind::Semi(ref e, ..) | StmtKind::Expr(ref e, ..) => Some(e),
671-
StmtKind::Decl(ref d, ..) => decl_to_expr(d),
672-
}
673-
}
674-
675-
fn decl_to_expr(decl: &Decl) -> Option<&Expr> {
676-
match decl.node {
677-
DeclKind::Local(ref local) => local.init.as_ref().map(|p| &**p),
671+
StmtKind::Local(ref local) => local.init.as_ref().map(|p| &**p),
678672
_ => None,
679673
}
680674
}
@@ -942,8 +936,8 @@ fn get_indexed_assignments<'a, 'tcx>(
942936
stmts
943937
.iter()
944938
.map(|stmt| match stmt.node {
945-
StmtKind::Decl(..) => None,
946-
StmtKind::Expr(ref e, _node_id) | StmtKind::Semi(ref e, _node_id) => Some(get_assignment(cx, e, var)),
939+
StmtKind::Local(..) | StmtKind::Item(..) => None,
940+
StmtKind::Expr(ref e) | StmtKind::Semi(ref e) => Some(get_assignment(cx, e, var)),
947941
})
948942
.chain(expr.as_ref().into_iter().map(|e| Some(get_assignment(cx, &*e, var))))
949943
.filter_map(|op| op)
@@ -1976,13 +1970,9 @@ fn extract_expr_from_first_stmt(block: &Block) -> Option<&Expr> {
19761970
if block.stmts.is_empty() {
19771971
return None;
19781972
}
1979-
if let StmtKind::Decl(ref decl, _) = block.stmts[0].node {
1980-
if let DeclKind::Local(ref local) = decl.node {
1981-
if let Some(ref expr) = local.init {
1982-
Some(expr)
1983-
} else {
1984-
None
1985-
}
1973+
if let StmtKind::Local(ref local) = block.stmts[0].node {
1974+
if let Some(ref expr) = local.init {
1975+
Some(expr)
19861976
} else {
19871977
None
19881978
}
@@ -1996,8 +1986,8 @@ fn extract_first_expr(block: &Block) -> Option<&Expr> {
19961986
match block.expr {
19971987
Some(ref expr) if block.stmts.is_empty() => Some(expr),
19981988
None if !block.stmts.is_empty() => match block.stmts[0].node {
1999-
StmtKind::Expr(ref expr, _) | StmtKind::Semi(ref expr, _) => Some(expr),
2000-
StmtKind::Decl(..) => None,
1989+
StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => Some(expr),
1990+
StmtKind::Local(..) | StmtKind::Item(..) => None,
20011991
},
20021992
_ => None,
20031993
}
@@ -2095,9 +2085,9 @@ struct InitializeVisitor<'a, 'tcx: 'a> {
20952085
}
20962086

20972087
impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
2098-
fn visit_decl(&mut self, decl: &'tcx Decl) {
2088+
fn visit_stmt(&mut self, stmt: &'tcx Stmt) {
20992089
// Look for declarations of the variable
2100-
if let DeclKind::Local(ref local) = decl.node {
2090+
if let StmtKind::Local(ref local) = stmt.node {
21012091
if local.pat.id == self.var_id {
21022092
if let PatKind::Binding(_, _, ident, _) = local.pat.node {
21032093
self.name = Some(ident.name);
@@ -2114,7 +2104,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
21142104
}
21152105
}
21162106
}
2117-
walk_decl(self, decl);
2107+
walk_stmt(self, stmt);
21182108
}
21192109

21202110
fn visit_expr(&mut self, expr: &'tcx Expr) {
@@ -2261,7 +2251,7 @@ struct LoopNestVisitor {
22612251

22622252
impl<'tcx> Visitor<'tcx> for LoopNestVisitor {
22632253
fn visit_stmt(&mut self, stmt: &'tcx Stmt) {
2264-
if stmt.node.id() == self.id {
2254+
if stmt.id == self.id {
22652255
self.nesting = LookFurther;
22662256
} else if self.nesting == Unknown {
22672257
walk_stmt(self, stmt);

‎clippy_lints/src/map_unit_fn.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ fn reduce_unit_expression<'a>(cx: &LateContext<'_, '_>, expr: &'a hir::Expr) ->
131131
// If block only contains statements,
132132
// reduce `{ X; }` to `X` or `X;`
133133
match inner_stmt.node {
134-
hir::StmtKind::Decl(ref d, _) => Some(d.span),
135-
hir::StmtKind::Expr(ref e, _) => Some(e.span),
136-
hir::StmtKind::Semi(_, _) => Some(inner_stmt.span),
134+
hir::StmtKind::Local(ref local) => Some(local.span),
135+
hir::StmtKind::Expr(ref e) => Some(e.span),
136+
hir::StmtKind::Semi(..) => Some(inner_stmt.span),
137+
hir::StmtKind::Item(..) => None,
137138
}
138139
},
139140
_ => {
@@ -250,7 +251,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
250251
return;
251252
}
252253

253-
if let hir::StmtKind::Semi(ref expr, _) = stmt.node {
254+
if let hir::StmtKind::Semi(ref expr) = stmt.node {
254255
if let Some(arglists) = method_chain_args(expr, &["map"]) {
255256
lint_map_unit_fn(cx, stmt, expr, arglists[0]);
256257
}

‎clippy_lints/src/methods/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1336,12 +1336,10 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr, arg: &hir::Exp
13361336
_ => {},
13371337
},
13381338
hir::Node::Stmt(stmt) => {
1339-
if let hir::StmtKind::Decl(ref decl, _) = stmt.node {
1340-
if let hir::DeclKind::Local(ref loc) = decl.node {
1341-
if let hir::PatKind::Ref(..) = loc.pat.node {
1342-
// let ref y = *x borrows x, let ref y = x.clone() does not
1343-
return;
1344-
}
1339+
if let hir::StmtKind::Local(ref loc) = stmt.node {
1340+
if let hir::PatKind::Ref(..) = loc.pat.node {
1341+
// let ref y = *x borrows x, let ref y = x.clone() does not
1342+
return;
13451343
}
13461344
}
13471345
},

‎clippy_lints/src/misc.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
277277

278278
fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, s: &'tcx Stmt) {
279279
if_chain! {
280-
if let StmtKind::Decl(ref d, _) = s.node;
281-
if let DeclKind::Local(ref l) = d.node;
280+
if let StmtKind::Local(ref l) = s.node;
282281
if let PatKind::Binding(an, _, i, None) = l.pat.node;
283282
if let Some(ref init) = l.init;
284283
then {
@@ -316,7 +315,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
316315
}
317316
};
318317
if_chain! {
319-
if let StmtKind::Semi(ref expr, _) = s.node;
318+
if let StmtKind::Semi(ref expr) = s.node;
320319
if let ExprKind::Binary(ref binop, ref a, ref b) = expr.node;
321320
if binop.node == BinOpKind::And || binop.node == BinOpKind::Or;
322321
if let Some(sugg) = Sugg::hir_opt(cx, a);

‎clippy_lints/src/needless_bool.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ fn fetch_bool_block(block: &Block) -> Expression {
267267
match (&*block.stmts, block.expr.as_ref()) {
268268
(&[], Some(e)) => fetch_bool_expr(&**e),
269269
(&[ref e], None) => {
270-
if let StmtKind::Semi(ref e, _) = e.node {
270+
if let StmtKind::Semi(ref e) = e.node {
271271
if let ExprKind::Ret(_) = e.node {
272272
fetch_bool_expr(&**e)
273273
} else {

‎clippy_lints/src/needless_pass_by_value.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ impl<'a, 'tcx> MovedVariablesCtxt<'a, 'tcx> {
368368
Node::Stmt(s) => {
369369
// `let <pat> = x;`
370370
if_chain! {
371-
if let StmtKind::Decl(ref decl, _) = s.node;
372-
if let DeclKind::Local(ref local) = decl.node;
371+
if let StmtKind::Local(ref local) = s.node;
373372
then {
374373
self.spans_need_deref
375374
.entry(vid)

‎clippy_lints/src/no_effect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl LintPass for Pass {
104104

105105
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
106106
fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx Stmt) {
107-
if let StmtKind::Semi(ref expr, _) = stmt.node {
107+
if let StmtKind::Semi(ref expr) = stmt.node {
108108
if has_no_effect(cx, expr) {
109109
span_lint(cx, NO_EFFECT, stmt.span, "statement with no effect");
110110
} else if let Some(reduced) = reduce_expression(cx, expr) {

‎clippy_lints/src/question_mark.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl Pass {
139139
if_chain! {
140140
if block.stmts.len() == 1;
141141
if let Some(expr) = block.stmts.iter().last();
142-
if let StmtKind::Semi(ref expr, _) = expr.node;
142+
if let StmtKind::Semi(ref expr) = expr.node;
143143
if let ExprKind::Ret(ref ret_expr) = expr.node;
144144
if let &Some(ref ret_expr) = ret_expr;
145145

0 commit comments

Comments
 (0)
Please sign in to comment.