]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/auxiliary/proc_macro_derive.rs
New upstream version 1.72.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / auxiliary / proc_macro_derive.rs
1 #![feature(repr128, proc_macro_quote)]
2 #![allow(incomplete_features)]
3 #![allow(clippy::field_reassign_with_default)]
4 #![allow(clippy::eq_op)]
5
6 extern crate proc_macro;
7
8 use proc_macro::{quote, Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
9
10 #[proc_macro_derive(DeriveSomething)]
11 pub fn derive(_: TokenStream) -> TokenStream {
12 // Should not trigger `used_underscore_binding`
13 let _inside_derive = 1;
14 assert_eq!(_inside_derive, _inside_derive);
15
16 let output = quote! {
17 // Should not trigger `useless_attribute`
18 #[allow(dead_code)]
19 extern crate rustc_middle;
20 };
21 output
22 }
23
24 #[proc_macro_derive(FieldReassignWithDefault)]
25 pub fn derive_foo(_input: TokenStream) -> TokenStream {
26 quote! {
27 #[derive(Default)]
28 struct A {
29 pub i: i32,
30 pub j: i64,
31 }
32 #[automatically_derived]
33 fn lint() {
34 let mut a: A = Default::default();
35 a.i = 42;
36 a;
37 }
38 }
39 }
40
41 #[proc_macro_derive(StructAUseSelf)]
42 pub fn derive_use_self(_input: TokenStream) -> proc_macro::TokenStream {
43 quote! {
44 struct A;
45 impl A {
46 fn new() -> A {
47 A
48 }
49 }
50 }
51 }
52
53 #[proc_macro_derive(ClippyMiniMacroTest)]
54 pub fn mini_macro(_: TokenStream) -> TokenStream {
55 quote!(
56 #[allow(unused)]
57 fn needless_take_by_value(s: String) {
58 println!("{}", s.len());
59 }
60 #[allow(unused)]
61 fn needless_loop(items: &[u8]) {
62 for i in 0..items.len() {
63 println!("{}", items[i]);
64 }
65 }
66 fn line_wrapper() {
67 println!("{}", line!());
68 }
69 )
70 }
71
72 #[proc_macro_derive(ExtraLifetimeDerive)]
73 #[allow(unused)]
74 pub fn extra_lifetime(_input: TokenStream) -> TokenStream {
75 quote!(
76 pub struct ExtraLifetime;
77
78 impl<'b> ExtraLifetime {
79 pub fn something<'c>() -> Self {
80 Self
81 }
82 }
83 )
84 }
85
86 #[allow(unused)]
87 #[proc_macro_derive(ArithmeticDerive)]
88 pub fn arithmetic_derive(_: TokenStream) -> TokenStream {
89 <TokenStream as FromIterator<TokenTree>>::from_iter([
90 Ident::new("fn", Span::call_site()).into(),
91 Ident::new("_foo", Span::call_site()).into(),
92 Group::new(Delimiter::Parenthesis, TokenStream::new()).into(),
93 Group::new(
94 Delimiter::Brace,
95 <TokenStream as FromIterator<TokenTree>>::from_iter([
96 Ident::new("let", Span::call_site()).into(),
97 Ident::new("mut", Span::call_site()).into(),
98 Ident::new("_n", Span::call_site()).into(),
99 Punct::new('=', Spacing::Alone).into(),
100 Literal::i32_unsuffixed(9).into(),
101 Punct::new(';', Spacing::Alone).into(),
102 Ident::new("_n", Span::call_site()).into(),
103 Punct::new('=', Spacing::Alone).into(),
104 Literal::i32_unsuffixed(9).into(),
105 Punct::new('/', Spacing::Alone).into(),
106 Literal::i32_unsuffixed(2).into(),
107 Punct::new(';', Spacing::Alone).into(),
108 Ident::new("_n", Span::call_site()).into(),
109 Punct::new('=', Spacing::Alone).into(),
110 Punct::new('-', Spacing::Alone).into(),
111 Ident::new("_n", Span::call_site()).into(),
112 Punct::new(';', Spacing::Alone).into(),
113 ]),
114 )
115 .into(),
116 ])
117 }
118
119 #[allow(unused)]
120 #[proc_macro_derive(ShadowDerive)]
121 pub fn shadow_derive(_: TokenStream) -> TokenStream {
122 <TokenStream as FromIterator<TokenTree>>::from_iter([
123 Ident::new("fn", Span::call_site()).into(),
124 Ident::new("_foo", Span::call_site()).into(),
125 Group::new(Delimiter::Parenthesis, TokenStream::new()).into(),
126 Group::new(
127 Delimiter::Brace,
128 <TokenStream as FromIterator<TokenTree>>::from_iter([
129 Ident::new("let", Span::call_site()).into(),
130 Ident::new("_x", Span::call_site()).into(),
131 Punct::new('=', Spacing::Alone).into(),
132 Literal::i32_unsuffixed(2).into(),
133 Punct::new(';', Spacing::Alone).into(),
134 Ident::new("let", Span::call_site()).into(),
135 Ident::new("_x", Span::call_site()).into(),
136 Punct::new('=', Spacing::Alone).into(),
137 Ident::new("_x", Span::call_site()).into(),
138 Punct::new(';', Spacing::Alone).into(),
139 ]),
140 )
141 .into(),
142 ])
143 }