From 8adc3f74f53a59d62ac914e920d5547a3e460924 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 24 Feb 2016 22:34:23 -0500 Subject: Prefer `slice::get` over length check with indexing. --- src/libstd/path.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 35118bde96b..e1393ce6aab 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -226,7 +226,7 @@ mod platform { } _ => (), } - } else if path.len() > 1 && path[1] == b':' { + } else if path.get(1) == Some(b':') { // C: let c = path[0]; if c.is_ascii() && (c as char).is_alphabetic() { -- cgit 1.4.1-3-g733a5 From c82be2f4cb909a70b870ab2fb176e54a91e5372f Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 24 Feb 2016 22:50:23 -0500 Subject: Prefer 'match' pattern guard over conditional within body. --- src/libstd/path.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/path.rs b/src/libstd/path.rs index e1393ce6aab..81d9d0a7f10 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -393,11 +393,8 @@ fn iter_after(mut iter: I, mut prefix: J) -> Option loop { let mut iter_next = iter.clone(); match (iter_next.next(), prefix.next()) { - (Some(x), Some(y)) => { - if x != y { - return None; - } - } + (Some(ref x), Some(ref y)) if x == y => (), + (Some(_), Some(_)) => return None, (Some(_), None) => return Some(iter), (None, None) => return Some(iter), (None, Some(_)) => return None, -- cgit 1.4.1-3-g733a5 From 52ed15fb7c7f3771a8a277c616a75f6ba26f050e Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Thu, 25 Feb 2016 22:52:02 +0200 Subject: doc: add missing comma --- src/libstd/env.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/env.rs b/src/libstd/env.rs index aa6a6d548b3..40f5e4c8345 100644 --- a/src/libstd/env.rs +++ b/src/libstd/env.rs @@ -492,7 +492,7 @@ pub fn temp_dir() -> PathBuf { /// /// Acquiring the path to the current executable is a platform-specific operation /// that can fail for a good number of reasons. Some errors can include, but not -/// be limited to filesystem operations failing or general syscall failures. +/// be limited to, filesystem operations failing or general syscall failures. /// /// # Examples /// -- cgit 1.4.1-3-g733a5 From 6c21b1bad62bdf289196df01a85ceea0703284c8 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Thu, 25 Feb 2016 23:14:20 +0200 Subject: doc: that explanation was a mess --- src/libstd/env.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/env.rs b/src/libstd/env.rs index aa6a6d548b3..be61994ac39 100644 --- a/src/libstd/env.rs +++ b/src/libstd/env.rs @@ -482,8 +482,7 @@ pub fn temp_dir() -> PathBuf { os_imp::temp_dir() } -/// Returns the filesystem path to the current executable which is running but -/// with the executable name. +/// Returns the full filesystem path to the current running executable. /// /// The path returned is not necessarily a "real path" to the executable as /// there may be intermediate symlinks. -- cgit 1.4.1-3-g733a5 From 5eb46d9a01473875dd26f89d2f52ea4b57fa98e5 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Thu, 25 Feb 2016 23:19:47 +0200 Subject: doc: follow the idiom of adding a trailing comma --- src/libstd/env.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/env.rs b/src/libstd/env.rs index aa6a6d548b3..0bf099714d8 100644 --- a/src/libstd/env.rs +++ b/src/libstd/env.rs @@ -442,7 +442,7 @@ impl Error for JoinPathsError { /// /// match env::home_dir() { /// Some(ref p) => println!("{}", p.display()), -/// None => println!("Impossible to get your home dir!") +/// None => println!("Impossible to get your home dir!"), /// } /// ``` #[stable(feature = "env", since = "1.0.0")] -- cgit 1.4.1-3-g733a5 From 6cfafad3c56736a62e1043a8d01f7f2c74384008 Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Fri, 26 Feb 2016 02:53:47 +0100 Subject: Make sure formatter errors are emitted by the default Write::write_fmt Previously, if an error was returned from the formatter that did not originate in an underlying writer error, Write::write_fmt would return successfully even if the formatting did not complete (was interrupted by an `fmt::Error` return). Now we choose to emit an io::Error with kind Other for formatter errors. Since this may reveal error returns from `write!()` and similar that previously passed silently, it's a kind of a [breaking-change]. --- src/libstd/io/mod.rs | 9 +++++- src/test/run-pass/write-fmt-errors.rs | 54 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/test/run-pass/write-fmt-errors.rs (limited to 'src/libstd') diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index abb47b69418..4baf02063a3 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -1055,7 +1055,14 @@ pub trait Write { let mut output = Adaptor { inner: self, error: Ok(()) }; match fmt::write(&mut output, fmt) { Ok(()) => Ok(()), - Err(..) => output.error + Err(..) => { + // check if the error came from the underlying `Write` or not + if output.error.is_err() { + output.error + } else { + Err(Error::new(ErrorKind::Other, "formatter error")) + } + } } } diff --git a/src/test/run-pass/write-fmt-errors.rs b/src/test/run-pass/write-fmt-errors.rs new file mode 100644 index 00000000000..e4439087946 --- /dev/null +++ b/src/test/run-pass/write-fmt-errors.rs @@ -0,0 +1,54 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::fmt; +use std::io::{self, Error, Write, sink}; + +struct ErrorDisplay; + +impl fmt::Display for ErrorDisplay { + fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { + Err(fmt::Error) + } +} + +struct ErrorWriter; + +const FORMAT_ERROR: io::ErrorKind = io::ErrorKind::Other; +const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected; + +impl Write for ErrorWriter { + fn write(&mut self, _buf: &[u8]) -> io::Result { + Err(Error::new(WRITER_ERROR, "not connected")) + } + + fn flush(&mut self) -> io::Result<()> { Ok(()) } +} + +fn main() { + // Test that the error from the formatter is propagated. + let res = write!(sink(), "{} {} {}", 1, ErrorDisplay, "bar"); + assert!(res.is_err(), "formatter error did not propagate"); + assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR); + + // Test that an underlying error is propagated + let res = write!(ErrorWriter, "abc"); + assert!(res.is_err(), "writer error did not propagate"); + + // Writer error + let res = write!(ErrorWriter, "abc {}", ErrorDisplay); + assert!(res.is_err(), "writer error did not propagate"); + assert_eq!(res.unwrap_err().kind(), WRITER_ERROR); + + // Formatter error + let res = write!(ErrorWriter, "{} abc", ErrorDisplay); + assert!(res.is_err(), "formatter error did not propagate"); + assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR); +} -- cgit 1.4.1-3-g733a5 From 3c9a26853cca8783892d5b2373e9de63b5f488b5 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 26 Feb 2016 17:05:46 +0530 Subject: fixup #31878 --- src/libstd/path.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 81d9d0a7f10..94967bfb96a 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -226,7 +226,7 @@ mod platform { } _ => (), } - } else if path.get(1) == Some(b':') { + } else if path.get(1) == Some(& b':') { // C: let c = path[0]; if c.is_ascii() && (c as char).is_alphabetic() { -- cgit 1.4.1-3-g733a5