diff --git a/src/bin/cargo-fmt.rs b/src/bin/cargo-fmt.rs index 442cb9e78f7..c55c283c654 100644 --- a/src/bin/cargo-fmt.rs +++ b/src/bin/cargo-fmt.rs @@ -105,7 +105,7 @@ pub enum Verbosity { fn format_crate(verbosity: Verbosity, workspace_hitlist: WorkspaceHitlist) -> Result { - let targets = try!(get_targets(workspace_hitlist)); + let targets = get_targets(workspace_hitlist)?; // Currently only bin and lib files get formatted let files: Vec<_> = targets @@ -181,7 +181,7 @@ pub fn from_matches(matches: &Matches) -> WorkspaceHitlist { fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result, std::io::Error> { let mut targets: Vec = vec![]; if workspace_hitlist == WorkspaceHitlist::None { - let output = try!(Command::new("cargo").arg("read-manifest").output()); + let output = Command::new("cargo").arg("read-manifest").output()?; if output.status.success() { // None of the unwraps should fail if output of `cargo read-manifest` is correct let data = &String::from_utf8(output.stdout).unwrap(); @@ -287,7 +287,7 @@ fn format_files(files: &[PathBuf], } println!(""); } - let mut command = try!(Command::new("rustfmt") + let mut command = Command::new("rustfmt") .stdout(stdout) .args(files) .args(fmt_args) @@ -298,6 +298,6 @@ fn format_files(files: &[PathBuf], "Could not run rustfmt, please make sure it is in your PATH.") } _ => e, - })); + })?; command.wait() } diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 2c741934a5e..2c5cf697362 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -82,7 +82,7 @@ fn from_matches(matches: &Matches) -> FmtResult { } if let Some(ref file_lines) = matches.opt_str("file-lines") { - options.file_lines = try!(file_lines.parse()); + options.file_lines = file_lines.parse()?; } Ok(options) @@ -104,12 +104,12 @@ fn apply_to(self, config: &mut Config) { /// nearest project file if one exists, or `None` if no project file was found. fn lookup_project_file(dir: &Path) -> FmtResult> { let mut current = if dir.is_relative() { - try!(env::current_dir()).join(dir) + env::current_dir()?.join(dir) } else { dir.to_path_buf() }; - current = try!(fs::canonicalize(current)); + current = fs::canonicalize(current)?; loop { for config_file_name in &CONFIG_FILE_NAMES { @@ -137,9 +137,9 @@ fn lookup_project_file(dir: &Path) -> FmtResult> { } fn open_config_file(file_path: &Path) -> FmtResult<(Config, Option)> { - let mut file = try!(File::open(&file_path)); + let mut file = File::open(&file_path)?; let mut toml = String::new(); - try!(file.read_to_string(&mut toml)); + file.read_to_string(&mut toml)?; match Config::from_toml(&toml) { Ok(cfg) => Ok((cfg, Some(file_path.to_path_buf()))), Err(err) => Err(FmtError::from(err)), @@ -151,7 +151,7 @@ fn open_config_file(file_path: &Path) -> FmtResult<(Config, Option)> { /// Returns the `Config` to use, and the path of the project file if there was /// one. fn resolve_config(dir: &Path) -> FmtResult<(Config, Option)> { - let path = try!(lookup_project_file(dir)); + let path = lookup_project_file(dir)?; if path.is_none() { return Ok((Config::default(), None)); } @@ -164,7 +164,7 @@ fn match_cli_path_or_file(config_path: Option, -> FmtResult<(Config, Option)> { if let Some(config_file) = config_path { - let (toml, path) = try!(open_config_file(config_file.as_ref())); + let (toml, path) = open_config_file(config_file.as_ref())?; if path.is_some() { return Ok((toml, path)); } @@ -200,9 +200,9 @@ fn make_opts() -> Options { } fn execute(opts: &Options) -> FmtResult { - let matches = try!(opts.parse(env::args().skip(1))); + let matches = opts.parse(env::args().skip(1))?; - match try!(determine_operation(&matches)) { + match determine_operation(&matches)? { Operation::Help => { print_usage(opts, ""); Summary::print_exit_codes(); @@ -226,7 +226,7 @@ fn execute(opts: &Options) -> FmtResult { // parse file_lines if let Some(ref file_lines) = matches.opt_str("file-lines") { - config.file_lines = try!(file_lines.parse()); + config.file_lines = file_lines.parse()?; for f in config.file_lines.files() { if f != "stdin" { println!("Warning: Extra file listed in file_lines option '{}'", f); @@ -237,7 +237,7 @@ fn execute(opts: &Options) -> FmtResult { Ok(run(Input::Text(input), &config)) } Operation::Format { files, config_path } => { - let options = try!(CliOptions::from_matches(&matches)); + let options = CliOptions::from_matches(&matches)?; for f in options.file_lines.files() { if !files.contains(&PathBuf::from(f)) { @@ -386,7 +386,7 @@ fn determine_operation(matches: &Matches) -> FmtResult { // if no file argument is supplied, read from stdin if matches.free.is_empty() { let mut buffer = String::new(); - try!(io::stdin().read_to_string(&mut buffer)); + io::stdin().read_to_string(&mut buffer)?; return Ok(Operation::Stdin { input: buffer, diff --git a/src/checkstyle.rs b/src/checkstyle.rs index 3fc117904c7..69c89a9c5fb 100644 --- a/src/checkstyle.rs +++ b/src/checkstyle.rs @@ -20,7 +20,7 @@ pub fn output_header(out: &mut T, mode: WriteMode) -> Result<(), io::Error> xml_heading.push_str(""); xml_heading.push_str("\n"); xml_heading.push_str(""); - try!(write!(out, "{}", xml_heading)); + write!(out, "{}", xml_heading)?; } Ok(()) } @@ -31,7 +31,7 @@ pub fn output_footer(out: &mut T, mode: WriteMode) -> Result<(), io::Error> if mode == WriteMode::Checkstyle { let mut xml_tail = String::new(); xml_tail.push_str(""); - try!(write!(out, "{}", xml_tail)); + write!(out, "{}", xml_tail)?; } Ok(()) } @@ -42,21 +42,21 @@ pub fn output_checkstyle_file(mut writer: T, -> Result<(), io::Error> where T: Write { - try!(write!(writer, "", filename)); + write!(writer, "", filename)?; for mismatch in diff { for line in mismatch.lines { // Do nothing with `DiffLine::Context` and `DiffLine::Resulting`. if let DiffLine::Expected(ref str) = line { let message = xml_escape_str(str); - try!(write!(writer, - "", - mismatch.line_number, - message)); + mismatch.line_number, + message)?; } } } - try!(write!(writer, "")); + write!(writer, "")?; Ok(()) } diff --git a/src/file_lines.rs b/src/file_lines.rs index a20c8c4d3b3..0316ba277e2 100644 --- a/src/file_lines.rs +++ b/src/file_lines.rs @@ -173,8 +173,10 @@ impl str::FromStr for FileLines { type Err = String; fn from_str(s: &str) -> Result { - let v: Vec = try!(json::from_str(s).map_err(|e| e.to_string())); - let m = try!(v.into_iter().map(JsonSpan::into_tuple).collect()); + let v: Vec = json::from_str(s).map_err(|e| e.to_string())?; + let m = v.into_iter() + .map(JsonSpan::into_tuple) + .collect::>()?; Ok(FileLines::from_multimap(m)) } } @@ -190,8 +192,8 @@ impl JsonSpan { // To allow `collect()`ing into a `MultiMap`. fn into_tuple(self) -> Result<(String, Range), String> { let (lo, hi) = self.range; - let canonical = try!(canonicalize_path_string(&self.file) - .map_err(|_| format!("Can't canonicalize {}", &self.file))); + let canonical = canonicalize_path_string(&self.file) + .map_err(|_| format!("Can't canonicalize {}", &self.file))?; Ok((canonical, Range::new(lo, hi))) } } diff --git a/src/filemap.rs b/src/filemap.rs index 6c15d659d54..27f5b990201 100644 --- a/src/filemap.rs +++ b/src/filemap.rs @@ -35,7 +35,7 @@ pub fn write_all_files(file_map: &FileMap, out: &mut T, config: &Config) -> R { output_header(out, config.write_mode).ok(); for &(ref filename, ref text) in file_map { - try!(write_file(text, filename, out, config)); + write_file(text, filename, out, config)?; } output_footer(out, config.write_mode).ok(); @@ -67,9 +67,9 @@ pub fn write_system_newlines(writer: T, NewlineStyle::Windows => { for (c, _) in text.chars() { match c { - '\n' => try!(write!(writer, "\r\n")), + '\n' => write!(writer, "\r\n")?, '\r' => continue, - c => try!(write!(writer, "{}", c)), + c => write!(writer, "{}", c)?, } } Ok(()) @@ -90,11 +90,11 @@ fn source_and_formatted_text(text: &StringBuffer, filename: &str, config: &Config) -> Result<(String, String), io::Error> { - let mut f = try!(File::open(filename)); + let mut f = File::open(filename)?; let mut ori_text = String::new(); - try!(f.read_to_string(&mut ori_text)); + f.read_to_string(&mut ori_text)?; let mut v = Vec::new(); - try!(write_system_newlines(&mut v, text, config)); + write_system_newlines(&mut v, text, config)?; let fmt_text = String::from_utf8(v).unwrap(); Ok((ori_text, fmt_text)) } @@ -103,7 +103,7 @@ fn create_diff(filename: &str, text: &StringBuffer, config: &Config) -> Result, io::Error> { - let (ori, fmt) = try!(source_and_formatted_text(text, filename, config)); + let (ori, fmt) = source_and_formatted_text(text, filename, config)?; Ok(make_diff(&ori, &fmt, 3)) } @@ -118,26 +118,26 @@ fn create_diff(filename: &str, let bk_name = filename.to_owned() + ".bk"; { // Write text to temp file - let tmp_file = try!(File::create(&tmp_name)); - try!(write_system_newlines(tmp_file, text, config)); + let tmp_file = File::create(&tmp_name)?; + write_system_newlines(tmp_file, text, config)?; } - try!(fs::rename(filename, bk_name)); - try!(fs::rename(tmp_name, filename)); + fs::rename(filename, bk_name)?; + fs::rename(tmp_name, filename)?; } } } WriteMode::Overwrite => { // Write text directly over original file. - let file = try!(File::create(filename)); - try!(write_system_newlines(file, text, config)); + let file = File::create(filename)?; + write_system_newlines(file, text, config)?; } WriteMode::Plain => { - try!(write_system_newlines(out, text, config)); + write_system_newlines(out, text, config)?; } WriteMode::Display | WriteMode::Coverage => { println!("{}:\n", filename); - try!(write_system_newlines(out, text, config)); + write_system_newlines(out, text, config)?; } WriteMode::Diff => { if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) { @@ -149,8 +149,8 @@ fn create_diff(filename: &str, } } WriteMode::Checkstyle => { - let diff = try!(create_diff(filename, text, config)); - try!(output_checkstyle_file(out, filename, diff)); + let diff = create_diff(filename, text, config)?; + output_checkstyle_file(out, filename, diff)?; } } diff --git a/src/lib.rs b/src/lib.rs index 13b2df06740..e201c891dc2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -414,13 +414,13 @@ impl fmt::Display for FormatReport { fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { for (file, errors) in &self.file_error_map { for error in errors { - try!(write!(fmt, - "{} {}:{}: {} {}\n", - error.msg_prefix(), - file, - error.line, - error.kind, - error.msg_suffix())); + write!(fmt, + "{} {}:{}: {} {}\n", + error.msg_prefix(), + file, + error.line, + error.kind, + error.msg_suffix())?; } } Ok(()) @@ -454,7 +454,7 @@ fn format_ast(krate: &ast::Crate, let mut visitor = FmtVisitor::from_codemap(parse_session, config); visitor.format_separate_mod(module); - has_diff |= try!(after_file(path, &mut visitor.buffer)); + has_diff |= after_file(path, &mut visitor.buffer)?; result.push((path.to_owned(), visitor.buffer)); } diff --git a/src/utils.rs b/src/utils.rs index efcf0f1b3e3..d7778448b86 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -211,7 +211,7 @@ fn visit_str(self, value: &str) -> Result { Ok(String::from(value)) } } - let s = try!(d.deserialize_string(StringOnly::(PhantomData))); + let s = d.deserialize_string(StringOnly::(PhantomData))?; $( if stringify!($x).eq_ignore_ascii_case(&s) { return Ok($e::$x);