Skip to content

Commit b76f818

Browse files
committed
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
2 parents 26cfc26 + c063c51 commit b76f818

File tree

147 files changed

+4050
-4046
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+4050
-4046
lines changed

src/compiletest/compiletest.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![feature(rustc_private)]
1616
#![feature(str_char)]
1717
#![feature(test)]
18+
#![feature(question_mark)]
1819

1920
#![deny(warnings)]
2021

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

290-
let dirs = try!(fs::read_dir(dir));
291+
let dirs = fs::read_dir(dir)?;
291292
for file in dirs {
292-
let file = try!(file);
293+
let file = file?;
293294
let file_path = file.path();
294295
debug!("inspecting file {:?}", file_path.display());
295296
if is_test(config, &file_path) {
@@ -310,11 +311,11 @@ fn collect_tests_from_dir(config: &Config,
310311
tests.push(make_test(config, &paths))
311312
} else if file_path.is_dir() {
312313
let relative_file_path = relative_dir_path.join(file.file_name());
313-
try!(collect_tests_from_dir(config,
314-
base,
315-
&file_path,
316-
&relative_file_path,
317-
tests));
314+
collect_tests_from_dir(config,
315+
base,
316+
&file_path,
317+
&relative_file_path,
318+
tests)?;
318319
}
319320
}
320321
Ok(())

src/libcore/fmt/builders.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<'a, 'b: 'a> fmt::Write for PadAdapter<'a, 'b> {
2929
fn write_str(&mut self, mut s: &str) -> fmt::Result {
3030
while !s.is_empty() {
3131
if self.on_newline {
32-
try!(self.fmt.write_str(" "));
32+
self.fmt.write_str(" ")?;
3333
}
3434

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

@@ -169,10 +169,10 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
169169
if self.fields > 0 {
170170
self.result = self.result.and_then(|_| {
171171
if self.is_pretty() {
172-
try!(self.fmt.write_str("\n"));
172+
self.fmt.write_str("\n")?;
173173
}
174174
if self.fields == 1 && self.empty_name {
175-
try!(self.fmt.write_str(","));
175+
self.fmt.write_str(",")?;
176176
}
177177
self.fmt.write_str(")")
178178
});

src/libcore/fmt/mod.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -795,24 +795,24 @@ pub fn write(output: &mut Write, args: Arguments) -> Result {
795795
None => {
796796
// We can use default formatting parameters for all arguments.
797797
for (arg, piece) in args.args.iter().zip(pieces.by_ref()) {
798-
try!(formatter.buf.write_str(*piece));
799-
try!((arg.formatter)(arg.value, &mut formatter));
798+
formatter.buf.write_str(*piece)?;
799+
(arg.formatter)(arg.value, &mut formatter)?;
800800
}
801801
}
802802
Some(fmt) => {
803803
// Every spec has a corresponding argument that is preceded by
804804
// a string piece.
805805
for (arg, piece) in fmt.iter().zip(pieces.by_ref()) {
806-
try!(formatter.buf.write_str(*piece));
807-
try!(formatter.run(arg));
806+
formatter.buf.write_str(*piece)?;
807+
formatter.run(arg)?;
808808
}
809809
}
810810
}
811811

812812
// There can be only one trailing string piece left.
813813
match pieces.next() {
814814
Some(piece) => {
815-
try!(formatter.buf.write_str(*piece));
815+
formatter.buf.write_str(*piece)?;
816816
}
817817
None => {}
818818
}
@@ -897,9 +897,9 @@ impl<'a> Formatter<'a> {
897897
// Writes the sign if it exists, and then the prefix if it was requested
898898
let write_prefix = |f: &mut Formatter| {
899899
if let Some(c) = sign {
900-
try!(f.buf.write_str(unsafe {
900+
f.buf.write_str(unsafe {
901901
str::from_utf8_unchecked(c.encode_utf8().as_slice())
902-
}));
902+
})?;
903903
}
904904
if prefixed { f.buf.write_str(prefix) }
905905
else { Ok(()) }
@@ -910,26 +910,26 @@ impl<'a> Formatter<'a> {
910910
// If there's no minimum length requirements then we can just
911911
// write the bytes.
912912
None => {
913-
try!(write_prefix(self)); self.buf.write_str(buf)
913+
write_prefix(self)?; self.buf.write_str(buf)
914914
}
915915
// Check if we're over the minimum width, if so then we can also
916916
// just write the bytes.
917917
Some(min) if width >= min => {
918-
try!(write_prefix(self)); self.buf.write_str(buf)
918+
write_prefix(self)?; self.buf.write_str(buf)
919919
}
920920
// The sign and prefix goes before the padding if the fill character
921921
// is zero
922922
Some(min) if self.sign_aware_zero_pad() => {
923923
self.fill = '0';
924-
try!(write_prefix(self));
924+
write_prefix(self)?;
925925
self.with_padding(min - width, rt::v1::Alignment::Right, |f| {
926926
f.buf.write_str(buf)
927927
})
928928
}
929929
// Otherwise, the sign and prefix goes after the padding
930930
Some(min) => {
931931
self.with_padding(min - width, rt::v1::Alignment::Right, |f| {
932-
try!(write_prefix(f)); f.buf.write_str(buf)
932+
write_prefix(f)?; f.buf.write_str(buf)
933933
})
934934
}
935935
}
@@ -1008,13 +1008,13 @@ impl<'a> Formatter<'a> {
10081008
};
10091009

10101010
for _ in 0..pre_pad {
1011-
try!(self.buf.write_str(fill));
1011+
self.buf.write_str(fill)?;
10121012
}
10131013

1014-
try!(f(self));
1014+
f(self)?;
10151015

10161016
for _ in 0..post_pad {
1017-
try!(self.buf.write_str(fill));
1017+
self.buf.write_str(fill)?;
10181018
}
10191019

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

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

10671067
if !formatted.sign.is_empty() {
1068-
try!(write_bytes(self.buf, formatted.sign));
1068+
write_bytes(self.buf, formatted.sign)?;
10691069
}
10701070
for part in formatted.parts {
10711071
match *part {
10721072
flt2dec::Part::Zero(mut nzeroes) => {
10731073
const ZEROES: &'static str = // 64 zeroes
10741074
"0000000000000000000000000000000000000000000000000000000000000000";
10751075
while nzeroes > ZEROES.len() {
1076-
try!(self.buf.write_str(ZEROES));
1076+
self.buf.write_str(ZEROES)?;
10771077
nzeroes -= ZEROES.len();
10781078
}
10791079
if nzeroes > 0 {
1080-
try!(self.buf.write_str(&ZEROES[..nzeroes]));
1080+
self.buf.write_str(&ZEROES[..nzeroes])?;
10811081
}
10821082
}
10831083
flt2dec::Part::Num(mut v) => {
@@ -1087,10 +1087,10 @@ impl<'a> Formatter<'a> {
10871087
*c = b'0' + (v % 10) as u8;
10881088
v /= 10;
10891089
}
1090-
try!(write_bytes(self.buf, &s[..len]));
1090+
write_bytes(self.buf, &s[..len])?;
10911091
}
10921092
flt2dec::Part::Copy(buf) => {
1093-
try!(write_bytes(self.buf, buf));
1093+
write_bytes(self.buf, buf)?;
10941094
}
10951095
}
10961096
}
@@ -1349,20 +1349,20 @@ impl Display for bool {
13491349
#[stable(feature = "rust1", since = "1.0.0")]
13501350
impl Debug for str {
13511351
fn fmt(&self, f: &mut Formatter) -> Result {
1352-
try!(f.write_char('"'));
1352+
f.write_char('"')?;
13531353
let mut from = 0;
13541354
for (i, c) in self.char_indices() {
13551355
let esc = c.escape_default();
13561356
// If char needs escaping, flush backlog so far and write, else skip
13571357
if esc.size_hint() != (1, Some(1)) {
1358-
try!(f.write_str(&self[from..i]));
1358+
f.write_str(&self[from..i])?;
13591359
for c in esc {
1360-
try!(f.write_char(c));
1360+
f.write_char(c)?;
13611361
}
13621362
from = i + c.len_utf8();
13631363
}
13641364
}
1365-
try!(f.write_str(&self[from..]));
1365+
f.write_str(&self[from..])?;
13661366
f.write_char('"')
13671367
}
13681368
}
@@ -1377,9 +1377,9 @@ impl Display for str {
13771377
#[stable(feature = "rust1", since = "1.0.0")]
13781378
impl Debug for char {
13791379
fn fmt(&self, f: &mut Formatter) -> Result {
1380-
try!(f.write_char('\''));
1380+
f.write_char('\'')?;
13811381
for c in self.escape_default() {
1382-
try!(f.write_char(c))
1382+
f.write_char(c)?
13831383
}
13841384
f.write_char('\'')
13851385
}

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#![feature(rustc_attrs)]
7878
#![feature(staged_api)]
7979
#![feature(unboxed_closures)]
80+
#![feature(question_mark)]
8081

8182
#[macro_use]
8283
mod macros;

src/libcore/num/dec2flt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ fn dec2flt<T: RawFloat>(s: &str) -> Result<T, ParseFloatError> {
214214
}
215215
let (sign, s) = extract_sign(s);
216216
let flt = match parse_decimal(s) {
217-
ParseResult::Valid(decimal) => try!(convert(decimal)),
217+
ParseResult::Valid(decimal) => convert(decimal)?,
218218
ParseResult::ShortcutToInf => T::infinity(),
219219
ParseResult::ShortcutToZero => T::zero(),
220220
ParseResult::Invalid => match s {

src/libcore/str/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl Utf8Error {
240240
/// ```
241241
#[stable(feature = "rust1", since = "1.0.0")]
242242
pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
243-
try!(run_utf8_validation(v));
243+
run_utf8_validation(v)?;
244244
Ok(unsafe { from_utf8_unchecked(v) })
245245
}
246246

src/libgraphviz/lib.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@
295295
#![cfg_attr(not(stage0), deny(warnings))]
296296

297297
#![feature(str_escape)]
298+
#![feature(question_mark)]
298299

299300
use self::LabelText::*;
300301

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

674-
try!(writeln(w, &["digraph ", g.graph_id().as_slice(), " {"]));
675+
writeln(w, &["digraph ", g.graph_id().as_slice(), " {"])?;
675676
for n in g.nodes().iter() {
676-
try!(indent(w));
677+
indent(w)?;
677678
let id = g.node_id(n);
678679

679680
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,
702703
}
703704

704705
text.push(";");
705-
try!(writeln(w, &text));
706+
writeln(w, &text)?;
706707
}
707708

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

731732
text.push(";");
732-
try!(writeln(w, &text));
733+
writeln(w, &text)?;
733734
}
734735

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

0 commit comments

Comments
 (0)