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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use std::collections::BTreeMap;
use std::sync::{Arc, LazyLock};
use serde::Deserialize;
use super::gate::GateConfig;
use super::naming::{NamingCheck, NamingOverride};
use super::pattern::{PatternCheck, PatternOverride};
use super::string_list::deserialize_arc_str_slice;
/// One `item-visibility-policy` rule: a named item at a path must have an
/// exact visibility.
#[derive(Debug, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct ItemVisibilityRule {
/// Repository-relative source path the item must live in.
pub path: Box<str>,
/// Item kind: `struct`, `enum`, `union`, `trait`, or `fn`.
pub kind: Box<str>,
/// Exact item name.
pub name: Box<str>,
/// Required visibility: `private`, `pub`, `pub(crate)`, `pub(super)`,
/// or `pub(in <path>)`.
pub visibility: Box<str>,
}
/// One `feature-boundary` rule: a package feature must obey the named invariant.
#[derive(Debug, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct FeatureBoundaryRule {
/// The package whose feature is constrained.
pub package: Box<str>,
/// The feature name the rule applies to.
pub feature: Box<str>,
/// `no-default` (must not be reachable from any default feature) or
/// `dev-only` (may be enabled only through dev-dependency edges).
pub rule: Box<str>,
}
/// One `flat-module-family` rule: a prefixed module family under `parent` must
/// live below `parent/package_root/`.
#[derive(Debug, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct FlatModuleFamily {
/// Directory (repo-relative) whose direct children are checked.
pub parent: Box<str>,
/// Sub-directory of `parent` where the family must live.
pub package_root: Box<str>,
/// Module-name prefix identifying family members.
pub prefix: Box<str>,
}
/// Deserialized `.pedant.toml` file with all check settings.
#[derive(Debug, Deserialize, Default)]
#[serde(deny_unknown_fields)]
pub struct ConfigFile {
/// Security gate rules configuration.
#[serde(default)]
pub gate: GateConfig,
/// Depth limit for nesting checks (default: 3).
#[serde(default = "default_max_depth")]
pub max_depth: usize,
/// Branch count that triggers `else-chain` (default: 3).
#[serde(default = "default_else_chain_threshold")]
pub else_chain_threshold: usize,
/// Maximum parameter count before `high-param-count` fires (default: 5).
#[serde(default = "default_max_params")]
pub max_params: usize,
/// Body line count before `long-function-body` fires (default: 120).
#[serde(default = "default_max_function_body_lines")]
pub max_function_body_lines: usize,
/// File names treated as module roots by `module-root-definitions`
/// (default: `mod.rs`, `lib.rs`).
#[serde(
default = "default_module_root_files",
deserialize_with = "deserialize_arc_str_slice"
)]
pub module_root_files: Arc<[Arc<str>]>,
/// Line count at which `large-source-file` emits a `Warn` (default: 500).
#[serde(default = "default_source_file_warn_lines")]
pub source_file_warn_lines: usize,
/// Line count at which `large-source-file` emits a `Deny` (default: 1000).
#[serde(default = "default_source_file_deny_lines")]
pub source_file_deny_lines: usize,
/// Inherent-method count before `high-method-count` fires (default: 40).
#[serde(default = "default_max_methods")]
pub max_methods: usize,
/// Banned attribute patterns (e.g., `allow(dead_code)`).
#[serde(default)]
pub forbid_attributes: PatternCheck,
/// Banned type patterns (e.g., `Arc<String>`).
#[serde(default)]
pub forbid_types: PatternCheck,
/// Banned method call patterns (e.g., `.unwrap()`).
#[serde(default)]
pub forbid_calls: PatternCheck,
/// Banned macro patterns (e.g., `panic!`).
#[serde(default)]
pub forbid_macros: PatternCheck,
/// Thresholds for the generic-naming check.
#[serde(default)]
pub check_naming: NamingCheck,
/// Flag `if` inside `if`.
#[serde(default = "default_true")]
pub check_nested_if: bool,
/// Flag `if` inside `match` arm.
#[serde(default = "default_true")]
pub check_if_in_match: bool,
/// Flag `match` inside `match`.
#[serde(default = "default_true")]
pub check_nested_match: bool,
/// Flag `match` inside `if` branch.
#[serde(default = "default_true")]
pub check_match_in_if: bool,
/// Flag long `if/else if` chains.
#[serde(default = "default_true")]
pub check_else_chain: bool,
/// Flag any use of the `else` keyword.
#[serde(default)]
pub forbid_else: bool,
/// Flag any `unsafe` block.
#[serde(default = "default_true")]
pub forbid_unsafe: bool,
/// Flag dynamic dispatch in return types.
#[serde(default)]
pub check_dyn_return: bool,
/// Flag dynamic dispatch in function parameters.
#[serde(default)]
pub check_dyn_param: bool,
/// Flag `Vec<Box<dyn T>>` anywhere.
#[serde(default)]
pub check_vec_box_dyn: bool,
/// Flag dynamic dispatch in struct fields.
#[serde(default)]
pub check_dyn_field: bool,
/// Flag `.clone()` inside loop bodies.
#[serde(default)]
pub check_clone_in_loop: bool,
/// Flag `HashMap`/`HashSet` with default SipHash hasher.
#[serde(default)]
pub check_default_hasher: bool,
/// Flag disconnected type groups in a single file.
#[serde(default)]
pub check_mixed_concerns: bool,
/// Flag `#[cfg(test)] mod` blocks embedded in source files.
#[serde(default)]
pub check_inline_tests: bool,
/// Flag `let _ = expr` that discards a Result.
#[serde(default)]
pub check_let_underscore_result: bool,
/// Flag functions with too many parameters.
#[serde(default)]
pub check_high_param_count: bool,
/// Flag function bodies that exceed the line ceiling.
#[serde(default)]
pub check_long_function_body: bool,
/// Flag item definitions in module-root files.
#[serde(default)]
pub check_module_root_definitions: bool,
/// Flag source files that exceed the line ceiling.
#[serde(default)]
pub check_large_source_file: bool,
/// Flag god-object types by inherent-method count.
#[serde(default)]
pub check_high_method_count: bool,
/// Count pure forwarders toward `high-method-count`.
#[serde(default)]
pub count_forwarders: bool,
/// Enforce configured item-visibility policies.
#[serde(default = "default_true")]
pub check_item_visibility_policy: bool,
/// Item-visibility policy rules.
#[serde(default)]
pub item_visibility_policy: Vec<ItemVisibilityRule>,
/// Flag ungated test-only APIs under `src/`.
#[serde(default)]
pub check_ungated_test_api: bool,
/// Flag sibling `<stem>.rs` and `<stem>/` module roots.
#[serde(default)]
pub check_conflicting_module_root: bool,
/// Name globs that mark test-only APIs (default: `*_for_tests`).
#[serde(
default = "default_test_api_patterns",
deserialize_with = "deserialize_arc_str_slice"
)]
pub test_api_patterns: Arc<[Arc<str>]>,
/// Feature that must gate a test-only API (default: `test-support`).
#[serde(default = "default_test_support_feature")]
pub test_support_feature: Box<str>,
/// Enforce configured flat-module-family layout rules.
#[serde(default = "default_true")]
pub check_flat_module_family: bool,
/// Flat-module-family layout rules.
#[serde(default)]
pub flat_module_families: Vec<FlatModuleFamily>,
/// Enforce configured Cargo feature-boundary invariants.
#[serde(default = "default_true")]
pub check_feature_boundary: bool,
/// Cargo feature-boundary invariants.
#[serde(default)]
pub feature_boundaries: Vec<FeatureBoundaryRule>,
/// Flag types whose inherent impls span more than one file.
#[serde(default)]
pub check_scattered_inherent_impl: bool,
/// Per-path configuration overrides keyed by glob pattern.
#[serde(default)]
pub overrides: BTreeMap<Box<str>, PathOverride>,
}
/// Per-path overrides (e.g., for `tests/**`). `None` inherits from base config.
#[derive(Debug, Deserialize, Default)]
#[serde(deny_unknown_fields)]
pub struct PathOverride {
/// `Some(false)` disables all checks for matched paths.
pub enabled: Option<bool>,
/// Replace nesting depth limit.
pub max_depth: Option<usize>,
/// Replace maximum parameter count.
pub max_params: Option<usize>,
/// Replace function body line ceiling.
pub max_function_body_lines: Option<usize>,
/// Replace the `large-source-file` warning line ceiling. Pair with a
/// TOML comment recording why this path is allowed to be large.
pub source_file_warn_lines: Option<usize>,
/// Replace the `large-source-file` denial line ceiling.
pub source_file_deny_lines: Option<usize>,
/// Replace the `high-method-count` method ceiling.
pub max_methods: Option<usize>,
/// Replace forbidden attribute patterns.
pub forbid_attributes: Option<PatternOverride>,
/// Replace forbidden type patterns.
pub forbid_types: Option<PatternOverride>,
/// Replace forbidden call patterns.
pub forbid_calls: Option<PatternOverride>,
/// Replace forbidden macro patterns.
pub forbid_macros: Option<PatternOverride>,
/// Replace generic naming thresholds.
pub check_naming: Option<NamingOverride>,
/// Replace nested-if check state.
pub check_nested_if: Option<bool>,
/// Replace if-in-match check state.
pub check_if_in_match: Option<bool>,
/// Replace nested-match check state.
pub check_nested_match: Option<bool>,
/// Replace match-in-if check state.
pub check_match_in_if: Option<bool>,
/// Replace else-chain check state.
pub check_else_chain: Option<bool>,
/// Replace `else` keyword ban state.
pub forbid_else: Option<bool>,
/// Replace `unsafe` block ban state.
pub forbid_unsafe: Option<bool>,
/// Replace dyn-return check state.
pub check_dyn_return: Option<bool>,
/// Replace dyn-param check state.
pub check_dyn_param: Option<bool>,
/// Replace `Vec<Box<dyn T>>` check state.
pub check_vec_box_dyn: Option<bool>,
/// Replace dyn-field check state.
pub check_dyn_field: Option<bool>,
/// Replace clone-in-loop check state.
pub check_clone_in_loop: Option<bool>,
/// Replace default-hasher check state.
pub check_default_hasher: Option<bool>,
/// Replace mixed-concerns check state.
pub check_mixed_concerns: Option<bool>,
/// Replace inline-tests check state.
pub check_inline_tests: Option<bool>,
/// Replace let-underscore-result check state.
pub check_let_underscore_result: Option<bool>,
/// Replace high-param-count check state.
pub check_high_param_count: Option<bool>,
/// Replace long-function-body check state.
pub check_long_function_body: Option<bool>,
/// Replace module-root-definitions check state.
pub check_module_root_definitions: Option<bool>,
/// Replace large-source-file check state.
pub check_large_source_file: Option<bool>,
/// Replace high-method-count check state.
pub check_high_method_count: Option<bool>,
/// Replace the forwarder-counting policy.
pub count_forwarders: Option<bool>,
/// Replace item-visibility-policy check state.
pub check_item_visibility_policy: Option<bool>,
/// Replace ungated-test-api check state.
pub check_ungated_test_api: Option<bool>,
/// Replace conflicting-module-root check state.
pub check_conflicting_module_root: Option<bool>,
/// Replace flat-module-family check state.
pub check_flat_module_family: Option<bool>,
/// Replace feature-boundary check state.
pub check_feature_boundary: Option<bool>,
/// Replace scattered-inherent-impl check state.
pub check_scattered_inherent_impl: Option<bool>,
}
pub(super) fn default_max_depth() -> usize {
3
}
pub(super) fn default_else_chain_threshold() -> usize {
3
}
pub(super) fn default_max_params() -> usize {
5
}
pub(super) fn default_max_function_body_lines() -> usize {
120
}
static MODULE_ROOT_FILES_ARC: LazyLock<Arc<[Arc<str>]>> =
LazyLock::new(|| [Arc::from("mod.rs"), Arc::from("lib.rs")].into());
pub(super) fn default_module_root_files() -> Arc<[Arc<str>]> {
Arc::clone(&MODULE_ROOT_FILES_ARC)
}
pub(super) fn default_source_file_warn_lines() -> usize {
500
}
pub(super) fn default_source_file_deny_lines() -> usize {
1000
}
pub(super) fn default_max_methods() -> usize {
40
}
static TEST_API_PATTERNS_ARC: LazyLock<Arc<[Arc<str>]>> =
LazyLock::new(|| [Arc::from("*_for_tests")].into());
pub(super) fn default_test_api_patterns() -> Arc<[Arc<str>]> {
Arc::clone(&TEST_API_PATTERNS_ARC)
}
pub(super) fn default_test_support_feature() -> Box<str> {
"test-support".into()
}
fn default_true() -> bool {
true
}