Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add UI tests
Signed-off-by: Oliver Tale-Yazdi <[email protected]>
  • Loading branch information
ggwpez committed May 6, 2023
commit 754d1539aa9f4a4f42ca699d51722c66c9feea04
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/target/
**/target

Cargo.lock
**/*.rs.bk
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Building a warning is easy with the builder pattern.

```rust
use proc_macro_warning::Warning;

let warning = Warning::new_deprecated("my_macro")
.old("my_macro()")
.new("my_macro::new()")
Expand All @@ -35,6 +36,8 @@ let warning = Warning::new_deprecated("my_macro")
let tokens = quote::quote!(#warning);
```

This works in derive-macros, but you **must** put in a span, otherwise it will not show up in the compile output.

## Used In

Substrate (since [#13798](https://github.com/paritytech/substrate/pull/13798)) uses this to emit warnings for its FRAME eDSL on deprecated behaviour.
Expand Down Expand Up @@ -69,13 +72,12 @@ warning: use of deprecated constant `pallet::warnings::ConstantWeight_0::_w`:
|
```


## License

Licensed under either of at your own choice:

* GNU GENERAL PUBLIC LICENSE, Version 3 ([LICENSE-GPL3](./LICENSE-GPL3) or https://www.gnu.org/licenses/gpl-3.0.txt)
* Apache License, Version 2.0 ([LICENSE-APACHE2](/LICENSE-APACHE2) or https://www.apache.org/licenses/LICENSE-2.0.txt).
* GNU GENERAL PUBLIC LICENSE, Version 3 ([LICENSE-GPL3](./LICENSE-GPL3) or [gnu.org](https://www.gnu.org/licenses/gpl-3.0.txt>))
* Apache License, Version 2.0 ([LICENSE-APACHE2](/LICENSE-APACHE2) or [apache.org](https://www.apache.org/licenses/LICENSE-2.0.txt>)).

### Contribution

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: (GPL-3.0 or Apache-2.0)
*/

//! Emit warnings from inside proc macros.
#![doc = include_str!("../README.md")]

use core::ops::Deref;
use proc_macro2::Span;
Expand Down
5 changes: 5 additions & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = [
"derive",
"ui"
]
13 changes: 13 additions & 0 deletions tests/derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "derive"
version = "0.1.0"
edition = "2021"
publish = false

[lib]
proc-macro = true

[dependencies]
proc-macro-warning = { path = "../../" }
quote = "1.0.26"
syn = "2.0.15"
26 changes: 26 additions & 0 deletions tests/derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use proc_macro::TokenStream;
use syn::spanned::Spanned;

#[proc_macro_derive(Deprecated)]
pub fn deprecated(input: TokenStream) -> TokenStream {
impl_dep(input, true)
}

#[proc_macro_derive(DeprecatedNoSpan)]
pub fn deprecated2(input: TokenStream) -> TokenStream {
impl_dep(input, false)
}

fn impl_dep(input: TokenStream, span: bool) -> TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput);

let warning = proc_macro_warning::Warning::new_deprecated("test")
.old("foo").new("bar");
let warning = if span {
warning.span(input.span())
} else {
warning
}.build();

quote::quote!{ #warning }.into()
}
9 changes: 9 additions & 0 deletions tests/ui/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ui"
version = "0.1.0"
edition = "2021"
publish = false

[dev-dependencies]
trybuild = "1.0.80"
derive = { path = "../derive" }
17 changes: 17 additions & 0 deletions tests/ui/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[test]
#[cfg(test)]
fn ui_warm() {
std::env::set_var("RUSTFLAGS", "--deny warnings");
let t = trybuild::TestCases::new();

t.compile_fail("src/warn/*.rs");
}

#[test]
#[cfg(test)]
fn ui_no_warn() {
std::env::set_var("RUSTFLAGS", "--deny warnings");
let t = trybuild::TestCases::new();

t.pass("src/no-warn/*.rs");
}
10 changes: 10 additions & 0 deletions tests/ui/src/no-warn/derive_no_span.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Without a span no warning will be printed.

#[derive(derive::DeprecatedNoSpan)]
struct Test {

}

fn main() {
let _ = Test { };
}
8 changes: 8 additions & 0 deletions tests/ui/src/warn/derive_basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[derive(derive::Deprecated)]
struct Test {

}

fn main() {
let _ = Test { };
}
11 changes: 11 additions & 0 deletions tests/ui/src/warn/derive_basic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: use of deprecated constant `test::_w`:
It is deprecated to foo.
Please instead bar.
--> src/warn/derive_basic.rs:2:1
|
2 | / struct Test {
3 | |
4 | | }
| |_^
|
= note: `-D deprecated` implied by `-D warnings`