Skip to content

Commit 0b2e9f7

Browse files
committed
rustc/session: improve common patterns
1 parent 675f00b commit 0b2e9f7

File tree

3 files changed

+35
-58
lines changed

3 files changed

+35
-58
lines changed

src/librustc/session/config.rs

+17-28
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,7 @@ pub fn default_configuration(sess: &Session) -> ast::CrateConfig {
14531453
if sess.opts.crate_types.contains(&CrateType::ProcMacro) {
14541454
ret.insert((Symbol::intern("proc_macro"), None));
14551455
}
1456-
return ret;
1456+
ret
14571457
}
14581458

14591459
pub fn build_configuration(sess: &Session, mut user_cfg: ast::CrateConfig) -> ast::CrateConfig {
@@ -1469,15 +1469,12 @@ pub fn build_configuration(sess: &Session, mut user_cfg: ast::CrateConfig) -> as
14691469
}
14701470

14711471
pub fn build_target_config(opts: &Options, sp: &Handler) -> Config {
1472-
let target = match Target::search(&opts.target_triple) {
1473-
Ok(t) => t,
1474-
Err(e) => {
1475-
sp.struct_fatal(&format!("Error loading target specification: {}", e))
1476-
.help("Use `--print target-list` for a list of built-in targets")
1477-
.emit();
1478-
FatalError.raise();
1479-
}
1480-
};
1472+
let target = Target::search(&opts.target_triple).unwrap_or_else(|e| {
1473+
sp.struct_fatal(&format!("Error loading target specification: {}", e))
1474+
.help("Use `--print target-list` for a list of built-in targets")
1475+
.emit();
1476+
FatalError.raise();
1477+
});
14811478

14821479
let (isize_ty, usize_ty) = match &target.target_pointer_width[..] {
14831480
"16" => (ast::IntTy::I16, ast::UintTy::U16),
@@ -1842,9 +1839,8 @@ pub fn build_session_options_and_crate_config(
18421839
};
18431840

18441841
let edition = match matches.opt_str("edition") {
1845-
Some(arg) => match Edition::from_str(&arg){
1846-
Ok(edition) => edition,
1847-
Err(_) => early_error(
1842+
Some(arg) => Edition::from_str(&arg).unwrap_or_else(|_|
1843+
early_error(
18481844
ErrorOutputType::default(),
18491845
&format!(
18501846
"argument for --edition must be one of: \
@@ -1853,7 +1849,7 @@ pub fn build_session_options_and_crate_config(
18531849
arg
18541850
),
18551851
),
1856-
}
1852+
),
18571853
None => DEFAULT_EDITION,
18581854
};
18591855

@@ -1922,17 +1918,16 @@ pub fn build_session_options_and_crate_config(
19221918
for output_type in list.split(',') {
19231919
let mut parts = output_type.splitn(2, '=');
19241920
let shorthand = parts.next().unwrap();
1925-
let output_type = match OutputType::from_shorthand(shorthand) {
1926-
Some(output_type) => output_type,
1927-
None => early_error(
1921+
let output_type = OutputType::from_shorthand(shorthand).unwrap_or_else(||
1922+
early_error(
19281923
error_format,
19291924
&format!(
19301925
"unknown emission type: `{}` - expected one of: {}",
19311926
shorthand,
19321927
OutputType::shorthands_display(),
19331928
),
19341929
),
1935-
};
1930+
);
19361931
let path = parts.next().map(PathBuf::from);
19371932
output_types.insert(output_type, path);
19381933
}
@@ -2060,12 +2055,8 @@ pub fn build_session_options_and_crate_config(
20602055
let target_triple = if let Some(target) = matches.opt_str("target") {
20612056
if target.ends_with(".json") {
20622057
let path = Path::new(&target);
2063-
match TargetTriple::from_path(&path) {
2064-
Ok(triple) => triple,
2065-
Err(_) => {
2066-
early_error(error_format, &format!("target file {:?} does not exist", path))
2067-
}
2068-
}
2058+
TargetTriple::from_path(&path).unwrap_or_else(|_|
2059+
early_error(error_format, &format!("target file {:?} does not exist", path)))
20692060
} else {
20702061
TargetTriple::TargetTriple(target)
20712062
}
@@ -2220,10 +2211,8 @@ pub fn build_session_options_and_crate_config(
22202211
let mut externs: BTreeMap<_, BTreeSet<_>> = BTreeMap::new();
22212212
for arg in &matches.opt_strs("extern") {
22222213
let mut parts = arg.splitn(2, '=');
2223-
let name = match parts.next() {
2224-
Some(s) => s,
2225-
None => early_error(error_format, "--extern value must not be empty"),
2226-
};
2214+
let name = parts.next().unwrap_or_else(||
2215+
early_error(error_format, "--extern value must not be empty"));
22272216
let location = parts.next().map(|s| s.to_string());
22282217
if location.is_none() && !is_unstable_enabled {
22292218
early_error(

src/librustc/session/filesearch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub fn get_or_default_sysroot() -> PathBuf {
160160
match env::current_exe() {
161161
Ok(exe) => {
162162
match canonicalize(Some(exe)) {
163-
Some(mut p) => { p.pop(); p.pop(); return p; },
163+
Some(mut p) => { p.pop(); p.pop(); p },
164164
None => bug!("can't determine value for sysroot")
165165
}
166166
}

src/librustc/session/mod.rs

+17-29
Original file line numberDiff line numberDiff line change
@@ -727,14 +727,8 @@ impl Session {
727727
pub fn set_incr_session_load_dep_graph(&self, load: bool) {
728728
let mut incr_comp_session = self.incr_comp_session.borrow_mut();
729729

730-
match *incr_comp_session {
731-
IncrCompSession::Active {
732-
ref mut load_dep_graph,
733-
..
734-
} => {
735-
*load_dep_graph = load;
736-
}
737-
_ => {}
730+
if let IncrCompSession::Active { ref mut load_dep_graph, .. } = *incr_comp_session {
731+
*load_dep_graph = load;
738732
}
739733
}
740734

@@ -872,9 +866,9 @@ impl Session {
872866
/// This expends fuel if applicable, and records fuel if applicable.
873867
pub fn consider_optimizing<T: Fn() -> String>(&self, crate_name: &str, msg: T) -> bool {
874868
let mut ret = true;
875-
match self.optimization_fuel_crate {
876-
Some(ref c) if c == crate_name => {
877-
assert!(self.query_threads() == 1);
869+
if let Some(ref c) = self.optimization_fuel_crate {
870+
if c == crate_name {
871+
assert_eq!(self.query_threads(), 1);
878872
let fuel = self.optimization_fuel_limit.get();
879873
ret = fuel != 0;
880874
if fuel == 0 && !self.out_of_fuel.get() {
@@ -884,14 +878,12 @@ impl Session {
884878
self.optimization_fuel_limit.set(fuel - 1);
885879
}
886880
}
887-
_ => {}
888881
}
889-
match self.print_fuel_crate {
890-
Some(ref c) if c == crate_name => {
891-
assert!(self.query_threads() == 1);
882+
if let Some(ref c) = self.print_fuel_crate {
883+
if c == crate_name {
884+
assert_eq!(self.query_threads(), 1);
892885
self.print_fuel.set(self.print_fuel.get() + 1);
893886
}
894-
_ => {}
895887
}
896888
ret
897889
}
@@ -1108,14 +1100,11 @@ pub fn build_session_(
11081100
source_map: Lrc<source_map::SourceMap>,
11091101
) -> Session {
11101102
let host_triple = TargetTriple::from_triple(config::host_triple());
1111-
let host = match Target::search(&host_triple) {
1112-
Ok(t) => t,
1113-
Err(e) => {
1114-
span_diagnostic
1115-
.fatal(&format!("Error loading host specification: {}", e))
1116-
.raise();
1117-
}
1118-
};
1103+
let host = Target::search(&host_triple).unwrap_or_else(|e|
1104+
span_diagnostic
1105+
.fatal(&format!("Error loading host specification: {}", e))
1106+
.raise()
1107+
);
11191108
let target_cfg = config::build_target_config(&sopts, &span_diagnostic);
11201109

11211110
let p_s = parse::ParseSess::with_span_handler(span_diagnostic, source_map);
@@ -1135,12 +1124,11 @@ pub fn build_session_(
11351124
let print_fuel_crate = sopts.debugging_opts.print_fuel.clone();
11361125
let print_fuel = LockCell::new(0);
11371126

1138-
let working_dir = match env::current_dir() {
1139-
Ok(dir) => dir,
1140-
Err(e) => p_s.span_diagnostic
1127+
let working_dir = env::current_dir().unwrap_or_else(|e|
1128+
p_s.span_diagnostic
11411129
.fatal(&format!("Current directory is invalid: {}", e))
1142-
.raise(),
1143-
};
1130+
.raise()
1131+
);
11441132
let working_dir = file_path_mapping.map_prefix(working_dir);
11451133

11461134
let cgu_reuse_tracker = if sopts.debugging_opts.query_dep_graph {

0 commit comments

Comments
 (0)