Skip to content

Commit

Permalink
Fix issue #3805 - wrap macro line with width of one char beyond max
Browse files Browse the repository at this point in the history
  • Loading branch information
davidBar-On committed Feb 6, 2023
1 parent 5391847 commit b1fecdd
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::comment::{
contains_comment, CharClasses, FindUncommented, FullCodeCharKind, LineClasses,
};
use crate::config::lists::*;
use crate::config::Version;
use crate::expr::{rewrite_array, rewrite_assign_rhs, RhsAssignKind};
use crate::lists::{itemize_list, write_list, ListFormatting};
use crate::overflow;
Expand Down Expand Up @@ -1218,8 +1219,16 @@ impl MacroBranch {
return None;
}

// 5 = " => {"
let mut result = format_macro_args(context, self.args.clone(), shape.sub_width(5)?)?;
let old_body = context.snippet(self.body).trim();
let has_block_body = old_body.starts_with('{');
let mut prefix_width = 5; // 5 = " => {"
if context.config.version() == Version::Two {
if has_block_body {
prefix_width = 6; // 6 = " => {{"
}
}
let mut result =
format_macro_args(context, self.args.clone(), shape.sub_width(prefix_width)?)?;

if multi_branch_style {
result += " =>";
Expand All @@ -1237,9 +1246,7 @@ impl MacroBranch {
// `$$`). We'll try and format like an AST node, but we'll substitute
// variables for new names with the same length first.

let old_body = context.snippet(self.body).trim();
let (body_str, substs) = replace_names(old_body)?;
let has_block_body = old_body.starts_with('{');

let mut config = context.config.clone();
config.set().hide_parse_errors(true);
Expand Down
65 changes: 65 additions & 0 deletions tests/source/issue-3805.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// rustfmt-version: Two
// rustfmt-format_macro_matchers: true

// From original issue example - Line length 101
macro_rules! test {
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr) => {{
return;
}};
}

// Spaces between the `{` and `}`
macro_rules! test {
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr) => { {
return;
} };
}

// Multi `{}`
macro_rules! test {
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr) => {{{{
return;
}}}};
}

// Multi `{}` with spaces
macro_rules! test {
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr) => { { { {
return;
} } } };
}

// Line length 102
macro_rules! test {
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeiou:expr, $add:expr) => {{
return;
}};
}

// Line length 103
macro_rules! test {
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeioua:expr, $add:expr) => {{
return;
}};
}

// With extended macro body - Line length 101
macro_rules! test {
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr) => {{
let VAR = "VALUE"; return VAR;
}};
}

// With extended macro body - Line length 102
macro_rules! test {
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeiou:expr, $add:expr) => {{
let VAR = "VALUE"; return VAR;
}};
}

// With extended macro body - Line length 103
macro_rules! test {
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeioua:expr, $add:expr) => {{
let VAR = "VALUE"; return VAR;
}};
}
94 changes: 94 additions & 0 deletions tests/target/issue-3805.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// rustfmt-version: Two
// rustfmt-format_macro_matchers: true

// From original issue example - Line length 101
macro_rules! test {
(
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr
) => {{
return;
}};
}

// Spaces between the `{` and `}`
macro_rules! test {
(
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr
) => {{
return;
}};
}

// Multi `{}`
macro_rules! test {
(
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr
) => {{
{
{
return;
}
}
}};
}

// Multi `{}` with spaces
macro_rules! test {
(
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr
) => {{
{
{
return;
}
}
}};
}

// Line length 102
macro_rules! test {
(
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeiou:expr, $add:expr
) => {{
return;
}};
}

// Line length 103
macro_rules! test {
(
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeioua:expr, $add:expr
) => {{
return;
}};
}

// With extended macro body - Line length 101
macro_rules! test {
(
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr
) => {{
let VAR = "VALUE";
return VAR;
}};
}

// With extended macro body - Line length 102
macro_rules! test {
(
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeiou:expr, $add:expr
) => {{
let VAR = "VALUE";
return VAR;
}};
}

// With extended macro body - Line length 103
macro_rules! test {
(
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeioua:expr, $add:expr
) => {{
let VAR = "VALUE";
return VAR;
}};
}

0 comments on commit b1fecdd

Please sign in to comment.