Skip to content
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
22 changes: 18 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,12 @@ 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!"
|| (context.config.style_edition() >= StyleEdition::Edition2027
&& macro_name == "lazy_static::lazy_static!"))
&& !has_comment
{
match format_lazy_static(context, shape, ts.clone(), mac.span(), &macro_name) {
Ok(rw) => return Ok(rw),
Err(err) => match err {
// We will move on to parsing macro args just like other macros
Expand Down Expand Up @@ -1394,7 +1398,8 @@ impl MacroBranch {
}
}

/// Format `lazy_static!` from <https://crates.io/crates/lazy_static>.
/// Format `lazy_static!` and `lazy_static::lazy_static!`
/// from <https://crates.io/crates/lazy_static>.
///
/// # Expected syntax
///
Expand All @@ -1405,19 +1410,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 =
Expand Down
43 changes: 43 additions & 0 deletions tests/source/lazy_staic_before_2027.rs
Original file line number Diff line number Diff line change
@@ -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<String,
(&'static str,
fn(Foo) -> Result<Box<Bar>, Either<FooError, BarError>>
),> = HashMap::new();
}
47 changes: 46 additions & 1 deletion tests/source/lazy_static.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Format `lazy_static!`.
// rustfmt-style_edition: 2027
// Format `lazy_static!` and `lazy_static::lazy_static!`.

lazy_static! {
static ref CONFIG_NAME_REGEX: regex::Regex =
Expand Down Expand Up @@ -43,3 +44,47 @@ static ref FOO: HashMap<String,
fn(Foo) -> Result<Box<Bar>, Either<FooError, BarError>>
),> = 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<Box<Bar>, Either<FooError, BarError>>
),> = HashMap::new();
}
37 changes: 37 additions & 0 deletions tests/target/lazy_staic_before_2027.rs
Original file line number Diff line number Diff line change
@@ -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<String,
(&'static str,
fn(Foo) -> Result<Box<Bar>, Either<FooError, BarError>>
),> = HashMap::new();
}
51 changes: 50 additions & 1 deletion tests/target/lazy_static.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Format `lazy_static!`.
// rustfmt-style_edition: 2027
// Format `lazy_static!` and `lazy_static::lazy_static!`.

lazy_static! {
static ref CONFIG_NAME_REGEX: regex::Regex =
Expand Down Expand Up @@ -47,3 +48,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<Box<Bar>, Either<FooError, BarError>>
),
> = HashMap::new();
}
Loading