Skip to content

Commit 4efdc04

Browse files
committed
Auto merge of #54756 - ljedrz:cleanup_middle, r=michaelwoerister
Cleanup rustc/middle - improve allocations - use `Cow<'static, str>` where applicable - improve some patterns - whitespace & formatting fixes
2 parents 7ec50f4 + 786b86e commit 4efdc04

16 files changed

+641
-711
lines changed

src/librustc/middle/dead.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,10 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
131131

132132
fn mark_live_symbols(&mut self) {
133133
let mut scanned = FxHashSet();
134-
while !self.worklist.is_empty() {
135-
let id = self.worklist.pop().unwrap();
136-
if scanned.contains(&id) {
134+
while let Some(id) = self.worklist.pop() {
135+
if !scanned.insert(id) {
137136
continue
138137
}
139-
scanned.insert(id);
140138

141139
if let Some(ref node) = self.tcx.hir.find(id) {
142140
self.live_symbols.insert(id);
@@ -212,7 +210,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
212210
}
213211

214212
fn visit_variant_data(&mut self, def: &'tcx hir::VariantData, _: ast::Name,
215-
_: &hir::Generics, _: ast::NodeId, _: syntax_pos::Span) {
213+
_: &hir::Generics, _: ast::NodeId, _: syntax_pos::Span) {
216214
let has_repr_c = self.repr_has_repr_c;
217215
let inherited_pub_visibility = self.inherited_pub_visibility;
218216
let live_fields = def.fields().iter().filter(|f| {
@@ -494,8 +492,8 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
494492
ctor_id: Option<ast::NodeId>)
495493
-> bool {
496494
if self.live_symbols.contains(&id)
497-
|| ctor_id.map_or(false,
498-
|ctor| self.live_symbols.contains(&ctor)) {
495+
|| ctor_id.map_or(false, |ctor| self.live_symbols.contains(&ctor))
496+
{
499497
return true;
500498
}
501499
// If it's a type whose items are live, then it's live, too.

src/librustc/middle/dependency_format.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
163163
let src = tcx.used_crate_source(cnum);
164164
if src.rlib.is_some() { continue }
165165
sess.err(&format!("crate `{}` required to be available in rlib format, \
166-
but was not found in this form",
166+
but was not found in this form",
167167
tcx.crate_name(cnum)));
168168
}
169169
return Vec::new();
@@ -247,13 +247,13 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
247247
_ => "dylib",
248248
};
249249
sess.err(&format!("crate `{}` required to be available in {} format, \
250-
but was not found in this form",
250+
but was not found in this form",
251251
tcx.crate_name(cnum), kind));
252252
}
253253
}
254254
}
255255

256-
return ret;
256+
ret
257257
}
258258

259259
fn add_library(tcx: TyCtxt<'_, '_, '_>,

src/librustc/middle/entry.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
1211
use hir::map as hir_map;
1312
use hir::def_id::{CRATE_DEF_INDEX};
1413
use session::{config, Session};
@@ -131,7 +130,7 @@ fn find_item(item: &Item, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
131130
ctxt.attr_main_fn = Some((item.id, item.span));
132131
} else {
133132
struct_span_err!(ctxt.session, item.span, E0137,
134-
"multiple functions with a #[main] attribute")
133+
"multiple functions with a #[main] attribute")
135134
.span_label(item.span, "additional #[main] function")
136135
.span_label(ctxt.attr_main_fn.unwrap().1, "first #[main] function")
137136
.emit();
@@ -141,11 +140,8 @@ fn find_item(item: &Item, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
141140
if ctxt.start_fn.is_none() {
142141
ctxt.start_fn = Some((item.id, item.span));
143142
} else {
144-
struct_span_err!(
145-
ctxt.session, item.span, E0138,
146-
"multiple 'start' functions")
147-
.span_label(ctxt.start_fn.unwrap().1,
148-
"previous `start` function here")
143+
struct_span_err!(ctxt.session, item.span, E0138, "multiple 'start' functions")
144+
.span_label(ctxt.start_fn.unwrap().1, "previous `start` function here")
149145
.span_label(item.span, "multiple `start` functions")
150146
.emit();
151147
}

src/librustc/middle/exported_symbols.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,8 @@ impl_stable_hash_for!(enum self::SymbolExportLevel {
3535

3636
impl SymbolExportLevel {
3737
pub fn is_below_threshold(self, threshold: SymbolExportLevel) -> bool {
38-
if threshold == SymbolExportLevel::Rust {
39-
// We export everything from Rust dylibs
40-
true
41-
} else {
42-
self == SymbolExportLevel::C
43-
}
38+
threshold == SymbolExportLevel::Rust // export everything from Rust dylibs
39+
|| self == SymbolExportLevel::C
4440
}
4541
}
4642

src/librustc/middle/expr_use_visitor.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
321321
region::Scope {
322322
id: body.value.hir_id.local_id,
323323
data: region::ScopeData::Node
324-
}));
324+
}));
325325
let arg_cmt = Rc::new(self.mc.cat_rvalue(
326326
arg.hir_id,
327327
arg.pat.span,
@@ -402,20 +402,20 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
402402
self.walk_expr(&subexpr)
403403
}
404404

405-
hir::ExprKind::Unary(hir::UnDeref, ref base) => { // *base
405+
hir::ExprKind::Unary(hir::UnDeref, ref base) => { // *base
406406
self.select_from_expr(&base);
407407
}
408408

409-
hir::ExprKind::Field(ref base, _) => { // base.f
409+
hir::ExprKind::Field(ref base, _) => { // base.f
410410
self.select_from_expr(&base);
411411
}
412412

413-
hir::ExprKind::Index(ref lhs, ref rhs) => { // lhs[rhs]
413+
hir::ExprKind::Index(ref lhs, ref rhs) => { // lhs[rhs]
414414
self.select_from_expr(&lhs);
415415
self.consume_expr(&rhs);
416416
}
417417

418-
hir::ExprKind::Call(ref callee, ref args) => { // callee(args)
418+
hir::ExprKind::Call(ref callee, ref args) => { // callee(args)
419419
self.walk_callee(expr, &callee);
420420
self.consume_exprs(args);
421421
}
@@ -801,10 +801,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
801801
self.walk_pat(discr_cmt.clone(), &pat, mode);
802802
}
803803

804-
if let Some(ref guard) = arm.guard {
805-
match guard {
806-
hir::Guard::If(ref e) => self.consume_expr(e),
807-
}
804+
if let Some(hir::Guard::If(ref e)) = arm.guard {
805+
self.consume_expr(e)
808806
}
809807

810808
self.consume_expr(&arm.body);
@@ -826,12 +824,13 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
826824
cmt_discr: mc::cmt<'tcx>,
827825
pat: &hir::Pat,
828826
mode: &mut TrackMatchMode) {
829-
debug!("determine_pat_move_mode cmt_discr={:?} pat={:?}", cmt_discr,
830-
pat);
827+
debug!("determine_pat_move_mode cmt_discr={:?} pat={:?}", cmt_discr, pat);
828+
831829
return_if_err!(self.mc.cat_pattern(cmt_discr, pat, |cmt_pat, pat| {
832830
if let PatKind::Binding(..) = pat.node {
833-
let bm = *self.mc.tables.pat_binding_modes().get(pat.hir_id)
834-
.expect("missing binding mode");
831+
let bm = *self.mc.tables.pat_binding_modes()
832+
.get(pat.hir_id)
833+
.expect("missing binding mode");
835834
match bm {
836835
ty::BindByReference(..) =>
837836
mode.lub(BorrowingMatch),

src/librustc/middle/intrinsicck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
107107
}
108108
Err(LayoutError::Unknown(bad)) => {
109109
if bad == ty {
110-
"this type's size can vary".to_string()
110+
"this type's size can vary".to_owned()
111111
} else {
112112
format!("size can vary because of {}", bad)
113113
}
@@ -117,7 +117,7 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
117117
};
118118

119119
struct_span_err!(self.tcx.sess, span, E0512,
120-
"transmute called with types of different sizes")
120+
"transmute called with types of different sizes")
121121
.note(&format!("source type: {} ({})", from, skeleton_string(from, sk_from)))
122122
.note(&format!("target type: {} ({})", to, skeleton_string(to, sk_to)))
123123
.emit();

src/librustc/middle/lang_items.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ macro_rules! language_item_table {
3939
$( $variant:ident, $name:expr, $method:ident; )*
4040
) => {
4141

42-
4342
enum_from_u32! {
4443
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
4544
pub enum LangItem {
@@ -145,8 +144,8 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
145144

146145
fn collect_item(&mut self, item_index: usize, item_def_id: DefId) {
147146
// Check for duplicates.
148-
match self.items.items[item_index] {
149-
Some(original_def_id) if original_def_id != item_def_id => {
147+
if let Some(original_def_id) = self.items.items[item_index] {
148+
if original_def_id != item_def_id {
150149
let name = LangItem::from_u32(item_index as u32).unwrap().name();
151150
let mut err = match self.tcx.hir.span_if_local(item_def_id) {
152151
Some(span) => struct_span_err!(
@@ -161,17 +160,13 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
161160
name)),
162161
};
163162
if let Some(span) = self.tcx.hir.span_if_local(original_def_id) {
164-
span_note!(&mut err, span,
165-
"first defined here.");
163+
span_note!(&mut err, span, "first defined here.");
166164
} else {
167165
err.note(&format!("first defined in crate `{}`.",
168166
self.tcx.crate_name(original_def_id.krate)));
169167
}
170168
err.emit();
171169
}
172-
_ => {
173-
// OK.
174-
}
175170
}
176171

177172
// Matched.
@@ -194,7 +189,7 @@ pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
194189
}
195190
}
196191

197-
return None;
192+
None
198193
}
199194

200195
pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItems {

src/librustc/middle/lib_features.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ impl<'a, 'tcx> LibFeatureCollector<'a, 'tcx> {
128128
let msg = format!(
129129
"feature `{}` is declared {}, but was previously declared {}",
130130
feature,
131-
if since.is_some() { "stable"} else { "unstable" },
132-
if since.is_none() { "stable"} else { "unstable" },
131+
if since.is_some() { "stable" } else { "unstable" },
132+
if since.is_none() { "stable" } else { "unstable" },
133133
);
134134
self.tcx.sess.struct_span_err_with_code(span, &msg,
135135
DiagnosticId::Error("E0711".into())).emit();

0 commit comments

Comments
 (0)