Skip to content

Commit d23c372

Browse files
authored
Merge pull request #927 from eagr/forbid-keys-remove
Lint `ForbidKeysRemoveCall`
2 parents b096420 + 63e9715 commit d23c372

File tree

6 files changed

+160
-9
lines changed

6 files changed

+160
-9
lines changed

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn main() {
6060
};
6161

6262
track_lint(ForbidAsPrimitiveConversion::lint(&parsed_file));
63+
track_lint(ForbidKeysRemoveCall::lint(&parsed_file));
6364
track_lint(RequireFreezeStruct::lint(&parsed_file));
6465
track_lint(RequireExplicitPalletIndex::lint(&parsed_file));
6566
});

pallets/subtensor/src/subnets/uids.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl<T: Config> Pallet<T> {
2929
// 2. Remove previous set memberships.
3030
Uids::<T>::remove(netuid, old_hotkey.clone());
3131
IsNetworkMember::<T>::remove(old_hotkey.clone(), netuid);
32+
#[allow(unknown_lints)]
3233
Keys::<T>::remove(netuid, uid_to_replace);
3334

3435
// 2a. Check if the uid is registered in any other subnetworks.

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
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}

support/linting/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ pub mod lint;
22
pub use lint::*;
33

44
mod forbid_as_primitive;
5+
mod forbid_keys_remove;
56
mod pallet_index;
67
mod require_freeze_struct;
78

89
pub use forbid_as_primitive::ForbidAsPrimitiveConversion;
10+
pub use forbid_keys_remove::ForbidKeysRemoveCall;
911
pub use pallet_index::RequireExplicitPalletIndex;
1012
pub use require_freeze_struct::RequireFreezeStruct;

support/linting/src/lint.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use syn::File;
1+
use proc_macro2::TokenTree;
2+
use syn::{Attribute, File, Meta, MetaList, Path};
23

34
pub type Result = core::result::Result<(), Vec<syn::Error>>;
45

@@ -11,3 +12,29 @@ pub trait Lint: Send + Sync {
1112
/// Lints the given Rust source file, returning a compile error if any issues are found.
1213
fn lint(source: &File) -> Result;
1314
}
15+
16+
pub fn is_allowed(attibutes: &[Attribute]) -> bool {
17+
attibutes.iter().any(|attribute| {
18+
let Attribute {
19+
meta:
20+
Meta::List(MetaList {
21+
path: Path { segments: attr, .. },
22+
tokens: attr_args,
23+
..
24+
}),
25+
..
26+
} = attribute
27+
else {
28+
return false;
29+
};
30+
31+
attr.len() == 1
32+
&& attr[0].ident == "allow"
33+
&& attr_args.clone().into_iter().any(|arg| {
34+
let TokenTree::Ident(ref id) = arg else {
35+
return false;
36+
};
37+
id == "unknown_lints"
38+
})
39+
})
40+
}

0 commit comments

Comments
 (0)