Skip to content

Commit e52b383

Browse files
authored
Merge pull request #2306 from dtwood/assert-eq-on-one-line
Add assert_eq! to special-cased macros
2 parents 9a7242c + 39e2f43 commit e52b383

File tree

6 files changed

+136
-73
lines changed

6 files changed

+136
-73
lines changed

src/codemap.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,9 @@ impl LineRangeUtils for CodeMap {
8686
let hi = self.lookup_char_pos(span.hi());
8787

8888
assert_eq!(
89-
lo.file.name,
90-
hi.file.name,
89+
lo.file.name, hi.file.name,
9190
"span crossed file boundary: lo: {:?}, hi: {:?}",
92-
lo,
93-
hi
91+
lo, hi
9492
);
9593

9694
LineRange {

src/expr.rs

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,25 +1811,40 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
18111811
)
18121812
}
18131813

1814-
const FORMAT_LIKE_WHITELIST: &[&str] = &[
1814+
/// A list of `format!`-like macros, that take a long format string and a list of arguments to
1815+
/// format.
1816+
///
1817+
/// Organized as a list of `(&str, usize)` tuples, giving the name of the macro and the number of
1818+
/// arguments before the format string (none for `format!("format", ...)`, one for `assert!(result,
1819+
/// "format", ...)`, two for `assert_eq!(left, right, "format", ...)`).
1820+
const SPECIAL_MACRO_WHITELIST: &[(&str, usize)] = &[
1821+
// format! like macros
18151822
// From the Rust Standard Library.
1816-
"eprint!",
1817-
"eprintln!",
1818-
"format!",
1819-
"format_args!",
1820-
"print!",
1821-
"println!",
1822-
"panic!",
1823-
"unreachable!",
1823+
("eprint!", 0),
1824+
("eprintln!", 0),
1825+
("format!", 0),
1826+
("format_args!", 0),
1827+
("print!", 0),
1828+
("println!", 0),
1829+
("panic!", 0),
1830+
("unreachable!", 0),
18241831
// From the `log` crate.
1825-
"debug!",
1826-
"error!",
1827-
"info!",
1828-
"warn!",
1832+
("debug!", 0),
1833+
("error!", 0),
1834+
("info!", 0),
1835+
("warn!", 0),
1836+
// write! like macros
1837+
("assert!", 1),
1838+
("debug_assert!", 1),
1839+
("write!", 1),
1840+
("writeln!", 1),
1841+
// assert_eq! like macros
1842+
("assert_eq!", 2),
1843+
("assert_ne!", 2),
1844+
("debug_assert_eq!", 2),
1845+
("debug_assert_ne!", 2),
18291846
];
18301847

1831-
const WRITE_LIKE_WHITELIST: &[&str] = &["assert!", "write!", "writeln!"];
1832-
18331848
pub fn rewrite_call(
18341849
context: &RewriteContext,
18351850
callee: &str,
@@ -2066,24 +2081,26 @@ where
20662081
} else {
20672082
tactic = default_tactic();
20682083

2069-
// For special-case macros, we may want to use different tactics.
2070-
let maybe_args_offset = maybe_get_args_offset(callee_str, args);
2071-
2072-
if tactic == DefinitiveListTactic::Vertical && maybe_args_offset.is_some() {
2073-
let args_offset = maybe_args_offset.unwrap();
2074-
let args_tactic = definitive_tactic(
2075-
&item_vec[args_offset..],
2076-
ListTactic::HorizontalVertical,
2077-
Separator::Comma,
2078-
nested_shape.width,
2079-
);
2080-
2081-
// Every argument is simple and fits on a single line.
2082-
if args_tactic == DefinitiveListTactic::Horizontal {
2083-
tactic = if args_offset == 1 {
2084-
DefinitiveListTactic::FormatCall
2085-
} else {
2086-
DefinitiveListTactic::WriteCall
2084+
if tactic == DefinitiveListTactic::Vertical {
2085+
if let Some((all_simple, num_args_before)) =
2086+
maybe_get_args_offset(callee_str, args)
2087+
{
2088+
let one_line = all_simple
2089+
&& definitive_tactic(
2090+
&item_vec[..num_args_before],
2091+
ListTactic::HorizontalVertical,
2092+
Separator::Comma,
2093+
nested_shape.width,
2094+
) == DefinitiveListTactic::Horizontal
2095+
&& definitive_tactic(
2096+
&item_vec[num_args_before + 1..],
2097+
ListTactic::HorizontalVertical,
2098+
Separator::Comma,
2099+
nested_shape.width,
2100+
) == DefinitiveListTactic::Horizontal;
2101+
2102+
if one_line {
2103+
tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
20872104
};
20882105
}
20892106
}
@@ -2120,15 +2137,14 @@ fn is_every_args_simple<T: ToExpr>(lists: &[&T]) -> bool {
21202137
}
21212138

21222139
/// In case special-case style is required, returns an offset from which we start horizontal layout.
2123-
fn maybe_get_args_offset<T: ToExpr>(callee_str: &str, args: &[&T]) -> Option<usize> {
2124-
if FORMAT_LIKE_WHITELIST.iter().any(|s| *s == callee_str) && args.len() >= 1
2125-
&& is_every_args_simple(args)
2126-
{
2127-
Some(1)
2128-
} else if WRITE_LIKE_WHITELIST.iter().any(|s| *s == callee_str) && args.len() >= 2
2129-
&& is_every_args_simple(args)
2140+
fn maybe_get_args_offset<T: ToExpr>(callee_str: &str, args: &[&T]) -> Option<(bool, usize)> {
2141+
if let Some(&(_, num_args_before)) = SPECIAL_MACRO_WHITELIST
2142+
.iter()
2143+
.find(|&&(s, _)| s == callee_str)
21302144
{
2131-
Some(2)
2145+
let all_simple = args.len() >= num_args_before && is_every_args_simple(args);
2146+
2147+
Some((all_simple, num_args_before))
21322148
} else {
21332149
None
21342150
}

src/lists.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,8 @@ pub enum DefinitiveListTactic {
160160
Vertical,
161161
Horizontal,
162162
Mixed,
163-
// Special case tactic for `format!()` variants.
164-
FormatCall,
165-
// Special case tactic for `write!()` varianta.
166-
WriteCall,
163+
/// Special case tactic for `format!()`, `write!()` style macros.
164+
SpecialMacro(usize),
167165
}
168166

169167
impl DefinitiveListTactic {
@@ -271,7 +269,7 @@ where
271269
I: IntoIterator<Item = T> + Clone,
272270
T: AsRef<ListItem>,
273271
{
274-
let mut tactic = formatting.tactic;
272+
let tactic = formatting.tactic;
275273
let sep_len = formatting.separator.len();
276274

277275
// Now that we know how we will layout, we can decide for sure if there
@@ -313,26 +311,16 @@ where
313311
DefinitiveListTactic::Horizontal if !first => {
314312
result.push(' ');
315313
}
316-
DefinitiveListTactic::FormatCall if !first => {
317-
result.push('\n');
318-
result.push_str(indent_str);
319-
tactic = DefinitiveListTactic::Horizontal;
320-
}
321-
DefinitiveListTactic::WriteCall => {
322-
let second = i == 1;
323-
let third = i == 2;
324-
325-
if first {
314+
DefinitiveListTactic::SpecialMacro(num_args_before) => {
315+
if i == 0 {
326316
// Nothing
327-
} else if second {
328-
result.push('\n');
329-
result.push_str(indent_str);
330-
} else if third {
317+
} else if i < num_args_before {
318+
result.push(' ');
319+
} else if i <= num_args_before + 1 {
331320
result.push('\n');
332321
result.push_str(indent_str);
333-
tactic = DefinitiveListTactic::Horizontal;
334322
} else {
335-
unreachable!();
323+
result.push(' ');
336324
}
337325
}
338326
DefinitiveListTactic::Vertical if !first => {

tests/source/macros.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,15 @@ fn special_case_macros() {
266266
warn!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
267267
warn!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
268268

269-
assert!(result, "Ahoy there, {}!", target);
270-
assert!(result, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
271-
assert!(result, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
269+
assert!(result == 42, "Ahoy there, {}!", target);
270+
assert!(result == 42, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
271+
assert!(result == 42, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
272+
273+
assert_eq!(left, right, "Ahoy there, {}!", target);
274+
assert_eq!(left, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
275+
assert_eq!(first_realllllllllllly_long_variable_that_doesnt_fit_one_one_line, second_reallllllllllly_long_variable_that_doesnt_fit_one_one_line, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
276+
assert_eq!(left + 42, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
277+
assert_eq!(left, right, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
272278

273279
write!(&mut s, "Ahoy there, {}!", target);
274280
write!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);

tests/system.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ fn self_tests() {
192192
}
193193

194194
assert_eq!(
195-
warnings,
196-
0,
195+
warnings, 0,
197196
"Rustfmt's code generated {} warnings",
198197
warnings
199198
);

tests/target/macros.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,14 +691,70 @@ fn special_case_macros() {
691691
26
692692
);
693693

694-
assert!(result, "Ahoy there, {}!", target);
694+
assert!(result == 42, "Ahoy there, {}!", target);
695695
assert!(
696+
result == 42,
697+
"Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
696698
result,
699+
input,
700+
expected
701+
);
702+
assert!(
703+
result == 42,
704+
"{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
705+
1,
706+
2,
707+
3,
708+
4,
709+
5,
710+
6,
711+
7,
712+
8,
713+
9,
714+
10,
715+
11,
716+
12,
717+
13,
718+
14,
719+
15,
720+
16,
721+
17,
722+
18,
723+
19,
724+
20,
725+
21,
726+
22,
727+
23,
728+
24,
729+
25,
730+
26
731+
);
732+
733+
assert_eq!(left, right, "Ahoy there, {}!", target);
734+
assert_eq!(
735+
left, right,
697736
"Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
698737
result, input, expected
699738
);
700-
assert!(
739+
assert_eq!(
740+
first_realllllllllllly_long_variable_that_doesnt_fit_one_one_line,
741+
second_reallllllllllly_long_variable_that_doesnt_fit_one_one_line,
742+
"Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
743+
result,
744+
input,
745+
expected
746+
);
747+
assert_eq!(
748+
left + 42,
749+
right,
750+
"Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
701751
result,
752+
input,
753+
expected
754+
);
755+
assert_eq!(
756+
left,
757+
right,
702758
"{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
703759
1,
704760
2,

0 commit comments

Comments
 (0)