From 2b0f7b0e4a849131bea538a58b8e56d975484a9a Mon Sep 17 00:00:00 2001 From: shulaoda <165626830+shulaoda@users.noreply.github.com> Date: Fri, 10 Oct 2025 17:02:27 +0800 Subject: [PATCH 1/3] Support formatting for lazy_static::lazy_static! --- src/macros.rs | 18 ++++++++++--- tests/source/lazy_static.rs | 46 +++++++++++++++++++++++++++++++++- tests/target/lazy_static.rs | 50 ++++++++++++++++++++++++++++++++++++- 3 files changed, 108 insertions(+), 6 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 16897e57dcb..915b197819d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -226,8 +226,8 @@ fn rewrite_macro_inner( }; } // Format well-known macros which cannot be parsed as a valid AST. - if macro_name == "lazy_static!" && !has_comment { - match format_lazy_static(context, shape, ts.clone(), mac.span()) { + if (macro_name == "lazy_static!" || macro_name == "lazy_static::lazy_static!") && !has_comment { + match format_lazy_static(context, shape, ts.clone(), mac.span(), ¯o_name) { Ok(rw) => return Ok(rw), Err(err) => match err { // We will move on to parsing macro args just like other macros @@ -1394,7 +1394,8 @@ impl MacroBranch { } } -/// Format `lazy_static!` from . +/// Format `lazy_static!` and `lazy_static::lazy_static!` +/// from . /// /// # Expected syntax /// @@ -1405,19 +1406,28 @@ impl MacroBranch { /// ... /// [pub] static ref NAME_N: TYPE_N = EXPR_N; /// } +/// +/// lazy_static::lazy_static! { +/// [pub] static ref NAME_1: TYPE_1 = EXPR_1; +/// [pub] static ref NAME_2: TYPE_2 = EXPR_2; +/// ... +/// [pub] static ref NAME_N: TYPE_N = EXPR_N; +/// } /// ``` fn format_lazy_static( context: &RewriteContext<'_>, shape: Shape, ts: TokenStream, span: Span, + macro_name: &str, ) -> RewriteResult { let mut result = String::with_capacity(1024); let nested_shape = shape .block_indent(context.config.tab_spaces()) .with_max_width(context.config); - result.push_str("lazy_static! {"); + result.push_str(macro_name); + result.push_str(" {"); result.push_str(&nested_shape.indent.to_string_with_newline(context.config)); let parsed_elems = diff --git a/tests/source/lazy_static.rs b/tests/source/lazy_static.rs index 38fefbcbef5..a3f2324e60c 100644 --- a/tests/source/lazy_static.rs +++ b/tests/source/lazy_static.rs @@ -1,4 +1,4 @@ -// Format `lazy_static!`. +// Format `lazy_static!` and `lazy_static::lazy_static!`. lazy_static! { static ref CONFIG_NAME_REGEX: regex::Regex = @@ -43,3 +43,47 @@ static ref FOO: HashMap Result, Either> ),> = HashMap::new(); } + +lazy_static::lazy_static! { +static ref CONFIG_NAME_REGEX: regex::Regex = +regex::Regex::new(r"^## `([^`]+)`").expect("Failed creating configuration pattern"); +static ref CONFIG_VALUE_REGEX: regex::Regex = regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#) +.expect("Failed creating configuration value pattern"); +} + +// We need to be able to format `lazy_static::lazy_static!` without known syntax. +lazy_static::lazy_static!( + xxx, +yyyy , + zzzzz +); + +lazy_static::lazy_static!{ +} + +// #2354 +lazy_static::lazy_static ! { +pub static ref Sbase64_encode_string : :: lisp :: LispSubrRef = { +let subr = :: remacs_sys :: Lisp_Subr { +header : :: remacs_sys :: Lisp_Vectorlike_Header { +size : ( +( :: remacs_sys :: PseudovecType :: PVEC_SUBR as :: libc :: ptrdiff_t ) << :: +remacs_sys :: PSEUDOVECTOR_AREA_BITS ) , } , function : self :: +Fbase64_encode_string as * const :: libc :: c_void , min_args : 1i16 , +max_args : 2i16 , symbol_name : ( b"base64-encode-string\x00" ) . as_ptr ( ) +as * const :: libc :: c_char , intspec : :: std :: ptr :: null ( ) , doc : :: +std :: ptr :: null ( ) , lang : :: remacs_sys :: Lisp_Subr_Lang_Rust , } ; +unsafe { +let ptr = :: remacs_sys :: xmalloc ( +:: std :: mem :: size_of :: < :: remacs_sys :: Lisp_Subr > ( ) ) as * mut :: +remacs_sys :: Lisp_Subr ; :: std :: ptr :: copy_nonoverlapping ( +& subr , ptr , 1 ) ; :: std :: mem :: forget ( subr ) ; :: lisp :: ExternalPtr +:: new ( ptr ) } } ; } + + +lazy_static::lazy_static! { +static ref FOO: HashMap Result, Either> +),> = HashMap::new(); +} diff --git a/tests/target/lazy_static.rs b/tests/target/lazy_static.rs index 3625e0a5f5c..342c1bd14c2 100644 --- a/tests/target/lazy_static.rs +++ b/tests/target/lazy_static.rs @@ -1,4 +1,4 @@ -// Format `lazy_static!`. +// Format `lazy_static!` and `lazy_static::lazy_static!`. lazy_static! { static ref CONFIG_NAME_REGEX: regex::Regex = @@ -47,3 +47,51 @@ lazy_static! { ), > = HashMap::new(); } + +lazy_static::lazy_static! { + static ref CONFIG_NAME_REGEX: regex::Regex = + regex::Regex::new(r"^## `([^`]+)`").expect("Failed creating configuration pattern"); + static ref CONFIG_VALUE_REGEX: regex::Regex = regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#) + .expect("Failed creating configuration value pattern"); +} + +// We need to be able to format `lazy_static::lazy_static!` without known syntax. +lazy_static::lazy_static!(xxx, yyyy, zzzzz); + +lazy_static::lazy_static! {} + +// #2354 +lazy_static::lazy_static! { + pub static ref Sbase64_encode_string: ::lisp::LispSubrRef = { + let subr = ::remacs_sys::Lisp_Subr { + header: ::remacs_sys::Lisp_Vectorlike_Header { + size: ((::remacs_sys::PseudovecType::PVEC_SUBR as ::libc::ptrdiff_t) + << ::remacs_sys::PSEUDOVECTOR_AREA_BITS), + }, + function: self::Fbase64_encode_string as *const ::libc::c_void, + min_args: 1i16, + max_args: 2i16, + symbol_name: (b"base64-encode-string\x00").as_ptr() as *const ::libc::c_char, + intspec: ::std::ptr::null(), + doc: ::std::ptr::null(), + lang: ::remacs_sys::Lisp_Subr_Lang_Rust, + }; + unsafe { + let ptr = ::remacs_sys::xmalloc(::std::mem::size_of::<::remacs_sys::Lisp_Subr>()) + as *mut ::remacs_sys::Lisp_Subr; + ::std::ptr::copy_nonoverlapping(&subr, ptr, 1); + ::std::mem::forget(subr); + ::lisp::ExternalPtr::new(ptr) + } + }; +} + +lazy_static::lazy_static! { + static ref FOO: HashMap< + String, + ( + &'static str, + fn(Foo) -> Result, Either> + ), + > = HashMap::new(); +} From e56a61f76b8a08367c924480f970c8d8c3849caa Mon Sep 17 00:00:00 2001 From: shulaoda <165626830+shulaoda@users.noreply.github.com> Date: Mon, 13 Oct 2025 10:38:51 +0800 Subject: [PATCH 2/3] u --- src/macros.rs | 6 +++++- tests/source/lazy_static.rs | 1 + tests/target/lazy_static.rs | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index 915b197819d..3b58d28bfb0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -226,7 +226,11 @@ fn rewrite_macro_inner( }; } // Format well-known macros which cannot be parsed as a valid AST. - if (macro_name == "lazy_static!" || macro_name == "lazy_static::lazy_static!") && !has_comment { + if (macro_name == "lazy_static!" + || (context.config.style_edition() >= StyleEdition::Edition2027 + && macro_name == "lazy_static::lazy_static!")) + && !has_comment + { match format_lazy_static(context, shape, ts.clone(), mac.span(), ¯o_name) { Ok(rw) => return Ok(rw), Err(err) => match err { diff --git a/tests/source/lazy_static.rs b/tests/source/lazy_static.rs index a3f2324e60c..633564da577 100644 --- a/tests/source/lazy_static.rs +++ b/tests/source/lazy_static.rs @@ -1,3 +1,4 @@ +// rustfmt-style_edition: 2027 // Format `lazy_static!` and `lazy_static::lazy_static!`. lazy_static! { diff --git a/tests/target/lazy_static.rs b/tests/target/lazy_static.rs index 342c1bd14c2..b29ec9c1429 100644 --- a/tests/target/lazy_static.rs +++ b/tests/target/lazy_static.rs @@ -1,3 +1,4 @@ +// rustfmt-style_edition: 2027 // Format `lazy_static!` and `lazy_static::lazy_static!`. lazy_static! { From 81da0cd426e5dc266a6251b1a092405ce8b5dbe9 Mon Sep 17 00:00:00 2001 From: shulaoda <165626830+shulaoda@users.noreply.github.com> Date: Mon, 13 Oct 2025 11:06:32 +0800 Subject: [PATCH 3/3] Add test case for lazy_static::lazy_static! before style edition 2027 --- tests/source/lazy_staic_before_2027.rs | 43 ++++++++++++++++++++++++++ tests/target/lazy_staic_before_2027.rs | 37 ++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tests/source/lazy_staic_before_2027.rs create mode 100644 tests/target/lazy_staic_before_2027.rs diff --git a/tests/source/lazy_staic_before_2027.rs b/tests/source/lazy_staic_before_2027.rs new file mode 100644 index 00000000000..ec9fbe83d72 --- /dev/null +++ b/tests/source/lazy_staic_before_2027.rs @@ -0,0 +1,43 @@ +lazy_static::lazy_static! { +static ref CONFIG_NAME_REGEX: regex::Regex = +regex::Regex::new(r"^## `([^`]+)`").expect("Failed creating configuration pattern"); +static ref CONFIG_VALUE_REGEX: regex::Regex = regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#) +.expect("Failed creating configuration value pattern"); +} + +// We need to be able to format `lazy_static::lazy_static!` without known syntax. +lazy_static::lazy_static!( + xxx, +yyyy , + zzzzz +); + +lazy_static::lazy_static!{ +} + +// #2354 +lazy_static::lazy_static ! { +pub static ref Sbase64_encode_string : :: lisp :: LispSubrRef = { +let subr = :: remacs_sys :: Lisp_Subr { +header : :: remacs_sys :: Lisp_Vectorlike_Header { +size : ( +( :: remacs_sys :: PseudovecType :: PVEC_SUBR as :: libc :: ptrdiff_t ) << :: +remacs_sys :: PSEUDOVECTOR_AREA_BITS ) , } , function : self :: +Fbase64_encode_string as * const :: libc :: c_void , min_args : 1i16 , +max_args : 2i16 , symbol_name : ( b"base64-encode-string\x00" ) . as_ptr ( ) +as * const :: libc :: c_char , intspec : :: std :: ptr :: null ( ) , doc : :: +std :: ptr :: null ( ) , lang : :: remacs_sys :: Lisp_Subr_Lang_Rust , } ; +unsafe { +let ptr = :: remacs_sys :: xmalloc ( +:: std :: mem :: size_of :: < :: remacs_sys :: Lisp_Subr > ( ) ) as * mut :: +remacs_sys :: Lisp_Subr ; :: std :: ptr :: copy_nonoverlapping ( +& subr , ptr , 1 ) ; :: std :: mem :: forget ( subr ) ; :: lisp :: ExternalPtr +:: new ( ptr ) } } ; } + + +lazy_static::lazy_static! { +static ref FOO: HashMap Result, Either> +),> = HashMap::new(); +} diff --git a/tests/target/lazy_staic_before_2027.rs b/tests/target/lazy_staic_before_2027.rs new file mode 100644 index 00000000000..881c2abe003 --- /dev/null +++ b/tests/target/lazy_staic_before_2027.rs @@ -0,0 +1,37 @@ +lazy_static::lazy_static! { +static ref CONFIG_NAME_REGEX: regex::Regex = +regex::Regex::new(r"^## `([^`]+)`").expect("Failed creating configuration pattern"); +static ref CONFIG_VALUE_REGEX: regex::Regex = regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#) +.expect("Failed creating configuration value pattern"); +} + +// We need to be able to format `lazy_static::lazy_static!` without known syntax. +lazy_static::lazy_static!(xxx, yyyy, zzzzz); + +lazy_static::lazy_static! {} + +// #2354 +lazy_static::lazy_static! { +pub static ref Sbase64_encode_string : :: lisp :: LispSubrRef = { +let subr = :: remacs_sys :: Lisp_Subr { +header : :: remacs_sys :: Lisp_Vectorlike_Header { +size : ( +( :: remacs_sys :: PseudovecType :: PVEC_SUBR as :: libc :: ptrdiff_t ) << :: +remacs_sys :: PSEUDOVECTOR_AREA_BITS ) , } , function : self :: +Fbase64_encode_string as * const :: libc :: c_void , min_args : 1i16 , +max_args : 2i16 , symbol_name : ( b"base64-encode-string\x00" ) . as_ptr ( ) +as * const :: libc :: c_char , intspec : :: std :: ptr :: null ( ) , doc : :: +std :: ptr :: null ( ) , lang : :: remacs_sys :: Lisp_Subr_Lang_Rust , } ; +unsafe { +let ptr = :: remacs_sys :: xmalloc ( +:: std :: mem :: size_of :: < :: remacs_sys :: Lisp_Subr > ( ) ) as * mut :: +remacs_sys :: Lisp_Subr ; :: std :: ptr :: copy_nonoverlapping ( +& subr , ptr , 1 ) ; :: std :: mem :: forget ( subr ) ; :: lisp :: ExternalPtr +:: new ( ptr ) } } ; } + +lazy_static::lazy_static! { +static ref FOO: HashMap Result, Either> +),> = HashMap::new(); +}