Skip to content

Commit 8bbe178

Browse files
committed
A few cleanups and minor improvements to rustc_passes
1 parent 2ae11a9 commit 8bbe178

File tree

5 files changed

+96
-128
lines changed

5 files changed

+96
-128
lines changed

src/librustc_passes/ast_validation.rs

+23-30
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,19 @@ impl<'a> AstValidator<'a> {
9898
}
9999

100100
fn check_trait_fn_not_const(&self, constness: Spanned<Constness>) {
101-
match constness.node {
102-
Constness::Const => {
103-
struct_span_err!(self.session, constness.span, E0379,
104-
"trait fns cannot be declared const")
105-
.span_label(constness.span, "trait fns cannot be const")
106-
.emit();
107-
}
108-
_ => {}
101+
if constness.node == Constness::Const {
102+
struct_span_err!(self.session, constness.span, E0379,
103+
"trait fns cannot be declared const")
104+
.span_label(constness.span, "trait fns cannot be const")
105+
.emit();
109106
}
110107
}
111108

112109
fn no_questions_in_bounds(&self, bounds: &GenericBounds, where_: &str, is_trait: bool) {
113110
for bound in bounds {
114111
if let GenericBound::Trait(ref poly, TraitBoundModifier::Maybe) = *bound {
115112
let mut err = self.err_handler().struct_span_err(poly.span,
116-
&format!("`?Trait` is not permitted in {}", where_));
113+
&format!("`?Trait` is not permitted in {}", where_));
117114
if is_trait {
118115
err.note(&format!("traits are `?{}` by default", poly.trait_ref.path));
119116
}
@@ -152,16 +149,16 @@ impl<'a> AstValidator<'a> {
152149
// Check only lifetime parameters are present and that the lifetime
153150
// parameters that are present have no bounds.
154151
let non_lt_param_spans: Vec<_> = params.iter().filter_map(|param| match param.kind {
155-
GenericParamKind::Lifetime { .. } => {
156-
if !param.bounds.is_empty() {
157-
let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
158-
self.err_handler()
159-
.span_err(spans, "lifetime bounds cannot be used in this context");
160-
}
161-
None
152+
GenericParamKind::Lifetime { .. } => {
153+
if !param.bounds.is_empty() {
154+
let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
155+
self.err_handler()
156+
.span_err(spans, "lifetime bounds cannot be used in this context");
162157
}
163-
_ => Some(param.ident.span),
164-
}).collect();
158+
None
159+
}
160+
_ => Some(param.ident.span),
161+
}).collect();
165162
if !non_lt_param_spans.is_empty() {
166163
self.err_handler().span_err(non_lt_param_spans,
167164
"only lifetime parameters can be used in this context");
@@ -387,7 +384,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
387384
self.err_handler().span_err(item.span,
388385
"tuple and unit unions are not permitted");
389386
}
390-
if vdata.fields().len() == 0 {
387+
if vdata.fields().is_empty() {
391388
self.err_handler().span_err(item.span,
392389
"unions cannot have zero fields");
393390
}
@@ -414,14 +411,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
414411
}
415412

416413
fn visit_vis(&mut self, vis: &'a Visibility) {
417-
match vis.node {
418-
VisibilityKind::Restricted { ref path, .. } => {
419-
path.segments.iter().find(|segment| segment.args.is_some()).map(|segment| {
420-
self.err_handler().span_err(segment.args.as_ref().unwrap().span(),
421-
"generic arguments in visibility path");
422-
});
423-
}
424-
_ => {}
414+
if let VisibilityKind::Restricted { ref path, .. } = vis.node {
415+
path.segments.iter().find(|segment| segment.args.is_some()).map(|segment| {
416+
self.err_handler().span_err(segment.args.as_ref().unwrap().span(),
417+
"generic arguments in visibility path");
418+
});
425419
}
426420

427421
visit::walk_vis(self, vis)
@@ -591,8 +585,7 @@ impl<'a> Visitor<'a> for ImplTraitProjectionVisitor<'a> {
591585
TyKind::ImplTrait(..) => {
592586
if self.is_banned {
593587
struct_span_err!(self.session, t.span, E0667,
594-
"`impl Trait` is not allowed in path parameters")
595-
.emit();
588+
"`impl Trait` is not allowed in path parameters").emit();
596589
}
597590
}
598591
TyKind::Path(ref qself, ref path) => {
@@ -616,7 +609,7 @@ impl<'a> Visitor<'a> for ImplTraitProjectionVisitor<'a> {
616609

617610
for (i, segment) in path.segments.iter().enumerate() {
618611
// Allow `impl Trait` iff we're on the final path segment
619-
if i == (path.segments.len() - 1) {
612+
if i == path.segments.len() - 1 {
620613
visit::walk_path_segment(self, path.span, segment);
621614
} else {
622615
self.with_ban(|this|

src/librustc_passes/hir_stats.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ pub fn print_ast_stats<'v>(krate: &'v ast::Crate, title: &str) {
6161
impl<'k> StatCollector<'k> {
6262

6363
fn record<T>(&mut self, label: &'static str, id: Id, node: &T) {
64-
if id != Id::None {
65-
if !self.seen.insert(id) {
66-
return
67-
}
64+
if id != Id::None && !self.seen.insert(id) {
65+
return
6866
}
6967

7068
let entry = self.data.entry(label).or_insert(NodeData {
@@ -135,40 +133,46 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
135133
hir_visit::walk_item(self, i)
136134
}
137135

138-
///////////////////////////////////////////////////////////////////////////
139-
140136
fn visit_mod(&mut self, m: &'v hir::Mod, _s: Span, n: NodeId) {
141137
self.record("Mod", Id::None, m);
142138
hir_visit::walk_mod(self, m, n)
143139
}
140+
144141
fn visit_foreign_item(&mut self, i: &'v hir::ForeignItem) {
145142
self.record("ForeignItem", Id::Node(i.id), i);
146143
hir_visit::walk_foreign_item(self, i)
147144
}
145+
148146
fn visit_local(&mut self, l: &'v hir::Local) {
149147
self.record("Local", Id::Node(l.id), l);
150148
hir_visit::walk_local(self, l)
151149
}
150+
152151
fn visit_block(&mut self, b: &'v hir::Block) {
153152
self.record("Block", Id::Node(b.id), b);
154153
hir_visit::walk_block(self, b)
155154
}
155+
156156
fn visit_stmt(&mut self, s: &'v hir::Stmt) {
157157
self.record("Stmt", Id::Node(s.node.id()), s);
158158
hir_visit::walk_stmt(self, s)
159159
}
160+
160161
fn visit_arm(&mut self, a: &'v hir::Arm) {
161162
self.record("Arm", Id::None, a);
162163
hir_visit::walk_arm(self, a)
163164
}
165+
164166
fn visit_pat(&mut self, p: &'v hir::Pat) {
165167
self.record("Pat", Id::Node(p.id), p);
166168
hir_visit::walk_pat(self, p)
167169
}
170+
168171
fn visit_decl(&mut self, d: &'v hir::Decl) {
169172
self.record("Decl", Id::None, d);
170173
hir_visit::walk_decl(self, d)
171174
}
175+
172176
fn visit_expr(&mut self, ex: &'v hir::Expr) {
173177
self.record("Expr", Id::Node(ex.id), ex);
174178
hir_visit::walk_expr(self, ex)
@@ -198,6 +202,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
198202
self.record("TraitItem", Id::Node(ti.id), ti);
199203
hir_visit::walk_trait_item(self, ti)
200204
}
205+
201206
fn visit_impl_item(&mut self, ii: &'v hir::ImplItem) {
202207
self.record("ImplItem", Id::Node(ii.id), ii);
203208
hir_visit::walk_impl_item(self, ii)
@@ -220,31 +225,38 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
220225
self.record("Variant", Id::None, v);
221226
hir_visit::walk_variant(self, v, g, item_id)
222227
}
228+
223229
fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
224230
self.record("Lifetime", Id::Node(lifetime.id), lifetime);
225231
hir_visit::walk_lifetime(self, lifetime)
226232
}
233+
227234
fn visit_qpath(&mut self, qpath: &'v hir::QPath, id: hir::HirId, span: Span) {
228235
self.record("QPath", Id::None, qpath);
229236
hir_visit::walk_qpath(self, qpath, id, span)
230237
}
238+
231239
fn visit_path(&mut self, path: &'v hir::Path, _id: hir::HirId) {
232240
self.record("Path", Id::None, path);
233241
hir_visit::walk_path(self, path)
234242
}
243+
235244
fn visit_path_segment(&mut self,
236245
path_span: Span,
237246
path_segment: &'v hir::PathSegment) {
238247
self.record("PathSegment", Id::None, path_segment);
239248
hir_visit::walk_path_segment(self, path_span, path_segment)
240249
}
250+
241251
fn visit_assoc_type_binding(&mut self, type_binding: &'v hir::TypeBinding) {
242252
self.record("TypeBinding", Id::Node(type_binding.id), type_binding);
243253
hir_visit::walk_assoc_type_binding(self, type_binding)
244254
}
255+
245256
fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
246257
self.record("Attribute", Id::Attr(attr.id), attr);
247258
}
259+
248260
fn visit_macro_def(&mut self, macro_def: &'v hir::MacroDef) {
249261
self.record("MacroDef", Id::Node(macro_def.id), macro_def);
250262
hir_visit::walk_macro_def(self, macro_def)

src/librustc_passes/loops.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
114114
};
115115

116116
if loop_id != ast::DUMMY_NODE_ID {
117-
match self.hir_map.find(loop_id).unwrap() {
118-
Node::Block(_) => return,
119-
_=> (),
117+
if let Node::Block(_) = self.hir_map.find(loop_id).unwrap() {
118+
return
120119
}
121120
}
122121

@@ -153,10 +152,10 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
153152

154153
self.require_break_cx("break", e.span);
155154
}
156-
hir::ExprKind::Continue(label) => {
157-
self.require_label_in_labeled_block(e.span, &label, "continue");
155+
hir::ExprKind::Continue(destination) => {
156+
self.require_label_in_labeled_block(e.span, &destination, "continue");
158157

159-
match label.target_id {
158+
match destination.target_id {
160159
Ok(loop_id) => {
161160
if let Node::Block(block) = self.hir_map.find(loop_id).unwrap() {
162161
struct_span_err!(self.sess, e.span, E0696,
@@ -171,7 +170,7 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
171170
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
172171
self.emit_unlabled_cf_in_while_condition(e.span, "continue");
173172
}
174-
_ => {}
173+
Err(_) => {}
175174
}
176175
self.require_break_cx("continue", e.span)
177176
},
@@ -192,8 +191,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
192191

193192
fn require_break_cx(&self, name: &str, span: Span) {
194193
match self.cx {
195-
LabeledBlock |
196-
Loop(_) => {}
194+
LabeledBlock | Loop(_) => {}
197195
Closure => {
198196
struct_span_err!(self.sess, span, E0267, "`{}` inside of a closure", name)
199197
.span_label(span, "cannot break inside of a closure")

src/librustc_passes/mir_stats.rs

+11-30
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,12 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
6565
self.super_mir(mir);
6666
}
6767

68-
fn visit_basic_block_data(&mut self,
69-
block: BasicBlock,
70-
data: &BasicBlockData<'tcx>) {
68+
fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) {
7169
self.record("BasicBlockData", data);
7270
self.super_basic_block_data(block, data);
7371
}
7472

75-
fn visit_source_scope_data(&mut self,
76-
scope_data: &SourceScopeData) {
73+
fn visit_source_scope_data(&mut self, scope_data: &SourceScopeData) {
7774
self.record("SourceScopeData", scope_data);
7875
self.super_source_scope_data(scope_data);
7976
}
@@ -130,9 +127,7 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
130127
self.super_terminator_kind(block, kind, location);
131128
}
132129

133-
fn visit_assert_message(&mut self,
134-
msg: &AssertMessage<'tcx>,
135-
location: Location) {
130+
fn visit_assert_message(&mut self, msg: &AssertMessage<'tcx>, location: Location) {
136131
self.record("AssertMessage", msg);
137132
self.record(match *msg {
138133
EvalErrorKind::BoundsCheck { .. } => "AssertMessage::BoundsCheck",
@@ -151,9 +146,7 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
151146
self.super_assert_message(msg, location);
152147
}
153148

154-
fn visit_rvalue(&mut self,
155-
rvalue: &Rvalue<'tcx>,
156-
location: Location) {
149+
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
157150
self.record("Rvalue", rvalue);
158151
let rvalue_kind = match *rvalue {
159152
Rvalue::Use(..) => "Rvalue::Use",
@@ -184,9 +177,7 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
184177
self.super_rvalue(rvalue, location);
185178
}
186179

187-
fn visit_operand(&mut self,
188-
operand: &Operand<'tcx>,
189-
location: Location) {
180+
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
190181
self.record("Operand", operand);
191182
self.record(match *operand {
192183
Operand::Copy(..) => "Operand::Copy",
@@ -234,42 +225,32 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
234225
self.super_projection_elem(place, context, location);
235226
}
236227

237-
fn visit_constant(&mut self,
238-
constant: &Constant<'tcx>,
239-
location: Location) {
228+
fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
240229
self.record("Constant", constant);
241230
self.super_constant(constant, location);
242231
}
243232

244-
fn visit_source_info(&mut self,
245-
source_info: &SourceInfo) {
233+
fn visit_source_info(&mut self, source_info: &SourceInfo) {
246234
self.record("SourceInfo", source_info);
247235
self.super_source_info(source_info);
248236
}
249237

250-
fn visit_closure_substs(&mut self,
251-
substs: &ClosureSubsts<'tcx>,
252-
_: Location) {
238+
fn visit_closure_substs(&mut self, substs: &ClosureSubsts<'tcx>, _: Location) {
253239
self.record("ClosureSubsts", substs);
254240
self.super_closure_substs(substs);
255241
}
256242

257-
fn visit_const(&mut self,
258-
constant: &&'tcx ty::Const<'tcx>,
259-
_: Location) {
243+
fn visit_const(&mut self, constant: &&'tcx ty::Const<'tcx>, _: Location) {
260244
self.record("Const", constant);
261245
self.super_const(constant);
262246
}
263247

264-
fn visit_local_decl(&mut self,
265-
local: Local,
266-
local_decl: &LocalDecl<'tcx>) {
248+
fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
267249
self.record("LocalDecl", local_decl);
268250
self.super_local_decl(local, local_decl);
269251
}
270252

271-
fn visit_source_scope(&mut self,
272-
scope: &SourceScope) {
253+
fn visit_source_scope(&mut self, scope: &SourceScope) {
273254
self.record("VisiblityScope", scope);
274255
self.super_source_scope(scope);
275256
}

0 commit comments

Comments
 (0)