Skip to content

Commit f986c74

Browse files
authored
Rollup merge of #94868 - dtolnay:noblock, r=Dylan-DPC
Format core and std macro rules, removing needless surrounding blocks Many of the asserting and printing macros in `core` and `std` are written with prehistoric-looking formatting, like this: https://github.com/rust-lang/rust/blob/335ffbfa547df94ac236f5c56130cecf99c8d82b/library/std/src/macros.rs#L96-L101 In modern Rust style this would conventionally be written as follows instead, always using braces and a trailing semicolon on the macro arms: https://github.com/rust-lang/rust/blob/af53809c874e0afb7be966df4d3cfcaa05277c53/library/std/src/macros.rs#L98-L105 Getting rid of the unneeded braces inside the expansion reduces extraneous indentation in macro-expanded code. For example: ```rust println!("repro {}", true); ``` ```rust // before: { ::std::io::_print( ::core::fmt::Arguments::new_v1( &["repro ", "\n"], &[::core::fmt::ArgumentV1::new_display(&true)], ), ); }; ``` ```rust // after: ::std::io::_print( ::core::fmt::Arguments::new_v1( &["repro ", "\n"], &[::core::fmt::ArgumentV1::new_display(&true)], ), ); ```
2 parents f21488a + ac5c657 commit f986c74

File tree

7 files changed

+76
-44
lines changed

7 files changed

+76
-44
lines changed

Diff for: library/core/src/macros/mod.rs

