2020-01-31 16:02:31 -06:00
|
|
|
#![feature(repr128, proc_macro_quote)]
|
2020-10-22 07:23:14 -05:00
|
|
|
#![allow(incomplete_features)]
|
2021-01-05 13:11:37 -06:00
|
|
|
#![allow(clippy::field_reassign_with_default)]
|
2020-10-08 16:02:16 -05:00
|
|
|
#![allow(clippy::eq_op)]
|
2019-04-09 16:19:11 -05:00
|
|
|
|
|
|
|
extern crate proc_macro;
|
|
|
|
|
2023-04-09 06:16:20 -05:00
|
|
|
use proc_macro::{quote, Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
|
2019-04-09 16:19:11 -05:00
|
|
|
|
|
|
|
#[proc_macro_derive(DeriveSomething)]
|
|
|
|
pub fn derive(_: TokenStream) -> TokenStream {
|
2022-04-15 13:25:55 -05:00
|
|
|
// Should not trigger `used_underscore_binding`
|
2019-04-20 02:33:13 -05:00
|
|
|
let _inside_derive = 1;
|
|
|
|
assert_eq!(_inside_derive, _inside_derive);
|
|
|
|
|
2019-04-09 16:19:11 -05:00
|
|
|
let output = quote! {
|
2019-04-20 02:33:13 -05:00
|
|
|
// Should not trigger `useless_attribute`
|
2019-04-09 16:19:11 -05:00
|
|
|
#[allow(dead_code)]
|
2020-03-30 04:02:14 -05:00
|
|
|
extern crate rustc_middle;
|
2019-04-09 16:19:11 -05:00
|
|
|
};
|
|
|
|
output
|
|
|
|
}
|
2021-01-05 13:11:37 -06:00
|
|
|
|
2023-09-30 10:47:57 -05:00
|
|
|
#[proc_macro_derive(ImplStructWithStdDisplay)]
|
|
|
|
pub fn derive_std(_: TokenStream) -> TokenStream {
|
|
|
|
quote! {
|
|
|
|
struct A {}
|
|
|
|
impl ::std::fmt::Display for A {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
|
|
|
|
write!(f, "A")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 13:11:37 -06:00
|
|
|
#[proc_macro_derive(FieldReassignWithDefault)]
|
|
|
|
pub fn derive_foo(_input: TokenStream) -> TokenStream {
|
|
|
|
quote! {
|
|
|
|
#[derive(Default)]
|
|
|
|
struct A {
|
|
|
|
pub i: i32,
|
|
|
|
pub j: i64,
|
|
|
|
}
|
|
|
|
#[automatically_derived]
|
|
|
|
fn lint() {
|
|
|
|
let mut a: A = Default::default();
|
|
|
|
a.i = 42;
|
|
|
|
a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 12:09:24 -06:00
|
|
|
|
|
|
|
#[proc_macro_derive(StructAUseSelf)]
|
|
|
|
pub fn derive_use_self(_input: TokenStream) -> proc_macro::TokenStream {
|
|
|
|
quote! {
|
|
|
|
struct A;
|
|
|
|
impl A {
|
|
|
|
fn new() -> A {
|
|
|
|
A
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 11:03:13 -05:00
|
|
|
|
|
|
|
#[proc_macro_derive(ClippyMiniMacroTest)]
|
|
|
|
pub fn mini_macro(_: TokenStream) -> TokenStream {
|
|
|
|
quote!(
|
|
|
|
#[allow(unused)]
|
|
|
|
fn needless_take_by_value(s: String) {
|
|
|
|
println!("{}", s.len());
|
|
|
|
}
|
|
|
|
#[allow(unused)]
|
|
|
|
fn needless_loop(items: &[u8]) {
|
|
|
|
for i in 0..items.len() {
|
|
|
|
println!("{}", items[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn line_wrapper() {
|
|
|
|
println!("{}", line!());
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2022-06-24 05:00:06 -05:00
|
|
|
|
|
|
|
#[proc_macro_derive(ExtraLifetimeDerive)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub fn extra_lifetime(_input: TokenStream) -> TokenStream {
|
|
|
|
quote!(
|
|
|
|
pub struct ExtraLifetime;
|
|
|
|
|
|
|
|
impl<'b> ExtraLifetime {
|
|
|
|
pub fn something<'c>() -> Self {
|
|
|
|
Self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2023-04-09 06:16:20 -05:00
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
#[proc_macro_derive(ArithmeticDerive)]
|
|
|
|
pub fn arithmetic_derive(_: TokenStream) -> TokenStream {
|
2023-05-30 15:32:47 -05:00
|
|
|
<TokenStream as FromIterator<TokenTree>>::from_iter([
|
|
|
|
Ident::new("fn", Span::call_site()).into(),
|
|
|
|
Ident::new("_foo", Span::call_site()).into(),
|
|
|
|
Group::new(Delimiter::Parenthesis, TokenStream::new()).into(),
|
|
|
|
Group::new(
|
|
|
|
Delimiter::Brace,
|
|
|
|
<TokenStream as FromIterator<TokenTree>>::from_iter([
|
|
|
|
Ident::new("let", Span::call_site()).into(),
|
|
|
|
Ident::new("mut", Span::call_site()).into(),
|
|
|
|
Ident::new("_n", Span::call_site()).into(),
|
|
|
|
Punct::new('=', Spacing::Alone).into(),
|
|
|
|
Literal::i32_unsuffixed(9).into(),
|
|
|
|
Punct::new(';', Spacing::Alone).into(),
|
|
|
|
Ident::new("_n", Span::call_site()).into(),
|
|
|
|
Punct::new('=', Spacing::Alone).into(),
|
|
|
|
Literal::i32_unsuffixed(9).into(),
|
|
|
|
Punct::new('/', Spacing::Alone).into(),
|
|
|
|
Literal::i32_unsuffixed(2).into(),
|
|
|
|
Punct::new(';', Spacing::Alone).into(),
|
|
|
|
Ident::new("_n", Span::call_site()).into(),
|
|
|
|
Punct::new('=', Spacing::Alone).into(),
|
|
|
|
Punct::new('-', Spacing::Alone).into(),
|
|
|
|
Ident::new("_n", Span::call_site()).into(),
|
|
|
|
Punct::new(';', Spacing::Alone).into(),
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
])
|
2023-04-09 06:16:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
#[proc_macro_derive(ShadowDerive)]
|
|
|
|
pub fn shadow_derive(_: TokenStream) -> TokenStream {
|
2023-05-30 15:32:47 -05:00
|
|
|
<TokenStream as FromIterator<TokenTree>>::from_iter([
|
|
|
|
Ident::new("fn", Span::call_site()).into(),
|
|
|
|
Ident::new("_foo", Span::call_site()).into(),
|
|
|
|
Group::new(Delimiter::Parenthesis, TokenStream::new()).into(),
|
|
|
|
Group::new(
|
|
|
|
Delimiter::Brace,
|
|
|
|
<TokenStream as FromIterator<TokenTree>>::from_iter([
|
|
|
|
Ident::new("let", Span::call_site()).into(),
|
|
|
|
Ident::new("_x", Span::call_site()).into(),
|
|
|
|
Punct::new('=', Spacing::Alone).into(),
|
|
|
|
Literal::i32_unsuffixed(2).into(),
|
|
|
|
Punct::new(';', Spacing::Alone).into(),
|
|
|
|
Ident::new("let", Span::call_site()).into(),
|
|
|
|
Ident::new("_x", Span::call_site()).into(),
|
|
|
|
Punct::new('=', Spacing::Alone).into(),
|
|
|
|
Ident::new("_x", Span::call_site()).into(),
|
|
|
|
Punct::new(';', Spacing::Alone).into(),
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
])
|
2023-04-09 06:16:20 -05:00
|
|
|
}
|
2023-10-03 06:36:35 -05:00
|
|
|
|
|
|
|
#[proc_macro_derive(StructIgnoredUnitPattern)]
|
|
|
|
pub fn derive_ignored_unit_pattern(_: TokenStream) -> TokenStream {
|
|
|
|
quote! {
|
|
|
|
struct A;
|
|
|
|
impl A {
|
|
|
|
fn a(&self) -> Result<(), ()> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn b(&self) {
|
|
|
|
let _ = self.a().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-26 04:54:44 -05:00
|
|
|
|
|
|
|
#[proc_macro_derive(NonCanonicalClone)]
|
|
|
|
pub fn non_canonical_clone_derive(_: TokenStream) -> TokenStream {
|
|
|
|
quote! {
|
|
|
|
struct NonCanonicalClone;
|
|
|
|
impl Clone for NonCanonicalClone {
|
|
|
|
fn clone(&self) -> Self {
|
2024-05-30 08:43:40 -05:00
|
|
|
todo!()
|
2024-05-26 04:54:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Copy for NonCanonicalClone {}
|
|
|
|
}
|
|
|
|
}
|