Skip to content

Commit 63e9715

Browse files
committed
use TokenStream in place of str
1 parent 1d6c938 commit 63e9715

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

support/linting/src/forbid_as_primitive.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ fn is_as_primitive(ident: &Ident) -> bool {
4545
#[cfg(test)]
4646
mod tests {
4747
use super::*;
48+
use quote::quote;
4849

49-
fn lint(input: &str) -> Result {
50-
let expr: ExprMethodCall = syn::parse_str(input).expect("should only use on a method call");
50+
fn lint(input: proc_macro2::TokenStream) -> Result {
5151
let mut visitor = AsPrimitiveVisitor::default();
52+
let expr: ExprMethodCall = syn::parse2(input).expect("should be a valid method call");
5253
visitor.visit_expr_method_call(&expr);
5354
if !visitor.errors.is_empty() {
5455
return Err(visitor.errors);
@@ -58,21 +59,21 @@ mod tests {
5859

5960
#[test]
6061
fn test_as_primitives() {
61-
let input = r#"x.as_u32()"#;
62+
let input = quote! {x.as_u32() };
6263
assert!(lint(input).is_err());
63-
let input = r#"x.as_u64()"#;
64+
let input = quote! {x.as_u64() };
6465
assert!(lint(input).is_err());
65-
let input = r#"x.as_u128()"#;
66+
let input = quote! {x.as_u128() };
6667
assert!(lint(input).is_err());
67-
let input = r#"x.as_usize()"#;
68+
let input = quote! {x.as_usize() };
6869
assert!(lint(input).is_err());
6970
}
7071

7172
#[test]
7273
fn test_non_as_primitives() {
73-
let input = r#"x.as_ref()"#;
74+
let input = quote! {x.as_ref() };
7475
assert!(lint(input).is_ok());
75-
let input = r#"x.as_slice()"#;
76+
let input = quote! {x.as_slice() };
7677
assert!(lint(input).is_ok());
7778
}
7879
}

support/linting/src/forbid_keys_remove.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ fn is_keys_remove_call(func: &Expr, args: &Punctuated<Expr, Comma>) -> bool {
5858
#[cfg(test)]
5959
mod tests {
6060
use super::*;
61+
use quote::quote;
6162

62-
fn lint(input: &str) -> Result {
63+
fn lint(input: proc_macro2::TokenStream) -> Result {
6364
let mut visitor = KeysRemoveVisitor::default();
64-
visitor
65-
.visit_expr_call(&syn::parse_str(input).expect("should only use on a function call"));
65+
let expr: syn::ExprCall = syn::parse2(input).expect("should be a valid function call");
66+
visitor.visit_expr_call(&expr);
6667

6768
if visitor.errors.is_empty() {
6869
Ok(())
@@ -73,46 +74,46 @@ mod tests {
7374

7475
#[test]
7576
fn test_keys_remove_forbidden() {
76-
let input = r#"Keys::<T>::remove(netuid, uid_to_replace)"#;
77+
let input = quote! { Keys::<T>::remove(netuid, uid_to_replace) };
7778
assert!(lint(input).is_err());
78-
let input = r#"Keys::<U>::remove(netuid, uid_to_replace)"#;
79+
let input = quote! { Keys::<U>::remove(netuid, uid_to_replace) };
7980
assert!(lint(input).is_err());
80-
let input = r#"Keys::<U>::remove(1, "2".parse().unwrap(),)"#;
81+
let input = quote! { Keys::<U>::remove(1, "2".parse().unwrap(),) };
8182
assert!(lint(input).is_err());
8283
}
8384

8485
#[test]
8586
fn test_non_keys_remove_not_forbidden() {
86-
let input = r#"remove(netuid, uid_to_replace)"#;
87+
let input = quote! { remove(netuid, uid_to_replace) };
8788
assert!(lint(input).is_ok());
88-
let input = r#"Keys::remove(netuid, uid_to_replace)"#;
89+
let input = quote! { Keys::remove(netuid, uid_to_replace) };
8990
assert!(lint(input).is_ok());
90-
let input = r#"Keys::<T>::remove::<U>(netuid, uid_to_replace)"#;
91+
let input = quote! { Keys::<T>::remove::<U>(netuid, uid_to_replace) };
9192
assert!(lint(input).is_ok());
92-
let input = r#"Keys::<T>::remove(netuid, uid_to_replace, third_wheel)"#;
93+
let input = quote! { Keys::<T>::remove(netuid, uid_to_replace, third_wheel) };
9394
assert!(lint(input).is_ok());
94-
let input = r#"ParentKeys::remove(netuid, uid_to_replace)"#;
95+
let input = quote! { ParentKeys::remove(netuid, uid_to_replace) };
9596
assert!(lint(input).is_ok());
96-
let input = r#"ChildKeys::<T>::remove(netuid, uid_to_replace)"#;
97+
let input = quote! { ChildKeys::<T>::remove(netuid, uid_to_replace) };
9798
assert!(lint(input).is_ok());
9899
}
99100

100101
#[test]
101102
fn test_keys_remove_allowed() {
102-
let input = r#"
103-
#[allow(unknown_lints)]
104-
Keys::<T>::remove(netuid, uid_to_replace)
105-
"#;
103+
let input = quote! {
104+
#[allow(unknown_lints)]
105+
Keys::<T>::remove(netuid, uid_to_replace)
106+
};
106107
assert!(lint(input).is_ok());
107-
let input = r#"
108-
#[allow(unknown_lints)]
109-
Keys::<U>::remove(netuid, uid_to_replace)
110-
"#;
108+
let input = quote! {
109+
#[allow(unknown_lints)]
110+
Keys::<U>::remove(netuid, uid_to_replace)
111+
};
111112
assert!(lint(input).is_ok());
112-
let input = r#"
113-
#[allow(unknown_lints)]
114-
Keys::<U>::remove(1, "2".parse().unwrap(),)
115-
"#;
113+
let input = quote! {
114+
#[allow(unknown_lints)]
115+
Keys::<U>::remove(1, "2".parse().unwrap(),)
116+
};
116117
assert!(lint(input).is_ok());
117118
}
118119
}

0 commit comments

Comments
 (0)