From b7a8f1f225db2fef723317a4bfb4cc649cc9ff84 Mon Sep 17 00:00:00 2001 From: Sergi-Ferrez Date: Tue, 4 Jun 2024 02:11:37 +0200 Subject: [PATCH 1/3] Include trailing commas in functions --- src/librustdoc/html/format.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index d20cef745ab0e..de92fb80b938b 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1431,20 +1431,16 @@ impl clean::FnDecl { cx: &Context<'_>, ) -> fmt::Result { let amp = if f.alternate() { "&" } else { "&" }; - + write!(f, "(")?; if let Some(n) = line_wrapping_indent && !self.inputs.values.is_empty() { write!(f, "\n{}", Indent(n + 4))?; } + + let last_input_index = self.inputs.values.len() - 1; for (i, input) in self.inputs.values.iter().enumerate() { - if i > 0 { - match line_wrapping_indent { - None => write!(f, ", ")?, - Some(n) => write!(f, ",\n{}", Indent(n + 4))?, - }; - } if let Some(selfty) = input.to_self() { match selfty { clean::SelfValue => { @@ -1477,18 +1473,24 @@ impl clean::FnDecl { write!(f, "{}: ", input.name)?; input.type_.print(cx).fmt(f)?; } + match line_wrapping_indent { + None if i == last_input_index => (), + None => write!(f, ", ")?, + Some(_n) if i == last_input_index => write!(f, ",\n")?, + Some(n) => write!(f, ",\n{}", Indent(n + 4))?, + } } if self.c_variadic { match line_wrapping_indent { - None => write!(f, ", ...")?, - Some(n) => write!(f, "\n{}...", Indent(n + 4))?, + None => write!(f, "...")?, + Some(n) => write!(f, "{}...\n", Indent(n + 4))?, }; } match line_wrapping_indent { None => write!(f, ")")?, - Some(n) => write!(f, "\n{})", Indent(n))?, + Some(n) => write!(f, "{})", Indent(n))?, }; self.print_output(cx).fmt(f) From 617e64c9e7233ec1f936b8e14c30037fc3f6a1be Mon Sep 17 00:00:00 2001 From: Sergi-Ferrez Date: Tue, 4 Jun 2024 13:49:39 +0200 Subject: [PATCH 2/3] Update code format and tests --- src/librustdoc/html/format.rs | 4 ++-- tests/rustdoc/async-fn.rs | 2 +- tests/rustdoc/decl-line-wrapping-empty-arg-list.decl.html | 3 +-- tests/rustdoc/decl-trailing-whitespace.declaration.html | 4 ++-- tests/rustdoc/inline_cross/impl_trait.rs | 2 +- tests/rustdoc/line-breaks.rs | 2 +- tests/rustdoc/reexports-priv.rs | 2 +- 7 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index de92fb80b938b..ff49f65ed08fe 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1431,7 +1431,7 @@ impl clean::FnDecl { cx: &Context<'_>, ) -> fmt::Result { let amp = if f.alternate() { "&" } else { "&" }; - + write!(f, "(")?; if let Some(n) = line_wrapping_indent && !self.inputs.values.is_empty() @@ -1483,7 +1483,7 @@ impl clean::FnDecl { if self.c_variadic { match line_wrapping_indent { - None => write!(f, "...")?, + None => write!(f, ", ...")?, Some(n) => write!(f, "{}...\n", Indent(n + 4))?, }; } diff --git a/tests/rustdoc/async-fn.rs b/tests/rustdoc/async-fn.rs index 010263f6ad31d..4de5d8575b03b 100644 --- a/tests/rustdoc/async-fn.rs +++ b/tests/rustdoc/async-fn.rs @@ -79,7 +79,7 @@ struct AsyncFdReadyGuard<'a, T> { x: &'a T } impl Foo { // @has async_fn/struct.Foo.html - // @has - '//*[@class="method"]' 'pub async fn complicated_lifetimes( &self, context: &impl Bar ) -> impl Iterator' + // @has - '//*[@class="method"]' 'pub async fn complicated_lifetimes( &self, context: &impl Bar, ) -> impl Iterator' pub async fn complicated_lifetimes(&self, context: &impl Bar) -> impl Iterator { [0].iter() } diff --git a/tests/rustdoc/decl-line-wrapping-empty-arg-list.decl.html b/tests/rustdoc/decl-line-wrapping-empty-arg-list.decl.html index 29c08c5bd5dec..12c6bc214a725 100644 --- a/tests/rustdoc/decl-line-wrapping-empty-arg-list.decl.html +++ b/tests/rustdoc/decl-line-wrapping-empty-arg-list.decl.html @@ -1,2 +1 @@ -
pub fn create(
-) -> Padding00000000000000000000000000000000000000000000000000000000000000000000000000000000
\ No newline at end of file +
pub fn create() -> Padding00000000000000000000000000000000000000000000000000000000000000000000000000000000
\ No newline at end of file diff --git a/tests/rustdoc/decl-trailing-whitespace.declaration.html b/tests/rustdoc/decl-trailing-whitespace.declaration.html index 59c318c16f3b0..0cc3f0fa244e1 100644 --- a/tests/rustdoc/decl-trailing-whitespace.declaration.html +++ b/tests/rustdoc/decl-trailing-whitespace.declaration.html @@ -3,7 +3,7 @@ fn poll_write( self, cx: &mut Option<String>, - buf: &mut [usize] + buf: &mut [usize], ) -> Option<Result<usize, Error>>; fn poll_flush(self, cx: &mut Option<String>) -> Option<Result<(), Error>>; fn poll_close(self, cx: &mut Option<String>) -> Option<Result<(), Error>>; @@ -12,6 +12,6 @@ fn poll_write_vectored( self, cx: &mut Option<String>, - bufs: &[usize] + bufs: &[usize], ) -> Option<Result<usize, Error>> { ... } } \ No newline at end of file diff --git a/tests/rustdoc/inline_cross/impl_trait.rs b/tests/rustdoc/inline_cross/impl_trait.rs index 47e2c9dc76bc6..19d1673f2eb9e 100644 --- a/tests/rustdoc/inline_cross/impl_trait.rs +++ b/tests/rustdoc/inline_cross/impl_trait.rs @@ -11,7 +11,7 @@ pub use impl_trait_aux::func; // @has impl_trait/fn.func2.html // @has - '//pre[@class="rust item-decl"]' "func2(" // @has - '//pre[@class="rust item-decl"]' "_x: impl Deref> + Iterator," -// @has - '//pre[@class="rust item-decl"]' "_y: impl Iterator )" +// @has - '//pre[@class="rust item-decl"]' "_y: impl Iterator, )" // @!has - '//pre[@class="rust item-decl"]' 'where' pub use impl_trait_aux::func2; diff --git a/tests/rustdoc/line-breaks.rs b/tests/rustdoc/line-breaks.rs index 21aa3a03ce430..0f760d51973c5 100644 --- a/tests/rustdoc/line-breaks.rs +++ b/tests/rustdoc/line-breaks.rs @@ -6,7 +6,7 @@ use std::ops::Add; // @matches foo/fn.function_with_a_really_long_name.html '//*[@class="rust item-decl"]//code' "\ // function_with_a_really_long_name\(\n\ // \ parameter_one: i32,\n\ -// \ parameter_two: i32\n\ +// \ parameter_two: i32,\n\ // \) -> Option$" pub fn function_with_a_really_long_name(parameter_one: i32, parameter_two: i32) -> Option { Some(parameter_one + parameter_two) diff --git a/tests/rustdoc/reexports-priv.rs b/tests/rustdoc/reexports-priv.rs index 1eee262d23303..97318a0141051 100644 --- a/tests/rustdoc/reexports-priv.rs +++ b/tests/rustdoc/reexports-priv.rs @@ -98,7 +98,7 @@ pub mod outer { pub use reexports::foo; // @has 'foo/outer/inner/fn.foo_crate.html' '//pre[@class="rust item-decl"]' 'pub(crate) fn foo_crate()' pub(crate) use reexports::foo_crate; - // @has 'foo/outer/inner/fn.foo_super.html' '//pre[@class="rust item-decl"]' 'pub(in outer) fn foo_super( )' + // @has 'foo/outer/inner/fn.foo_super.html' '//pre[@class="rust item-decl"]' 'pub(in outer) fn foo_super()' pub(super) use::reexports::foo_super; // @!has 'foo/outer/inner/fn.foo_self.html' pub(self) use reexports::foo_self; From 744dc8c503debec5ce39f9529de80300ee1f7a2a Mon Sep 17 00:00:00 2001 From: Sergi-Ferrez Date: Tue, 4 Jun 2024 16:05:51 +0200 Subject: [PATCH 3/3] Use checked_sub --- src/librustdoc/html/format.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index ff49f65ed08fe..4268fadd6c59c 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1439,7 +1439,7 @@ impl clean::FnDecl { write!(f, "\n{}", Indent(n + 4))?; } - let last_input_index = self.inputs.values.len() - 1; + let last_input_index = self.inputs.values.len().checked_sub(1); for (i, input) in self.inputs.values.iter().enumerate() { if let Some(selfty) = input.to_self() { match selfty { @@ -1473,11 +1473,12 @@ impl clean::FnDecl { write!(f, "{}: ", input.name)?; input.type_.print(cx).fmt(f)?; } - match line_wrapping_indent { - None if i == last_input_index => (), - None => write!(f, ", ")?, - Some(_n) if i == last_input_index => write!(f, ",\n")?, - Some(n) => write!(f, ",\n{}", Indent(n + 4))?, + match (line_wrapping_indent, last_input_index) { + (_, None) => (), + (None, Some(last_i)) if i != last_i => write!(f, ", ")?, + (None, Some(_)) => (), + (Some(n), Some(last_i)) if i != last_i => write!(f, ",\n{}", Indent(n + 4))?, + (Some(_), Some(_)) => write!(f, ",\n")?, } }