Auto merge of #32390 - japaric:untry, r=pnkfelix

convert 99.9% of `try!`s to `?`s

The first commit is an automated conversion using the [untry] tool and the following command:

```
$ find -name '*.rs' -type f | xargs untry
```

at the root of the Rust repo.

[untry]: https://github.com/japaric/untry

cc @rust-lang/lang @alexcrichton @brson
This commit is contained in:
bors
2016-03-23 08:59:10 -07:00
147 changed files with 4037 additions and 4033 deletions

View File

@@ -9,6 +9,7 @@
// except according to those terms.
#![feature(rustc_private, rustdoc)]
#![feature(question_mark)]
extern crate syntax;
extern crate rustdoc;
@@ -56,7 +57,7 @@ struct MarkdownFormatter;
impl Formatter for HTMLFormatter {
fn header(&self, output: &mut Write) -> Result<(), Box<Error>> {
try!(write!(output, r##"<!DOCTYPE html>
write!(output, r##"<!DOCTYPE html>
<html>
<head>
<title>Rust Compiler Error Index</title>
@@ -71,12 +72,12 @@ impl Formatter for HTMLFormatter {
</style>
</head>
<body>
"##));
"##)?;
Ok(())
}
fn title(&self, output: &mut Write) -> Result<(), Box<Error>> {
try!(write!(output, "<h1>Rust Compiler Error Index</h1>\n"));
write!(output, "<h1>Rust Compiler Error Index</h1>\n")?;
Ok(())
}
@@ -91,25 +92,25 @@ impl Formatter for HTMLFormatter {
Some(_) => "error-used",
None => "error-unused",
};
try!(write!(output, "<div class=\"{} {}\">", desc_desc, use_desc));
write!(output, "<div class=\"{} {}\">", desc_desc, use_desc)?;
// Error title (with self-link).
try!(write!(output,
"<h2 id=\"{0}\" class=\"section-header\"><a href=\"#{0}\">{0}</a></h2>\n",
err_code));
write!(output,
"<h2 id=\"{0}\" class=\"section-header\"><a href=\"#{0}\">{0}</a></h2>\n",
err_code)?;
// Description rendered as markdown.
match info.description {
Some(ref desc) => try!(write!(output, "{}", Markdown(desc))),
None => try!(write!(output, "<p>No description.</p>\n")),
Some(ref desc) => write!(output, "{}", Markdown(desc))?,
None => write!(output, "<p>No description.</p>\n")?,
}
try!(write!(output, "</div>\n"));
write!(output, "</div>\n")?;
Ok(())
}
fn footer(&self, output: &mut Write) -> Result<(), Box<Error>> {
try!(write!(output, "</body>\n</html>"));
write!(output, "</body>\n</html>")?;
Ok(())
}
}
@@ -121,14 +122,14 @@ impl Formatter for MarkdownFormatter {
}
fn title(&self, output: &mut Write) -> Result<(), Box<Error>> {
try!(write!(output, "# Rust Compiler Error Index\n"));
write!(output, "# Rust Compiler Error Index\n")?;
Ok(())
}
fn error_code_block(&self, output: &mut Write, info: &ErrorMetadata,
err_code: &str) -> Result<(), Box<Error>> {
Ok(match info.description {
Some(ref desc) => try!(write!(output, "## {}\n{}\n", err_code, desc)),
Some(ref desc) => write!(output, "## {}\n{}\n", err_code, desc)?,
None => (),
})
}
@@ -143,13 +144,13 @@ impl Formatter for MarkdownFormatter {
fn load_all_errors(metadata_dir: &Path) -> Result<ErrorMetadataMap, Box<Error>> {
let mut all_errors = BTreeMap::new();
for entry in try!(read_dir(metadata_dir)) {
let path = try!(entry).path();
for entry in read_dir(metadata_dir)? {
let path = entry?.path();
let mut metadata_str = String::new();
try!(File::open(&path).and_then(|mut f| f.read_to_string(&mut metadata_str)));
File::open(&path).and_then(|mut f| f.read_to_string(&mut metadata_str))?;
let some_errors: ErrorMetadataMap = try!(json::decode(&metadata_str));
let some_errors: ErrorMetadataMap = json::decode(&metadata_str)?;
for (err_code, info) in some_errors {
all_errors.insert(err_code, info);
@@ -162,26 +163,26 @@ fn load_all_errors(metadata_dir: &Path) -> Result<ErrorMetadataMap, Box<Error>>
/// Output an HTML page for the errors in `err_map` to `output_path`.
fn render_error_page<T: Formatter>(err_map: &ErrorMetadataMap, output_path: &Path,
formatter: T) -> Result<(), Box<Error>> {
let mut output_file = try!(File::create(output_path));
let mut output_file = File::create(output_path)?;
try!(formatter.header(&mut output_file));
try!(formatter.title(&mut output_file));
formatter.header(&mut output_file)?;
formatter.title(&mut output_file)?;
for (err_code, info) in err_map {
try!(formatter.error_code_block(&mut output_file, info, err_code));
formatter.error_code_block(&mut output_file, info, err_code)?;
}
formatter.footer(&mut output_file)
}
fn main_with_result(format: OutputFormat, dst: &Path) -> Result<(), Box<Error>> {
let build_arch = try!(env::var("CFG_BUILD"));
let build_arch = env::var("CFG_BUILD")?;
let metadata_dir = get_metadata_dir(&build_arch);
let err_map = try!(load_all_errors(&metadata_dir));
let err_map = load_all_errors(&metadata_dir)?;
match format {
OutputFormat::Unknown(s) => panic!("Unknown output format: {}", s),
OutputFormat::HTML(h) => try!(render_error_page(&err_map, dst, h)),
OutputFormat::Markdown(m) => try!(render_error_page(&err_map, dst, m)),
OutputFormat::HTML(h) => render_error_page(&err_map, dst, h)?,
OutputFormat::Markdown(m) => render_error_page(&err_map, dst, m)?,
}
Ok(())
}

View File

@@ -41,7 +41,7 @@ fn write_toc(book: &Book, current_page: &BookItem, out: &mut Write) -> io::Resul
current_page: &BookItem,
out: &mut Write) -> io::Result<()> {
for (i, item) in items.iter().enumerate() {
try!(walk_item(item, &format!("{}{}.", section, i + 1)[..], current_page, out));
walk_item(item, &format!("{}{}.", section, i + 1)[..], current_page, out)?;
}
Ok(())
}
@@ -55,32 +55,32 @@ fn write_toc(book: &Book, current_page: &BookItem, out: &mut Write) -> io::Resul
""
};
try!(writeln!(out, "<li><a {} href='{}'><b>{}</b> {}</a>",
class_string,
current_page.path_to_root.join(&item.path).with_extension("html").display(),
section,
item.title));
writeln!(out, "<li><a {} href='{}'><b>{}</b> {}</a>",
class_string,
current_page.path_to_root.join(&item.path).with_extension("html").display(),
section,
item.title)?;
if !item.children.is_empty() {
try!(writeln!(out, "<ul class='section'>"));
writeln!(out, "<ul class='section'>")?;
let _ = walk_items(&item.children[..], section, current_page, out);
try!(writeln!(out, "</ul>"));
writeln!(out, "</ul>")?;
}
try!(writeln!(out, "</li>"));
writeln!(out, "</li>")?;
Ok(())
}
try!(writeln!(out, "<div id='toc' class='mobile-hidden'>"));
try!(writeln!(out, "<ul class='chapter'>"));
try!(walk_items(&book.chapters[..], "", &current_page, out));
try!(writeln!(out, "</ul>"));
try!(writeln!(out, "</div>"));
writeln!(out, "<div id='toc' class='mobile-hidden'>")?;
writeln!(out, "<ul class='chapter'>")?;
walk_items(&book.chapters[..], "", &current_page, out)?;
writeln!(out, "</ul>")?;
writeln!(out, "</div>")?;
Ok(())
}
fn render(book: &Book, tgt: &Path) -> CliResult<()> {
let tmp = try!(TempDir::new("rustbook"));
let tmp = TempDir::new("rustbook")?;
for (_section, item) in book.iter() {
let out_path = match item.path.parent() {
@@ -97,22 +97,22 @@ fn render(book: &Book, tgt: &Path) -> CliResult<()> {
// preprocess the markdown, rerouting markdown references to html
// references
let mut markdown_data = String::new();
try!(File::open(&src.join(&item.path)).and_then(|mut f| {
File::open(&src.join(&item.path)).and_then(|mut f| {
f.read_to_string(&mut markdown_data)
}));
})?;
let preprocessed_path = tmp.path().join(item.path.file_name().unwrap());
{
let urls = markdown_data.replace(".md)", ".html)");
try!(File::create(&preprocessed_path).and_then(|mut f| {
File::create(&preprocessed_path).and_then(|mut f| {
f.write_all(urls.as_bytes())
}));
})?;
}
// write the prelude to a temporary HTML file for rustdoc inclusion
let prelude = tmp.path().join("prelude.html");
{
let mut buffer = BufWriter::new(try!(File::create(&prelude)));
try!(writeln!(&mut buffer, r#"
let mut buffer = BufWriter::new(File::create(&prelude)?);
writeln!(&mut buffer, r#"
<div id="nav">
<button id="toggle-nav">
<span class="sr-only">Toggle navigation</span>
@@ -120,22 +120,22 @@ fn render(book: &Book, tgt: &Path) -> CliResult<()> {
<span class="bar"></span>
<span class="bar"></span>
</button>
</div>"#));
</div>"#)?;
let _ = write_toc(book, &item, &mut buffer);
try!(writeln!(&mut buffer, "<div id='page-wrapper'>"));
try!(writeln!(&mut buffer, "<div id='page'>"));
writeln!(&mut buffer, "<div id='page-wrapper'>")?;
writeln!(&mut buffer, "<div id='page'>")?;
}
// write the postlude to a temporary HTML file for rustdoc inclusion
let postlude = tmp.path().join("postlude.html");
{
let mut buffer = BufWriter::new(try!(File::create(&postlude)));
try!(writeln!(&mut buffer, "<script src='rustbook.js'></script>"));
try!(writeln!(&mut buffer, "<script src='playpen.js'></script>"));
try!(writeln!(&mut buffer, "</div></div>"));
let mut buffer = BufWriter::new(File::create(&postlude)?);
writeln!(&mut buffer, "<script src='rustbook.js'></script>")?;
writeln!(&mut buffer, "<script src='playpen.js'></script>")?;
writeln!(&mut buffer, "</div></div>")?;
}
try!(fs::create_dir_all(&out_path));
fs::create_dir_all(&out_path)?;
let rustdoc_args: &[String] = &[
"".to_string(),
@@ -156,12 +156,12 @@ fn render(book: &Book, tgt: &Path) -> CliResult<()> {
}
// create index.html from the root README
try!(fs::copy(&tgt.join("README.html"), &tgt.join("index.html")));
fs::copy(&tgt.join("README.html"), &tgt.join("index.html"))?;
// Copy js for playpen
let mut playpen = try!(File::create(tgt.join("playpen.js")));
let mut playpen = File::create(tgt.join("playpen.js"))?;
let js = include_bytes!("../../librustdoc/html/static/playpen.js");
try!(playpen.write_all(js));
playpen.write_all(js)?;
Ok(())
}
@@ -189,24 +189,24 @@ impl Subcommand for Build {
// `_book` directory may already exist from previous runs. Check and
// delete it if it exists.
for entry in try!(fs::read_dir(&cwd)) {
let path = try!(entry).path();
if path == tgt { try!(fs::remove_dir_all(&tgt)) }
for entry in fs::read_dir(&cwd)? {
let path = entry?.path();
if path == tgt { fs::remove_dir_all(&tgt)? }
}
try!(fs::create_dir(&tgt));
fs::create_dir(&tgt)?;
// Copy static files
let css = include_bytes!("static/rustbook.css");
let js = include_bytes!("static/rustbook.js");
let mut css_file = try!(File::create(tgt.join("rustbook.css")));
try!(css_file.write_all(css));
let mut css_file = File::create(tgt.join("rustbook.css"))?;
css_file.write_all(css)?;
let mut js_file = try!(File::create(tgt.join("rustbook.js")));
try!(js_file.write_all(js));
let mut js_file = File::create(tgt.join("rustbook.js"))?;
js_file.write_all(js)?;
let mut summary = try!(File::open(&src.join("SUMMARY.md")));
let mut summary = File::open(&src.join("SUMMARY.md"))?;
match book::parse_summary(&mut summary, &src) {
Ok(book) => {
// execute rustdoc on the whole book

View File

@@ -13,6 +13,7 @@
#![feature(iter_arith)]
#![feature(rustc_private)]
#![feature(rustdoc)]
#![feature(question_mark)]
extern crate rustdoc;
extern crate rustc_back;

View File

@@ -38,7 +38,7 @@ impl Subcommand for Test {
let cwd = env::current_dir().unwrap();
let src = cwd.clone();
let mut summary = try!(File::open(&src.join("SUMMARY.md")));
let mut summary = File::open(&src.join("SUMMARY.md"))?;
match book::parse_summary(&mut summary, &src) {
Ok(book) => {
for (_, item) in book.iter() {