Merge pull request #1516 from topecongiro/remove-try
Replace 'try!' with '?'
This commit is contained in:
commit
56bd0662c1
@ -105,7 +105,7 @@ pub enum Verbosity {
|
||||
fn format_crate(verbosity: Verbosity,
|
||||
workspace_hitlist: WorkspaceHitlist)
|
||||
-> Result<ExitStatus, std::io::Error> {
|
||||
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<Vec<Target>, std::io::Error> {
|
||||
let mut targets: Vec<Target> = 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()
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
|
||||
}
|
||||
|
||||
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<Option<PathBuf>> {
|
||||
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<Option<PathBuf>> {
|
||||
}
|
||||
|
||||
fn open_config_file(file_path: &Path) -> FmtResult<(Config, Option<PathBuf>)> {
|
||||
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<PathBuf>)> {
|
||||
/// Returns the `Config` to use, and the path of the project file if there was
|
||||
/// one.
|
||||
fn resolve_config(dir: &Path) -> FmtResult<(Config, Option<PathBuf>)> {
|
||||
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<PathBuf>,
|
||||
-> FmtResult<(Config, Option<PathBuf>)> {
|
||||
|
||||
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<Summary> {
|
||||
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<Summary> {
|
||||
|
||||
// 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<Summary> {
|
||||
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<Operation> {
|
||||
// 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,
|
||||
|
@ -20,7 +20,7 @@ pub fn output_header<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
|
||||
xml_heading.push_str("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
|
||||
xml_heading.push_str("\n");
|
||||
xml_heading.push_str("<checkstyle version=\"4.3\">");
|
||||
try!(write!(out, "{}", xml_heading));
|
||||
write!(out, "{}", xml_heading)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -31,7 +31,7 @@ pub fn output_footer<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
|
||||
if mode == WriteMode::Checkstyle {
|
||||
let mut xml_tail = String::new();
|
||||
xml_tail.push_str("</checkstyle>");
|
||||
try!(write!(out, "{}", xml_tail));
|
||||
write!(out, "{}", xml_tail)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -42,21 +42,21 @@ pub fn output_checkstyle_file<T>(mut writer: T,
|
||||
-> Result<(), io::Error>
|
||||
where T: Write
|
||||
{
|
||||
try!(write!(writer, "<file name=\"{}\">", filename));
|
||||
write!(writer, "<file name=\"{}\">", 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,
|
||||
"<error line=\"{}\" severity=\"warning\" message=\"Should be `{}`\" \
|
||||
write!(writer,
|
||||
"<error line=\"{}\" severity=\"warning\" message=\"Should be `{}`\" \
|
||||
/>",
|
||||
mismatch.line_number,
|
||||
message));
|
||||
mismatch.line_number,
|
||||
message)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
try!(write!(writer, "</file>"));
|
||||
write!(writer, "</file>")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -173,8 +173,10 @@ impl str::FromStr for FileLines {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<FileLines, String> {
|
||||
let v: Vec<JsonSpan> = 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<JsonSpan> = json::from_str(s).map_err(|e| e.to_string())?;
|
||||
let m = v.into_iter()
|
||||
.map(JsonSpan::into_tuple)
|
||||
.collect::<Result<_, _>>()?;
|
||||
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)))
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ pub fn write_all_files<T>(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<T>(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<Vec<Mismatch>, 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)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
16
src/lib.rs
16
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<F>(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));
|
||||
}
|
||||
|
@ -211,7 +211,7 @@ fn visit_str<E>(self, value: &str) -> Result<String, E> {
|
||||
Ok(String::from(value))
|
||||
}
|
||||
}
|
||||
let s = try!(d.deserialize_string(StringOnly::<D>(PhantomData)));
|
||||
let s = d.deserialize_string(StringOnly::<D>(PhantomData))?;
|
||||
$(
|
||||
if stringify!($x).eq_ignore_ascii_case(&s) {
|
||||
return Ok($e::$x);
|
||||
|
Loading…
Reference in New Issue
Block a user