Skip to content

Commit c7f1b97

Browse files
committed
Suggest using .display() when trying to print a Path
1 parent 0076f58 commit c7f1b97

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/libcore/fmt/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,15 @@ pub trait Debug {
609609
/// println!("The origin is: {}", origin);
610610
/// ```
611611
#[rustc_on_unimplemented(
612+
on(
613+
_Self="std::path::Path",
614+
label="`{Self}` cannot be formatted with the default formatter, call `.display()` on it",
615+
note="you need to call `.display()` or `.to_string_lossy()` for safely printing paths as \
616+
they may contain non-Unicode data"
617+
),
612618
message="`{Self}` doesn't implement `{Display}`",
613619
label="`{Self}` cannot be formatted with the default formatter",
614-
note="in format strings you may be able to use `{{:?}}` \
615-
(or {{:#?}} for pretty-print) instead",
620+
note="in format strings you may be able to use `{{:?}}` (or {{:#?}} for pretty-print) instead",
616621
)]
617622
#[doc(alias = "{}")]
618623
#[stable(feature = "rust1", since = "1.0.0")]
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use std::path::Path;
2+
3+
fn main() {
4+
let path = Path::new("/tmp/foo/bar.txt");
5+
println!("{}", path);
6+
//~^ ERROR E0277
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0277]: `std::path::Path` doesn't implement `std::fmt::Display`
2+
--> $DIR/path-display.rs:5:20
3+
|
4+
LL | println!("{}", path);
5+
| ^^^^ `std::path::Path` cannot be formatted with the default formatter, call `.display()` on it
6+
|
7+
= help: the trait `std::fmt::Display` is not implemented for `std::path::Path`
8+
= note: you need to call `.display()` or `.to_string_lossy()` for safely printing paths as they may contain non-Unicode data
9+
= note: required because of the requirements on the impl of `std::fmt::Display` for `&std::path::Path`
10+
= note: required by `std::fmt::Display::fmt`
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)