Skip to content

Commit e65ad6f

Browse files
committed
Auto merge of #10453 - Jarcho:test_utils, r=dswij
Add utility macros to help with writing tests. Adds two utility macros to help with testing: * `external` expands to it's argument tokens, but makes them appear to come from an external macro. Helps make tests for `in_external_macro` much more readable. * `inline_macros` is an attribute macro which allows the use of a pseudo `inline!` macro which expands to it's argument tokens, but makes them appear to be from a crate-local macro expansion. This removes the need to write `macro_rules` boilerplate when testing how lints interact with macros. --- `external`'s usage is simple. `external!(struct Foo { x: u32});` will make the struct appear as though it came from an external macro. Individual tokens can be escaped if needed. `external!($x + 0 / 10)` will make everything except `x` appear as though it came from an external macro. Can also use `$literal` and `$(tokens...)` as well. --- `inline_macros` is more complicated due to compiler constraints. Given: ```rust #[inline_macros] fn foo() { inline!(5 + 5 / 10); } ``` `inline!(5 + 5 / 10)` will be replace with a call to a generated macro which expands to the contained tokens. Tokens can be escaped by prefixing them with `$`: ```rust #[inline_macros] fn foo() { let x = 5; inline!($x + 5 / $10); } ``` This will pass `x` as an `ident` argument and `10` as a `literal` argument. Token sequences can also be passed with `$(...)`: ```rust #[inline_macros] fn foo() { let mut x = 5; inline!(if $(x >= 5) { $x = 5; }); } ``` This will pass `x >= 5` as `tt` arguments, and `x` as an `ident` argument. --- Not 100% sure `inline_macros` is actually worth having. It does make the tests a little easier to read once you're used to it and it becomes more useful once there are multiple macro tests. The verbosity of declaring single use macros starts to hurt at that point. changelog: None
2 parents f19db28 + 1c7048d commit e65ad6f

File tree

86 files changed

+1173
-1092
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1173
-1092
lines changed

tests/ui/almost_complete_range.fixed

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// run-rustfix
22
// edition:2018
3-
// aux-build:macro_rules.rs
3+
// aux-build:proc_macros.rs
44

55
#![feature(exclusive_range_pattern)]
66
#![feature(stmt_expr_attributes)]
@@ -9,33 +9,10 @@
99
#![allow(clippy::needless_parens_on_range_literals)]
1010
#![allow(clippy::double_parens)]
1111

12-
#[macro_use]
13-
extern crate macro_rules;
14-
15-
macro_rules! a {
16-
() => {
17-
'a'
18-
};
19-
}
20-
macro_rules! A {
21-
() => {
22-
'A'
23-
};
24-
}
25-
macro_rules! zero {
26-
() => {
27-
'0'
28-
};
29-
}
30-
31-
macro_rules! b {
32-
() => {
33-
let _ = 'a'..='z';
34-
let _ = 'A'..='Z';
35-
let _ = '0'..='9';
36-
};
37-
}
12+
extern crate proc_macros;
13+
use proc_macros::{external, inline_macros};
3814

15+
#[inline_macros]
3916
fn main() {
4017
#[rustfmt::skip]
4118
{
@@ -56,9 +33,9 @@ fn main() {
5633
let _ = b'B'..b'Z';
5734
let _ = b'1'..b'9';
5835

59-
let _ = a!()..='z';
60-
let _ = A!()..='Z';
61-
let _ = zero!()..='9';
36+
let _ = inline!('a')..='z';
37+
let _ = inline!('A')..='Z';
38+
let _ = inline!('0')..='9';
6239

6340
let _ = match 0u8 {
6441
b'a'..=b'z' if true => 1,
@@ -80,8 +57,16 @@ fn main() {
8057
_ => 7,
8158
};
8259

83-
almost_complete_range!();
84-
b!();
60+
external!(
61+
let _ = 'a'..'z';
62+
let _ = 'A'..'Z';
63+
let _ = '0'..'9';
64+
);
65+
inline!(
66+
let _ = 'a'..='z';
67+
let _ = 'A'..='Z';
68+
let _ = '0'..='9';
69+
);
8570
}
8671

8772
#[clippy::msrv = "1.25"]

tests/ui/almost_complete_range.rs

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// run-rustfix
22
// edition:2018
3-
// aux-build:macro_rules.rs
3+
// aux-build:proc_macros.rs
44

55
#![feature(exclusive_range_pattern)]
66
#![feature(stmt_expr_attributes)]
@@ -9,33 +9,10 @@
99
#![allow(clippy::needless_parens_on_range_literals)]
1010
#![allow(clippy::double_parens)]
1111

12-
#[macro_use]
13-
extern crate macro_rules;
14-
15-
macro_rules! a {
16-
() => {
17-
'a'
18-
};
19-
}
20-
macro_rules! A {
21-
() => {
22-
'A'
23-
};
24-
}
25-
macro_rules! zero {
26-
() => {
27-
'0'
28-
};
29-
}
30-
31-
macro_rules! b {
32-
() => {
33-
let _ = 'a'..'z';
34-
let _ = 'A'..'Z';
35-
let _ = '0'..'9';
36-
};
37-
}
12+
extern crate proc_macros;
13+
use proc_macros::{external, inline_macros};
3814

15+
#[inline_macros]
3916
fn main() {
4017
#[rustfmt::skip]
4118
{
@@ -56,9 +33,9 @@ fn main() {
5633
let _ = b'B'..b'Z';
5734
let _ = b'1'..b'9';
5835

59-
let _ = a!()..'z';
60-
let _ = A!()..'Z';
61-
let _ = zero!()..'9';
36+
let _ = inline!('a')..'z';
37+
let _ = inline!('A')..'Z';
38+
let _ = inline!('0')..'9';
6239

6340
let _ = match 0u8 {
6441
b'a'..b'z' if true => 1,
@@ -80,8 +57,16 @@ fn main() {
8057
_ => 7,
8158
};
8259

83-
almost_complete_range!();
84-
b!();
60+
external!(
61+
let _ = 'a'..'z';
62+
let _ = 'A'..'Z';
63+
let _ = '0'..'9';
64+
);
65+
inline!(
66+
let _ = 'a'..'z';
67+
let _ = 'A'..'Z';
68+
let _ = '0'..'9';
69+
);
8570
}
8671

8772
#[clippy::msrv = "1.25"]

0 commit comments

Comments
 (0)