+50-26
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ macro_rules! panic {
3434
#[cfg_attr(not(test), rustc_diagnostic_item = "assert_eq_macro")]
3535
#[allow_internal_unstable(core_panic)]
3636
macro_rules! assert_eq {
37-
($left:expr, $right:expr $(,)?) => ({
37+
($left:expr, $right:expr $(,)?) => {
3838
match (&$left, &$right) {
3939
(left_val, right_val) => {
4040
if !(*left_val == *right_val) {
@@ -46,8 +46,8 @@ macro_rules! assert_eq {
4646
}
4747
}
4848
}
49-
});
50-
($left:expr, $right:expr, $($arg:tt)+) => ({
49+
};
50+
($left:expr, $right:expr, $($arg:tt)+) => {
5151
match (&$left, &$right) {
5252
(left_val, right_val) => {
5353
if !(*left_val == *right_val) {
@@ -59,7 +59,7 @@ macro_rules! assert_eq {
5959
}
6060
}
6161
}
62-
});
62+
};
6363
}
6464

6565
/// Asserts that two expressions are not equal to each other (using [`PartialEq`]).
@@ -84,7 +84,7 @@ macro_rules! assert_eq {
8484
#[cfg_attr(not(test), rustc_diagnostic_item = "assert_ne_macro")]
8585
#[allow_internal_unstable(core_panic)]
8686
macro_rules! assert_ne {
87-
($left:expr, $right:expr $(,)?) => ({
87+
($left:expr, $right:expr $(,)?) => {
8888
match (&$left, &$right) {
8989
(left_val, right_val) => {
9090
if *left_val == *right_val {
@@ -96,8 +96,8 @@ macro_rules! assert_ne {
9696
}
9797
}
9898
}
99-
});
100-
($left:expr, $right:expr, $($arg:tt)+) => ({
99+
};
100+
($left:expr, $right:expr, $($arg:tt)+) => {
101101
match (&($left), &($right)) {
102102
(left_val, right_val) => {
103103
if *left_val == *right_val {
@@ -109,7 +109,7 @@ macro_rules! assert_ne {
109109
}
110110
}
111111
}
112-
});
112+
};
113113
}
114114

115115
/// Asserts that an expression matches any of the given patterns.
@@ -142,7 +142,7 @@ macro_rules! assert_ne {
142142
#[allow_internal_unstable(core_panic)]
143143
#[rustc_macro_transparency = "semitransparent"]
144144
pub macro assert_matches {
145-
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => ({
145+
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
146146
match $left {
147147
$( $pattern )|+ $( if $guard )? => {}
148148
ref left_val => {
@@ -153,8 +153,8 @@ pub macro assert_matches {
153153
);
154154
}
155155
}
156-
}),
157-
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => ({
156+
},
157+
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => {
158158
match $left {
159159
$( $pattern )|+ $( if $guard )? => {}
160160
ref left_val => {
@@ -165,7 +165,7 @@ pub macro assert_matches {
165165
);
166166
}
167167
}
168-
}),
168+
},
169169
}
170170

171171
/// Asserts that a boolean expression is `true` at runtime.
@@ -214,7 +214,11 @@ pub macro assert_matches {
214214
#[rustc_diagnostic_item = "debug_assert_macro"]
215215
#[allow_internal_unstable(edition_panic)]
216216
macro_rules! debug_assert {
217-
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert!($($arg)*); })
217+
($($arg:tt)*) => {
218+
if $crate::cfg!(debug_assertions) {
219+
$crate::assert!($($arg)*);
220+
}
221+
};
218222
}
219223

220224
/// Asserts that two expressions are equal to each other.
@@ -240,7 +244,11 @@ macro_rules! debug_assert {
240244
#[stable(feature = "rust1", since = "1.0.0")]
241245
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_eq_macro")]
242246
macro_rules! debug_assert_eq {
243-
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert_eq!($($arg)*); })
247+
($($arg:tt)*) => {
248+
if $crate::cfg!(debug_assertions) {
249+
$crate::assert_eq!($($arg)*);
250+
}
251+
};
244252
}
245253

246254
/// Asserts that two expressions are not equal to each other.
@@ -266,7 +274,11 @@ macro_rules! debug_assert_eq {
266274
#[stable(feature = "assert_ne", since = "1.13.0")]
267275
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_ne_macro")]
268276
macro_rules! debug_assert_ne {
269-
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert_ne!($($arg)*); })
277+
($($arg:tt)*) => {
278+
if $crate::cfg!(debug_assertions) {
279+
$crate::assert_ne!($($arg)*);
280+
}
281+
};
270282
}
271283

272284
/// Asserts that an expression matches any of the given patterns.
@@ -305,7 +317,9 @@ macro_rules! debug_assert_ne {
305317
#[allow_internal_unstable(assert_matches)]
306318
#[rustc_macro_transparency = "semitransparent"]
307319
pub macro debug_assert_matches($($arg:tt)*) {
308-
if $crate::cfg!(debug_assertions) { $crate::assert_matches::assert_matches!($($arg)*); }
320+
if $crate::cfg!(debug_assertions) {
321+
$crate::assert_matches::assert_matches!($($arg)*);
322+
}
309323
}
310324

311325
/// Returns whether the given expression matches any of the given patterns.
@@ -331,7 +345,7 @@ macro_rules! matches {
331345
$( $pattern )|+ $( if $guard )? => true,
332346
_ => false
333347
}
334-
}
348+
};
335349
}
336350

337351
/// Unwraps a result or propagates its error.
@@ -482,7 +496,9 @@ macro_rules! r#try {
482496
#[stable(feature = "rust1", since = "1.0.0")]
483497
#[cfg_attr(not(test), rustc_diagnostic_item = "write_macro")]
484498
macro_rules! write {
485-
($dst:expr, $($arg:tt)*) => ($dst.write_fmt($crate::format_args!($($arg)*)))
499+
($dst:expr, $($arg:tt)*) => {
500+
$dst.write_fmt($crate::format_args!($($arg)*))
501+
};
486502
}
487503

488504
/// Write formatted data into a buffer, with a newline appended.
@@ -534,12 +550,12 @@ macro_rules! write {
534550
#[cfg_attr(not(test), rustc_diagnostic_item = "writeln_macro")]
535551
#[allow_internal_unstable(format_args_nl)]
536552
macro_rules! writeln {
537-
($dst:expr $(,)?) => (
553+
($dst:expr $(,)?) => {
538554
$crate::write!($dst, "\n")
539-
);
540-
($dst:expr, $($arg:tt)*) => (
555+
};
556+
($dst:expr, $($arg:tt)*) => {
541557
$dst.write_fmt($crate::format_args_nl!($($arg)*))
542-
);
558+
};
543559
}
544560

545561
/// Indicates unreachable code.
@@ -683,8 +699,12 @@ macro_rules! unreachable {
683699
#[cfg_attr(not(test), rustc_diagnostic_item = "unimplemented_macro")]
684700
#[allow_internal_unstable(core_panic)]
685701
macro_rules! unimplemented {
686-
() => ($crate::panicking::panic("not implemented"));
687-
($($arg:tt)+) => ($crate::panic!("not implemented: {}", $crate::format_args!($($arg)+)));
702+
() => {
703+
$crate::panicking::panic("not implemented")
704+
};
705+
($($arg:tt)+) => {
706+
$crate::panic!("not implemented: {}", $crate::format_args!($($arg)+))
707+
};
688708
}
689709

690710
/// Indicates unfinished code.
@@ -746,8 +766,12 @@ macro_rules! unimplemented {
746766
#[cfg_attr(not(test), rustc_diagnostic_item = "todo_macro")]
747767
#[allow_internal_unstable(core_panic)]
748768
macro_rules! todo {
749-
() => ($crate::panicking::panic("not yet implemented"));
750-
($($arg:tt)+) => ($crate::panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
769+
() => {
770+
$crate::panicking::panic("not yet implemented")
771+
};
772+
($($arg:tt)+) => {
773+
$crate::panic!("not yet implemented: {}", $crate::format_args!($($arg)+))
774+
};
751775
}
752776

753777
/// Definitions of built-in macros.

Diff for: library/std/src/macros.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ macro_rules! panic {
6060
#[cfg_attr(not(test), rustc_diagnostic_item = "print_macro")]
6161
#[allow_internal_unstable(print_internals)]
6262
macro_rules! print {
63-
($($arg:tt)*) => ($crate::io::_print($crate::format_args!($($arg)*)));
63+
($($arg:tt)*) => {
64+
$crate::io::_print($crate::format_args!($($arg)*))
65+
};
6466
}
6567

6668
/// Prints to the standard output, with a newline.
@@ -94,10 +96,12 @@ macro_rules! print {
9496
#[cfg_attr(not(test), rustc_diagnostic_item = "println_macro")]
9597
#[allow_internal_unstable(print_internals, format_args_nl)]
9698
macro_rules! println {
97-
() => ($crate::print!("\n"));
98-
($($arg:tt)*) => ({
99-
$crate::io::_print($crate::format_args_nl!($($arg)*));
100-
})
99+
() => {
100+
$crate::print!("\n")
101+
};
102+
($($arg:tt)*) => {
103+
$crate::io::_print($crate::format_args_nl!($($arg)*))
104+
};
101105
}
102106

103107
/// Prints to the standard error.
@@ -126,7 +130,9 @@ macro_rules! println {
126130
#[cfg_attr(not(test), rustc_diagnostic_item = "eprint_macro")]
127131
#[allow_internal_unstable(print_internals)]
128132
macro_rules! eprint {
129-
($($arg:tt)*) => ($crate::io::_eprint($crate::format_args!($($arg)*)));
133+
($($arg:tt)*) => {
134+
$crate::io::_eprint($crate::format_args!($($arg)*))
135+
};
130136
}
131137

132138
/// Prints to the standard error, with a newline.
@@ -155,10 +161,12 @@ macro_rules! eprint {
155161
#[cfg_attr(not(test), rustc_diagnostic_item = "eprintln_macro")]
156162
#[allow_internal_unstable(print_internals, format_args_nl)]
157163
macro_rules! eprintln {
158-
() => ($crate::eprint!("\n"));
159-
($($arg:tt)*) => ({
160-
$crate::io::_eprint($crate::format_args_nl!($($arg)*));
161-
})
164+
() => {
165+
$crate::eprint!("\n")
166+
};
167+
($($arg:tt)*) => {
168+
$crate::io::_eprint($crate::format_args_nl!($($arg)*))
169+
};
162170
}
163171

164172
/// Prints and returns the value of a given expression for quick and dirty

Diff for: src/test/pretty/dollar-crate.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
// pp-exact:dollar-crate.pp
1010

1111
fn main() {
12-
{ ::std::io::_print(::core::fmt::Arguments::new_v1(&["rust\n"], &[])); };
12+
::std::io::_print(::core::fmt::Arguments::new_v1(&["rust\n"], &[]));
1313
}

Diff for: src/test/run-make-fulldeps/coverage-reports/expected_show_coverage.closure.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@
116116
116| 1|
117117
117| 1| let
118118
118| 1| _unused_closure
119-
119| | =
120-
120| | |
119+
119| 1| =
120+
120| 1| |
121121
121| | mut countdown
122122
122| | |
123123
123| 0| {
@@ -173,7 +173,7 @@
173173
169| | ;
174174
170| |
175175
171| 1| let short_used_not_covered_closure_line_break_no_block_embedded_branch =
176-
172| | | _unused_arg: u8 |
176+
172| 1| | _unused_arg: u8 |
177177
173| 0| println!(
178178
174| 0| "not called: {}",
179179
175| 0| if is_true { "check" } else { "me" }
@@ -191,7 +191,7 @@
191191
187| | ;
192192
188| |
193193
189| 1| let short_used_covered_closure_line_break_no_block_embedded_branch =
194-
190| 1| | _unused_arg: u8 |
194+
190| | | _unused_arg: u8 |
195195
191| 1| println!(
196196
192| 1| "not called: {}",
197197
193| 1| if is_true { "check" } else { "me" }

Diff for: src/test/ui/macros/trace-macro.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ LL | println!("Hello, World!");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: expanding `println! { "Hello, World!" }`
8-
= note: to `{ $crate :: io :: _print($crate :: format_args_nl! ("Hello, World!")) ; }`
8+
= note: to `$crate :: io :: _print($crate :: format_args_nl! ("Hello, World!"))`
99

Diff for: src/test/ui/parser/issues/issue-62894.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ LL | fn main() {}
4545
|
4646
::: $SRC_DIR/core/src/macros/mod.rs:LL:COL
4747
|
48-
LL | ($left:expr, $right:expr $(,)?) => ({
48+
LL | ($left:expr, $right:expr $(,)?) => {
4949
| ---------- while parsing argument for this `expr` macro fragment
5050

5151
error: aborting due to 4 previous errors

Diff for: src/test/ui/pattern/usefulness/tuple-struct-nonexhaustive.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | struct Foo(isize, isize);
1212
= note: the matched value is of type `Foo`
1313
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
1414
|
15-
LL ~ Foo(2, b) => println!("{}", b)
15+
LL ~ Foo(2, b) => println!("{}", b),
1616
LL + Foo(_, _) => todo!()
1717
|
1818

0 commit comments

Comments
 (0)