Skip to content

convert 99.9% of try!s to ?s #32390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 23, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/compiletest/compiletest.rs
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
#![feature(rustc_private)]
#![feature(str_char)]
#![feature(test)]
#![feature(question_mark)]

#![deny(warnings)]

@@ -280,16 +281,16 @@ fn collect_tests_from_dir(config: &Config,
-> io::Result<()> {
// Ignore directories that contain a file
// `compiletest-ignore-dir`.
for file in try!(fs::read_dir(dir)) {
let file = try!(file);
for file in fs::read_dir(dir)? {
let file = file?;
if file.file_name() == *"compiletest-ignore-dir" {
return Ok(());
}
}

let dirs = try!(fs::read_dir(dir));
let dirs = fs::read_dir(dir)?;
for file in dirs {
let file = try!(file);
let file = file?;
let file_path = file.path();
debug!("inspecting file {:?}", file_path.display());
if is_test(config, &file_path) {
@@ -310,11 +311,11 @@ fn collect_tests_from_dir(config: &Config,
tests.push(make_test(config, &paths))
} else if file_path.is_dir() {
let relative_file_path = relative_dir_path.join(file.file_name());
try!(collect_tests_from_dir(config,
base,
&file_path,
&relative_file_path,
tests));
collect_tests_from_dir(config,
base,
&file_path,
&relative_file_path,
tests)?;
}
}
Ok(())
8 changes: 4 additions & 4 deletions src/libcore/fmt/builders.rs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ impl<'a, 'b: 'a> fmt::Write for PadAdapter<'a, 'b> {
fn write_str(&mut self, mut s: &str) -> fmt::Result {
while !s.is_empty() {
if self.on_newline {
try!(self.fmt.write_str(" "));
self.fmt.write_str(" ")?;
}

let split = match s.find('\n') {
@@ -42,7 +42,7 @@ impl<'a, 'b: 'a> fmt::Write for PadAdapter<'a, 'b> {
s.len()
}
};
try!(self.fmt.write_str(&s[..split]));
self.fmt.write_str(&s[..split])?;
s = &s[split..];
}

@@ -169,10 +169,10 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
if self.fields > 0 {
self.result = self.result.and_then(|_| {
if self.is_pretty() {
try!(self.fmt.write_str("\n"));
self.fmt.write_str("\n")?;
}
if self.fields == 1 && self.empty_name {
try!(self.fmt.write_str(","));
self.fmt.write_str(",")?;
}
self.fmt.write_str(")")
});
52 changes: 26 additions & 26 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
@@ -795,24 +795,24 @@ pub fn write(output: &mut Write, args: Arguments) -> Result {
None => {
// We can use default formatting parameters for all arguments.
for (arg, piece) in args.args.iter().zip(pieces.by_ref()) {
try!(formatter.buf.write_str(*piece));
try!((arg.formatter)(arg.value, &mut formatter));
formatter.buf.write_str(*piece)?;
(arg.formatter)(arg.value, &mut formatter)?;
}
}
Some(fmt) => {
// Every spec has a corresponding argument that is preceded by
// a string piece.
for (arg, piece) in fmt.iter().zip(pieces.by_ref()) {
try!(formatter.buf.write_str(*piece));
try!(formatter.run(arg));
formatter.buf.write_str(*piece)?;
formatter.run(arg)?;
}
}
}

// There can be only one trailing string piece left.
match pieces.next() {
Some(piece) => {
try!(formatter.buf.write_str(*piece));
formatter.buf.write_str(*piece)?;
}
None => {}
}
@@ -897,9 +897,9 @@ impl<'a> Formatter<'a> {
// Writes the sign if it exists, and then the prefix if it was requested
let write_prefix = |f: &mut Formatter| {
if let Some(c) = sign {
try!(f.buf.write_str(unsafe {
f.buf.write_str(unsafe {
str::from_utf8_unchecked(c.encode_utf8().as_slice())
}));
})?;
}
if prefixed { f.buf.write_str(prefix) }
else { Ok(()) }
@@ -910,26 +910,26 @@ impl<'a> Formatter<'a> {
// If there's no minimum length requirements then we can just
// write the bytes.
None => {
try!(write_prefix(self)); self.buf.write_str(buf)
write_prefix(self)?; self.buf.write_str(buf)
}
// Check if we're over the minimum width, if so then we can also
// just write the bytes.
Some(min) if width >= min => {
try!(write_prefix(self)); self.buf.write_str(buf)
write_prefix(self)?; self.buf.write_str(buf)
}
// The sign and prefix goes before the padding if the fill character
// is zero
Some(min) if self.sign_aware_zero_pad() => {
self.fill = '0';
try!(write_prefix(self));
write_prefix(self)?;
self.with_padding(min - width, rt::v1::Alignment::Right, |f| {
f.buf.write_str(buf)
})
}
// Otherwise, the sign and prefix goes after the padding
Some(min) => {
self.with_padding(min - width, rt::v1::Alignment::Right, |f| {
try!(write_prefix(f)); f.buf.write_str(buf)
write_prefix(f)?; f.buf.write_str(buf)
})
}
}
@@ -1008,13 +1008,13 @@ impl<'a> Formatter<'a> {
};

for _ in 0..pre_pad {
try!(self.buf.write_str(fill));
self.buf.write_str(fill)?;
}

try!(f(self));
f(self)?;

for _ in 0..post_pad {
try!(self.buf.write_str(fill));
self.buf.write_str(fill)?;
}

Ok(())
@@ -1033,7 +1033,7 @@ impl<'a> Formatter<'a> {
if self.sign_aware_zero_pad() {
// a sign always goes first
let sign = unsafe { str::from_utf8_unchecked(formatted.sign) };
try!(self.buf.write_str(sign));
self.buf.write_str(sign)?;

// remove the sign from the formatted parts
formatted.sign = b"";
@@ -1065,19 +1065,19 @@ impl<'a> Formatter<'a> {
}

if !formatted.sign.is_empty() {
try!(write_bytes(self.buf, formatted.sign));
write_bytes(self.buf, formatted.sign)?;
}
for part in formatted.parts {
match *part {
flt2dec::Part::Zero(mut nzeroes) => {
const ZEROES: &'static str = // 64 zeroes
"0000000000000000000000000000000000000000000000000000000000000000";
while nzeroes > ZEROES.len() {
try!(self.buf.write_str(ZEROES));
self.buf.write_str(ZEROES)?;
nzeroes -= ZEROES.len();
}
if nzeroes > 0 {
try!(self.buf.write_str(&ZEROES[..nzeroes]));
self.buf.write_str(&ZEROES[..nzeroes])?;
}
}
flt2dec::Part::Num(mut v) => {
@@ -1087,10 +1087,10 @@ impl<'a> Formatter<'a> {
*c = b'0' + (v % 10) as u8;
v /= 10;
}
try!(write_bytes(self.buf, &s[..len]));
write_bytes(self.buf, &s[..len])?;
}
flt2dec::Part::Copy(buf) => {
try!(write_bytes(self.buf, buf));
write_bytes(self.buf, buf)?;
}
}
}
@@ -1349,20 +1349,20 @@ impl Display for bool {
#[stable(feature = "rust1", since = "1.0.0")]
impl Debug for str {
fn fmt(&self, f: &mut Formatter) -> Result {
try!(f.write_char('"'));
f.write_char('"')?;
let mut from = 0;
for (i, c) in self.char_indices() {
let esc = c.escape_default();
// If char needs escaping, flush backlog so far and write, else skip
if esc.size_hint() != (1, Some(1)) {
try!(f.write_str(&self[from..i]));
f.write_str(&self[from..i])?;
for c in esc {
try!(f.write_char(c));
f.write_char(c)?;
}
from = i + c.len_utf8();
}
}
try!(f.write_str(&self[from..]));
f.write_str(&self[from..])?;
f.write_char('"')
}
}
@@ -1377,9 +1377,9 @@ impl Display for str {
#[stable(feature = "rust1", since = "1.0.0")]
impl Debug for char {
fn fmt(&self, f: &mut Formatter) -> Result {
try!(f.write_char('\''));
f.write_char('\'')?;
for c in self.escape_default() {
try!(f.write_char(c))
f.write_char(c)?
}
f.write_char('\'')
}
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@
#![feature(rustc_attrs)]
#![feature(staged_api)]
#![feature(unboxed_closures)]
#![feature(question_mark)]

#[macro_use]
mod macros;
2 changes: 1 addition & 1 deletion src/libcore/num/dec2flt/mod.rs
Original file line number Diff line number Diff line change
@@ -214,7 +214,7 @@ fn dec2flt<T: RawFloat>(s: &str) -> Result<T, ParseFloatError> {
}
let (sign, s) = extract_sign(s);
let flt = match parse_decimal(s) {
ParseResult::Valid(decimal) => try!(convert(decimal)),
ParseResult::Valid(decimal) => convert(decimal)?,
ParseResult::ShortcutToInf => T::infinity(),
ParseResult::ShortcutToZero => T::zero(),
ParseResult::Invalid => match s {
2 changes: 1 addition & 1 deletion src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
@@ -240,7 +240,7 @@ impl Utf8Error {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
try!(run_utf8_validation(v));
run_utf8_validation(v)?;
Ok(unsafe { from_utf8_unchecked(v) })
}

15 changes: 8 additions & 7 deletions src/libgraphviz/lib.rs
Original file line number Diff line number Diff line change
@@ -295,6 +295,7 @@
#![cfg_attr(not(stage0), deny(warnings))]

#![feature(str_escape)]
#![feature(question_mark)]

use self::LabelText::*;

@@ -662,7 +663,7 @@ pub fn render_opts<'a, N, E, G, W>(g: &'a G,
{
fn writeln<W: Write>(w: &mut W, arg: &[&str]) -> io::Result<()> {
for &s in arg {
try!(w.write_all(s.as_bytes()));
w.write_all(s.as_bytes())?;
}
write!(w, "\n")
}
@@ -671,9 +672,9 @@ pub fn render_opts<'a, N, E, G, W>(g: &'a G,
w.write_all(b" ")
}

try!(writeln(w, &["digraph ", g.graph_id().as_slice(), " {"]));
writeln(w, &["digraph ", g.graph_id().as_slice(), " {"])?;
for n in g.nodes().iter() {
try!(indent(w));
indent(w)?;
let id = g.node_id(n);

let escaped = &g.node_label(n).to_dot_string();
@@ -702,12 +703,12 @@ pub fn render_opts<'a, N, E, G, W>(g: &'a G,
}

text.push(";");
try!(writeln(w, &text));
writeln(w, &text)?;
}

for e in g.edges().iter() {
let escaped_label = &g.edge_label(e).to_dot_string();
try!(indent(w));
indent(w)?;
let source = g.source(e);
let target = g.target(e);
let source_id = g.node_id(&source);
@@ -729,7 +730,7 @@ pub fn render_opts<'a, N, E, G, W>(g: &'a G,
}

text.push(";");
try!(writeln(w, &text));
writeln(w, &text)?;
}

writeln(w, &["}"])
@@ -959,7 +960,7 @@ mod tests {
let mut writer = Vec::new();
render(&g, &mut writer).unwrap();
let mut s = String::new();
try!(Read::read_to_string(&mut &*writer, &mut s));
Read::read_to_string(&mut &*writer, &mut s)?;
Ok(s)
}

Loading