rust_style_guide/lib.rs
1// -------- rust coding guidelines: https://rust-coding-guidelines.github.io/rust-coding-guidelines-zh/ --------
2// -------- rustc lint doc: https://doc.rust-lang.org/rustc/lints/listing/index.html --------
3// -------- rust-clippy doc: https://rust-lang.github.io/rust-clippy/master/index.html --------
4
5// [REQUIRED] G.VAR.02 Do not use non-ASCII characters in identifiers
6#![deny(non_ascii_idents)]
7// [REQUIRED]
8#![allow(clippy::disallowed_names)]
9// [REQUIRED]
10#![allow(clippy::blanket_clippy_restriction_lints)]
11// [OPTIONAL] G.CMT.01 Add Error documentation in the docs of public functions that return Result
12// #![warn(clippy::missing_errors_doc)]
13// [REQUIRED] G.CMT.02 Add Panic documentation in the docs of public APIs that may panic under certain circumstances
14#![warn(clippy::missing_panics_doc)]
15// [RECOMMENDED] G.VAR.03 Variable shadowing should be used carefully
16#![warn(clippy::shadow_reuse, clippy::shadow_same, clippy::shadow_unrelated)]
17// [RECOMMENDED] G.CNS.05 Use const fn for functions or methods wherever applicable
18#![warn(clippy::missing_const_for_fn)]
19// [REQUIRED] G.TYP.01 Prefer safe conversion functions over `as` for type casting
20#![warn(
21 clippy::as_conversions,
22 clippy::cast_lossless,
23 clippy::cast_possible_truncation,
24 clippy::cast_possible_wrap,
25 clippy::ptr_as_ptr
26)]
27// [RECOMMENDED] G.VAR.01 Avoid using too many meaningless variable names when destructuring tuples with more than four
28// variables
29#![warn(clippy::many_single_char_names)]
30// [RECOMMENDED] G.TYP.02 Explicitly specify the type for numeric literals
31#![warn(clippy::default_numeric_fallback)]
32// [RECOMMENDED] G.TYP.03 Use `try_from` methods instead of relying on numeric boundaries for safe conversion
33#![warn(clippy::checked_conversions)]
34// [RECOMMENDED] G.TYP.BOL.02 Use `if` expressions instead of `match` for boolean conditions
35#![warn(clippy::match_bool)]
36// [RECOMMENDED] G.TYP.BOL.05 Use logical operators (&&/||) instead of bitwise operators (&/|) for boolean operations
37// when not necessary
38#![warn(clippy::needless_bitwise_bool)]
39// [RECOMMENDED] G.TYP.INT.01 Consider the risks of integer overflow, wrapping, and truncation in integer arithmetic
40#![warn(clippy::arithmetic_side_effects)]
41// [REQUIRED] G.TYP.INT.02 Avoid `as` casting between signed and unsigned integers; use safe conversion functions
42#![deny(clippy::cast_sign_loss)]
43// [REQUIRED] G.TYP.INT.03 Avoid using `%` for modulo operations on negative numbers
44#![warn(clippy::modulo_arithmetic)]
45// [REQUIRED] G.TYP.FLT.02 Avoid precision loss when casting from any numeric type to floating-point; use safe
46// conversion functions
47#![warn(clippy::cast_precision_loss)]
48// [REQUIRED] G.TYP.FLT.03 Be cautious of precision loss in floating-point arithmetic and comparisons
49#![warn(clippy::float_arithmetic, clippy::float_cmp, clippy::float_cmp_const)]
50// [REQUIRED] G.TYP.FLT.04 Use Rust's built-in methods for floating-point calculations
51#![warn(clippy::imprecise_flops, clippy::suboptimal_flops)]
52// [OPTIONAL] G.TYP.ARR.01 Use static variables instead of constants for large global arrays
53// #![warn(clippy::large_stack_arrays)]
54// [RECOMMENDED] G.TYP.SCT.01 Add `#[non_exhaustive]` attribute to publicly exported structs
55#![warn(clippy::exhaustive_structs)]
56// [RECOMMENDED] G.TYP.ENM.05 Add `#[non_exhaustive]` attribute to publicly exported enums
57#![warn(clippy::exhaustive_enums)]
58// [OPTIONAL] G.TYP.SCT.02 Consider refactoring when a struct contains more than three boolean fields
59// #![warn(clippy::struct_excessive_bools)]
60// [RECOMMENDED] G.FUD.03 Consider using a custom struct or enum instead of many boolean parameters in function
61// signatures
62#![warn(clippy::fn_params_excessive_bools)]
63// [RECOMMENDED] G.TYP.ENM.04 Avoid using glob imports for enum variants in `use` statements
64#![warn(clippy::enum_glob_use)]
65// [RECOMMENDED] G.CTF.02 Ensure `else` branches are present whenever `else if` is used
66#![warn(clippy::else_if_without_else)]
67// [OPTIONAL] G.STR.02 Use `push_str` method for appending strings
68// #![warn(clippy::string_add_assign, clippy::string_add)]
69// [RECOMMENDED] G.STR.03 Convert string literals containing only ASCII characters to byte sequences using `b"str"`
70// syntax instead of `as_bytes()`
71#![warn(clippy::string_lit_as_bytes)]
72// [RECOMMENDED] G.STR.05 Take care to avoid disrupting UTF-8 encoding when slicing strings at specific positions
73#![warn(clippy::string_slice)]
74// [RECOMMENDED] G.FUD.02 Prefer passing large values by reference if function parameters implement `Copy`
75#![warn(clippy::large_types_passed_by_value)]
76// [RECOMMENDED] G.FUD.04 Pass small `Copy` type values by value instead of by reference
77#![warn(clippy::trivially_copy_pass_by_ref)]
78// [OPTIONAL] G.FUD.05 Avoid using `inline(always)` for functions indiscriminately
79// #![warn(clippy::inline_always)]
80// [REQUIRED] G.GEN.02 Be cautious to avoid using generic default implementations of some methods from Rust's standard
81// library; prefer specific type implementations
82#![warn(clippy::inefficient_to_string)]
83// [OPTIONAL] G.TRA.BLN.01 Prefer using the concrete type's `default()` method over calling `Default::default()`
84// #![warn(clippy::default_trait_access)]
85// [REQUIRED] G.TRA.BLN.02 Do not implement the `Copy` trait for iterators
86#![warn(clippy::copy_iterator)]
87// [RECOMMENDED] G.TRA.BLN.07 Use `copied` method instead of `cloned` for iterable `Copy` types
88#![warn(clippy::cloned_instead_of_copied)]
89// [RECOMMENDED] G.ERR.01 Avoid using `unwrap` indiscriminately when handling `Option<T>` and `Result<T, E>`
90#![warn(clippy::unwrap_used)]
91// [RECOMMENDED] G.MOD.03 Avoid using wildcard imports in module declarations
92#![warn(clippy::wildcard_imports)]
93// [REQUIRED] G.MOD.04 Avoid using different module layout styles within the same project
94#![warn(clippy::self_named_module_files)]
95// [RECOMMENDED] G.CAR.02 Ensure that necessary metadata is included in the `Cargo.toml` of the crate
96#![warn(clippy::cargo_common_metadata)]
97// [RECOMMENDED] G.CAR.03 Avoid negative or redundant prefixes and suffixes in feature names
98#![warn(clippy::negative_feature_names, clippy::redundant_feature_names)]
99// [REQUIRED] G.CAR.04 Avoid using wildcard dependencies in `Cargo.toml`
100#![warn(clippy::wildcard_dependencies)]
101// [RECOMMENDED] G.MAC.01 Only use the `dbg!()` macro for debugging code
102#![warn(clippy::dbg_macro)]
103// [REQUIRED] Ensure that locks are released before `await` is called in asynchronous code
104#![warn(clippy::await_holding_lock)]
105// [REQUIRED] Handle `RefCell` references across `await` points
106#![warn(clippy::await_holding_refcell_ref)]
107// [RECOMMENDED] G.ASY.04 Avoid defining unnecessary async functions
108#![warn(clippy::unused_async)]
109// [REQUIRED] G.UNS.SAS.02 Use `assert!` instead of `debug_assert!` to verify boundary conditions in unsafe functions
110#![warn(clippy::debug_assert_with_mut_call)]
111
112mod macro_attr;
113use proc_macro::TokenStream;
114
115#[proc_macro_attribute]
116pub fn global_attributes(args: TokenStream, input: TokenStream) -> TokenStream {
117 let mut ts = macro_attr::global_rustc_clippy_attributes(args);
118 ts.extend(input);
119 ts
120}