You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+40-6Lines changed: 40 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,7 @@ Building a warning is easy with the builder pattern.
24
24
25
25
```rust
26
26
useproc_macro_warning::Warning;
27
+
27
28
letwarning=Warning::new_deprecated("my_macro")
28
29
.old("my_macro()")
29
30
.new("my_macro::new()")
@@ -35,11 +36,45 @@ let warning = Warning::new_deprecated("my_macro")
35
36
lettokens=quote::quote!(#warning);
36
37
```
37
38
39
+
This works in derive-macros, but you **must** set a span; otherwise it will not show up in the compile output.
40
+
41
+
The difference to a `#[deprecated]` attribute is that it emits the warning either way. For example when creating a custom `Deprecated` derive macro, it will warn without the struct being constructed.
42
+
43
+
```rust
44
+
#[derive(derive::Deprecated)]
45
+
structTest {}
46
+
47
+
fnmain() {
48
+
// Warning triggers although we never used `Test`.
49
+
// Otherwise use a normal `#[deprecated]`.
50
+
}
51
+
```
52
+
53
+
## Un-opinionated Formatting
54
+
55
+
The normal aforementioned way of creating a warning will impose specific unified grammar and formatting rules.
56
+
You can opt out of this and use your own instead by using `FormattedWarning::new_deprecated`:
57
+
58
+
```rust
59
+
useproc_macro_warning::FormattedWarning;
60
+
61
+
letwarning=FormattedWarning::new_deprecated(
62
+
"my_macro",
63
+
"looooooooooooooooooooooooooooooong line that will not be brokeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeen ;)",
64
+
proc_macro2::Span::call_site(),
65
+
);
66
+
67
+
// Use the warning in a proc macro
68
+
lettokens=quote::quote!(#warning);
69
+
```
70
+
71
+
The output of a [similar example](ui-tests/derive/src/lib.rs) is in [derive_raw.stderr](ui-tests/ui/src/warn/derive_raw.stderr).
72
+
38
73
## Used In
39
74
40
-
Substrate (since [#13798](https://github.com/paritytech/substrate/pull/13798)) uses this to emit warnings for its FRAME eDSL on deprecated behaviour.
75
+
Substrate uses it to emit warnings for its eDSL (FRAME) on deprecated behaviour. The integration was done in [#13798](https://github.com/paritytech/substrate/pull/13798) and shows how to use these warnings in macro expansion.
41
76
42
-
For example not putting a `call_index` on your functions produces:
77
+
The warnings are uniformly formatted and have consistent grammar:
43
78
```pre
44
79
warning: use of deprecated constant `pallet::warnings::ImplicitCallIndex_0::_w`:
45
80
It is deprecated to use implicit call indices.
@@ -55,7 +90,7 @@ warning: use of deprecated constant `pallet::warnings::ImplicitCallIndex_0::_w`:
55
90
|
56
91
```
57
92
58
-
Or using a hard-coded weight:
93
+
A different one:
59
94
```pre
60
95
warning: use of deprecated constant `pallet::warnings::ConstantWeight_0::_w`:
61
96
It is deprecated to use hard-coded constant as call weight.
@@ -69,13 +104,12 @@ warning: use of deprecated constant `pallet::warnings::ConstantWeight_0::_w`:
69
104
|
70
105
```
71
106
72
-
73
107
## License
74
108
75
109
Licensed under either of at your own choice:
76
110
77
-
* GNU GENERAL PUBLIC LICENSE, Version 3 ([LICENSE-GPL3](./LICENSE-GPL3) or https://www.gnu.org/licenses/gpl-3.0.txt)
78
-
* Apache License, Version 2.0 ([LICENSE-APACHE2](/LICENSE-APACHE2) or https://www.apache.org/licenses/LICENSE-2.0.txt).
111
+
* GNU GENERAL PUBLIC LICENSE, Version 3 ([LICENSE-GPL3](./LICENSE-GPL3) or [gnu.org](https://www.gnu.org/licenses/gpl-3.0.txt>))
112
+
* Apache License, Version 2.0 ([LICENSE-APACHE2](/LICENSE-APACHE2) or [apache.org](https://www.apache.org/licenses/LICENSE-2.0.txt>)).
0 commit comments