forked from opentensor/subtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforbid_keys_remove.rs
119 lines (105 loc) · 3.57 KB
/
forbid_keys_remove.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use super::*;
use syn::{
punctuated::Punctuated, spanned::Spanned, token::Comma, visit::Visit, Expr, ExprCall, ExprPath,
File, Path,
};
pub struct ForbidKeysRemoveCall;
impl Lint for ForbidKeysRemoveCall {
fn lint(source: &File) -> Result {
let mut visitor = KeysRemoveVisitor::default();
visitor.visit_file(source);
if visitor.errors.is_empty() {
Ok(())
} else {
Err(visitor.errors)
}
}
}
#[derive(Default)]
struct KeysRemoveVisitor {
errors: Vec<syn::Error>,
}
impl<'ast> Visit<'ast> for KeysRemoveVisitor {
fn visit_expr_call(&mut self, node: &'ast syn::ExprCall) {
let ExprCall {
func, args, attrs, ..
} = node;
if is_keys_remove_call(func, args) && !is_allowed(attrs) {
let msg = "Keys::<T>::remove()` is banned to prevent accidentally breaking \
the neuron sequence. If you need to replace neurons, try `SubtensorModule::replace_neuron()`";
self.errors.push(syn::Error::new(node.func.span(), msg));
}
}
}
fn is_keys_remove_call(func: &Expr, args: &Punctuated<Expr, Comma>) -> bool {
let Expr::Path(ExprPath {
path: Path { segments: func, .. },
..
}) = func
else {
return false;
};
func.len() == 2
&& args.len() == 2
&& func[0].ident == "Keys"
&& !func[0].arguments.is_none()
&& func[1].ident == "remove"
&& func[1].arguments.is_none()
}
#[cfg(test)]
mod tests {
use super::*;
use quote::quote;
fn lint(input: proc_macro2::TokenStream) -> Result {
let mut visitor = KeysRemoveVisitor::default();
let expr: syn::ExprCall = syn::parse2(input).expect("should be a valid function call");
visitor.visit_expr_call(&expr);
if visitor.errors.is_empty() {
Ok(())
} else {
Err(visitor.errors)
}
}
#[test]
fn test_keys_remove_forbidden() {
let input = quote! { Keys::<T>::remove(netuid, uid_to_replace) };
assert!(lint(input).is_err());
let input = quote! { Keys::<U>::remove(netuid, uid_to_replace) };
assert!(lint(input).is_err());
let input = quote! { Keys::<U>::remove(1, "2".parse().unwrap(),) };
assert!(lint(input).is_err());
}
#[test]
fn test_non_keys_remove_not_forbidden() {
let input = quote! { remove(netuid, uid_to_replace) };
assert!(lint(input).is_ok());
let input = quote! { Keys::remove(netuid, uid_to_replace) };
assert!(lint(input).is_ok());
let input = quote! { Keys::<T>::remove::<U>(netuid, uid_to_replace) };
assert!(lint(input).is_ok());
let input = quote! { Keys::<T>::remove(netuid, uid_to_replace, third_wheel) };
assert!(lint(input).is_ok());
let input = quote! { ParentKeys::remove(netuid, uid_to_replace) };
assert!(lint(input).is_ok());
let input = quote! { ChildKeys::<T>::remove(netuid, uid_to_replace) };
assert!(lint(input).is_ok());
}
#[test]
fn test_keys_remove_allowed() {
let input = quote! {
#[allow(unknown_lints)]
Keys::<T>::remove(netuid, uid_to_replace)
};
assert!(lint(input).is_ok());
let input = quote! {
#[allow(unknown_lints)]
Keys::<U>::remove(netuid, uid_to_replace)
};
assert!(lint(input).is_ok());
let input = quote! {
#[allow(unknown_lints)]
Keys::<U>::remove(1, "2".parse().unwrap(),)
};
assert!(lint(input).is_ok());
}
}