diff --git a/src/tools/rustfmt/src/types.rs b/src/tools/rustfmt/src/types.rs index 0009490e86fee..7b44b47c7196f 100644 --- a/src/tools/rustfmt/src/types.rs +++ b/src/tools/rustfmt/src/types.rs @@ -1019,7 +1019,11 @@ impl Rewrite for ast::Ty { } ast::TyKind::UnsafeBinder(ref binder) => { let mut result = String::new(); - if let Some(ref lifetime_str) = + if binder.generic_params.is_empty() { + // We always want to write `unsafe<>` since `unsafe<> Ty` + // and `Ty` are distinct types. + result.push_str("unsafe<> ") + } else if let Some(ref lifetime_str) = rewrite_bound_params(context, shape, &binder.generic_params) { result.push_str("unsafe<"); diff --git a/src/tools/rustfmt/tests/source/unsafe-binders.rs b/src/tools/rustfmt/tests/source/unsafe-binders.rs index ccf7c8bb9afa3..2f43af54d2043 100644 --- a/src/tools/rustfmt/tests/source/unsafe-binders.rs +++ b/src/tools/rustfmt/tests/source/unsafe-binders.rs @@ -9,3 +9,6 @@ struct Foo { struct Bar(unsafe<'a> &'a ()); impl Trait for unsafe<'a> &'a () {} + +fn empty() +-> unsafe<> () {} diff --git a/src/tools/rustfmt/tests/target/unsafe-binders.rs b/src/tools/rustfmt/tests/target/unsafe-binders.rs index 9d308f4a8946b..d52dc55951966 100644 --- a/src/tools/rustfmt/tests/target/unsafe-binders.rs +++ b/src/tools/rustfmt/tests/target/unsafe-binders.rs @@ -7,3 +7,5 @@ struct Foo { struct Bar(unsafe<'a> &'a ()); impl Trait for unsafe<'a> &'a () {} + +fn empty() -> unsafe<> () {}