Skip to content
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

Suggest using .display() when trying to print a Path #56761

Merged
merged 2 commits into from
Dec 16, 2018
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,15 @@ pub trait Debug {
/// println!("The origin is: {}", origin);
/// ```
#[rustc_on_unimplemented(
on(
_Self="std::path::Path",
label="`{Self}` cannot be formatted with the default formatter; call `.display()` on it",
note="call `.display()` or `.to_string_lossy()` to safely print paths, \
as they may contain non-Unicode data"
),
message="`{Self}` doesn't implement `{Display}`",
label="`{Self}` cannot be formatted with the default formatter",
note="in format strings you may be able to use `{{:?}}` \
(or {{:#?}} for pretty-print) instead",
note="in format strings you may be able to use `{{:?}}` (or {{:#?}} for pretty-print) instead",
)]
#[doc(alias = "{}")]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/suggestions/path-display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::path::Path;

fn main() {
let path = Path::new("/tmp/foo/bar.txt");
println!("{}", path);
//~^ ERROR E0277
}
14 changes: 14 additions & 0 deletions src/test/ui/suggestions/path-display.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0277]: `std::path::Path` doesn't implement `std::fmt::Display`
--> $DIR/path-display.rs:5:20
|
LL | println!("{}", path);
| ^^^^ `std::path::Path` cannot be formatted with the default formatter; call `.display()` on it
|
= help: the trait `std::fmt::Display` is not implemented for `std::path::Path`
= note: call `.display()` or `.to_string_lossy()` to safely print paths, as they may contain non-Unicode data
= note: required because of the requirements on the impl of `std::fmt::Display` for `&std::path::Path`
= note: required by `std::fmt::Display::fmt`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.