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