warning magic-literal
What it means#
The same duration, size, retry count or limit literal appears two or more times in attributes or arguments (part III.4, IV.1). Such values should be named once with a const, a config field or a dynamic value. A literal that occurs once and is self-explanatory is fine.
Example#
data User { name: str }
service Users {
@timeout(3s) @public rpc get(id: u64) -> User
@timeout(3s) @public rpc list(page: u32) -> List<User> // <- magic-literal
}
impl Users {
rpc get(id) { User{ name: "ann" } }
rpc list(page) { [User{ name: "ann" }] }
}error
magic-literal
How to fix#
data User { name: str }
pub const STD_TIMEOUT = 3s
service Users {
@timeout(STD_TIMEOUT) @public rpc get(id: u64) -> User
@timeout(STD_TIMEOUT) @public rpc list(page: u32) -> List<User>
}
impl Users {
rpc get(id) { User{ name: "ann" } }
rpc list(page) { [User{ name: "ann" }] }
}Introduce a named constant (or a cluster { defaults { ... } } value shared by all rpc) and use it everywhere. The lint can be raised to an error with magic-literal = "error" in [lint].