-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathforbid_saturating_math.rs
114 lines (97 loc) · 3.65 KB
/
forbid_saturating_math.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
use super::*;
use syn::{Expr, ExprCall, ExprMethodCall, ExprPath, File, Path, spanned::Spanned, visit::Visit};
pub struct ForbidSaturatingMath;
impl Lint for ForbidSaturatingMath {
fn lint(source: &File) -> Result {
let mut visitor = SaturatingMathBanVisitor::default();
visitor.visit_file(source);
if visitor.errors.is_empty() {
Ok(())
} else {
Err(visitor.errors)
}
}
}
#[derive(Default)]
struct SaturatingMathBanVisitor {
errors: Vec<syn::Error>,
}
impl<'ast> Visit<'ast> for SaturatingMathBanVisitor {
fn visit_expr_method_call(&mut self, node: &'ast ExprMethodCall) {
let ExprMethodCall { method, .. } = node;
if method.to_string().starts_with("saturating_") {
let msg = "Safe math is banned to encourage tests to panic";
self.errors.push(syn::Error::new(method.span(), msg));
}
}
fn visit_expr_call(&mut self, node: &'ast ExprCall) {
let ExprCall { func, .. } = node;
if is_saturating_math_call(func) {
let msg = "Safe math is banned to encourage tests to panic";
self.errors.push(syn::Error::new(node.func.span(), msg));
}
}
}
fn is_saturating_math_call(func: &Expr) -> bool {
let Expr::Path(ExprPath {
path: Path { segments: path, .. },
..
}) = func
else {
return false;
};
path.last().is_some_and(|seg| {
seg.ident.to_string().starts_with("saturating_")
})
}
#[cfg(test)]
mod tests {
use super::*;
use quote::quote;
fn lint(input: proc_macro2::TokenStream) -> Result {
let mut visitor = SaturatingMathBanVisitor::default();
let expr: syn::Expr = syn::parse2(input).expect("should be a valid expression");
match &expr {
syn::Expr::MethodCall(call) => visitor.visit_expr_method_call(call),
syn::Expr::Call(call) => visitor.visit_expr_call(call),
_ => panic!("should be a valid method call or function call"),
}
if visitor.errors.is_empty() {
Ok(())
} else {
Err(visitor.errors)
}
}
#[test]
fn test_saturating_forbidden() {
let input = quote! { stake.saturating_add(alpha) };
assert!(lint(input).is_err());
let input = quote! { alpha_price.saturating_mul(float_alpha_block_emission) };
assert!(lint(input).is_err());
let input = quote! { alpha_out_i.saturating_sub(root_alpha) };
assert!(lint(input).is_err());
}
#[test]
fn test_saturating_ufcs_forbidden() {
let input = quote! { SaturatingAdd::saturating_add(stake, alpha) };
assert!(lint(input).is_err());
let input = quote! { core::num::SaturatingAdd::saturating_add(stake, alpha) };
assert!(lint(input).is_err());
let input =
quote! { SaturatingMul::saturating_mul(alpha_price, float_alpha_block_emission) };
assert!(lint(input).is_err());
let input = quote! { core::num::SaturatingMul::saturating_mul(alpha_price, float_alpha_block_emission) };
assert!(lint(input).is_err());
let input = quote! { SaturatingSub::saturating_sub(alpha_out_i, root_alpha) };
assert!(lint(input).is_err());
let input = quote! { core::num::SaturatingSub::saturating_sub(alpha_out_i, root_alpha) };
assert!(lint(input).is_err());
}
#[test]
fn test_saturating_to_from_num_forbidden() {
let input = quote! { I96F32::saturating_from_num(u64::MAX) };
assert!(lint(input).is_err());
let input = quote! { remaining_emission.saturating_to_num::<u64>() };
assert!(lint(input).is_err());
}
}