1use syn::{Expr, Item, Local, Pat, Stmt, UseTree};
2
3#[derive(#[automatically_derived]
impl ::core::fmt::Debug for UseGroup {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
UseGroup::Std => "Std",
UseGroup::External => "External",
UseGroup::CrateLocal => "CrateLocal",
})
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for UseGroup {
#[inline]
fn clone(&self) -> UseGroup { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for UseGroup { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for UseGroup {
#[inline]
fn eq(&self, other: &UseGroup) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for UseGroup {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq)]
8pub enum UseGroup {
9 Std,
10 External,
11 CrateLocal,
12}
13
14pub fn classify_use(item: &syn::ItemUse) -> UseGroup {
15 let root_ident = use_tree_root_ident(&item.tree);
16 match root_ident.as_deref() {
17 Some("std" | "alloc" | "core") => UseGroup::Std,
18 Some("crate" | "super" | "self") => UseGroup::CrateLocal,
19 _ => UseGroup::External,
20 }
21}
22
23fn use_tree_root_ident(tree: &UseTree) -> Option<String> {
24 match tree {
25 UseTree::Path(path) => Some(path.ident.to_string()),
26 UseTree::Name(name) => Some(name.ident.to_string()),
27 UseTree::Rename(rename) => Some(rename.ident.to_string()),
28 UseTree::Glob(_) => None,
29 UseTree::Group(group) => group.items.first().and_then(use_tree_root_ident),
30 }
31}
32
33#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ItemKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
ItemKind::Use => "Use",
ItemKind::ExternCrate => "ExternCrate",
ItemKind::Mod => "Mod",
ItemKind::Const => "Const",
ItemKind::Static => "Static",
ItemKind::TypeAlias => "TypeAlias",
ItemKind::Definition => "Definition",
ItemKind::Other => "Other",
})
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for ItemKind {
#[inline]
fn clone(&self) -> ItemKind { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for ItemKind { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for ItemKind {
#[inline]
fn eq(&self, other: &ItemKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ItemKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq)]
38enum ItemKind {
39 Use,
40 ExternCrate,
41 Mod,
42 Const,
43 Static,
44 TypeAlias,
45 Definition,
46 Other,
47}
48
49fn classify_item_kind(item: &Item) -> ItemKind {
50 match item {
51 Item::Use(_) => ItemKind::Use,
52 Item::ExternCrate(_) => ItemKind::ExternCrate,
53 Item::Mod(_) => ItemKind::Mod,
54 Item::Const(_) => ItemKind::Const,
55 Item::Static(_) => ItemKind::Static,
56 Item::Type(_) => ItemKind::TypeAlias,
57 Item::Fn(_)
58 | Item::Struct(_)
59 | Item::Enum(_)
60 | Item::Union(_)
61 | Item::Trait(_)
62 | Item::TraitAlias(_)
63 | Item::Impl(_)
64 | Item::Macro(_) => ItemKind::Definition,
65 _ => ItemKind::Other,
66 }
67}
68
69pub fn should_blank_between_items(prev: &Item, next: &Item) -> bool {
70 let pk = classify_item_kind(prev);
71 let nk = classify_item_kind(next);
72
73 match (pk, nk) {
75 (ItemKind::Use, ItemKind::Use) => {
76 let prev_group = classify_use(match prev {
77 Item::Use(u) => u,
78 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
79 });
80 let next_group = classify_use(match next {
81 Item::Use(u) => u,
82 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
83 });
84 prev_group != next_group
85 }
86 (ItemKind::ExternCrate, ItemKind::ExternCrate) => false,
87 (ItemKind::Mod, ItemKind::Mod) => false,
88 (ItemKind::Const, ItemKind::Const) => false,
89 (ItemKind::Static, ItemKind::Static) => false,
90 (ItemKind::TypeAlias, ItemKind::TypeAlias) => false,
91 _ => true,
92 }
93}
94
95#[derive(#[automatically_derived]
impl ::core::fmt::Debug for StmtWeight {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
StmtWeight::Light => "Light",
StmtWeight::Binding => "Binding",
StmtWeight::Medium => "Medium",
StmtWeight::Heavy => "Heavy",
StmtWeight::Item => "Item",
})
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for StmtWeight {
#[inline]
fn clone(&self) -> StmtWeight { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for StmtWeight { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for StmtWeight {
#[inline]
fn eq(&self, other: &StmtWeight) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for StmtWeight {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq)]
100enum StmtWeight {
101 Light,
102 Binding,
103 Medium,
104 Heavy,
105 Item,
106}
107
108fn expr_is_heavy(expr: &Expr) -> bool {
109 match expr {
110 Expr::If(_)
111 | Expr::Match(_)
112 | Expr::ForLoop(_)
113 | Expr::While(_)
114 | Expr::Loop(_)
115 | Expr::Block(_)
116 | Expr::Unsafe(_)
117 | Expr::TryBlock(_) => true,
118 Expr::Closure(c) => #[allow(non_exhaustive_omitted_patterns)] match *c.body {
Expr::Block(_) => true,
_ => false,
}matches!(*c.body, Expr::Block(_)),
119 Expr::Assign(a) => expr_is_heavy(&a.right),
120 _ => false,
121 }
122}
123
124fn pat_contains_mut(pat: &Pat) -> bool {
125 match pat {
126 Pat::Ident(p) => p.mutability.is_some(),
127 Pat::Reference(p) => p.mutability.is_some() || pat_contains_mut(&p.pat),
128 Pat::Struct(p) => p.fields.iter().any(|f| pat_contains_mut(&f.pat)),
129 Pat::Tuple(p) => p.elems.iter().any(pat_contains_mut),
130 Pat::TupleStruct(p) => p.elems.iter().any(pat_contains_mut),
131 Pat::Slice(p) => p.elems.iter().any(pat_contains_mut),
132 Pat::Or(p) => p.cases.iter().any(pat_contains_mut),
133 Pat::Type(p) => pat_contains_mut(&p.pat),
134 _ => false,
135 }
136}
137
138fn classify_local(local: &Local) -> StmtWeight {
139 if let Some(init) = &local.init {
140 if expr_is_heavy(&init.expr) {
141 return StmtWeight::Heavy;
142 }
143 }
144 if pat_contains_mut(&local.pat) {
145 StmtWeight::Binding
146 } else {
147 StmtWeight::Light
148 }
149}
150
151#[derive(#[automatically_derived]
impl ::core::fmt::Debug for TracingLevel {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
TracingLevel::Trace => "Trace",
TracingLevel::Debug => "Debug",
TracingLevel::Info => "Info",
TracingLevel::Warn => "Warn",
TracingLevel::Error => "Error",
})
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for TracingLevel {
#[inline]
fn clone(&self) -> TracingLevel { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for TracingLevel { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for TracingLevel {
#[inline]
fn eq(&self, other: &TracingLevel) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for TracingLevel {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq)]
156enum TracingLevel {
157 Trace,
158 Debug,
159 Info,
160 Warn,
161 Error,
162}
163
164fn is_tracing_macro(expr: &Expr) -> Option<TracingLevel> {
165 if let Expr::Macro(m) = expr {
166 let name = m.mac.path.segments.last()?.ident.to_string();
167 match name.as_str() {
168 "trace" => Some(TracingLevel::Trace),
169 "debug" => Some(TracingLevel::Debug),
170 "info" => Some(TracingLevel::Info),
171 "warn" => Some(TracingLevel::Warn),
172 "error" => Some(TracingLevel::Error),
173 _ => None,
174 }
175 } else {
176 None
177 }
178}
179
180fn stmt_expr(stmt: &Stmt) -> Option<&Expr> {
181 match stmt {
182 Stmt::Expr(expr, _) => Some(expr),
183 _ => None,
184 }
185}
186
187fn tracing_blank_line(prev: &Stmt, next: &Stmt) -> Option<bool> {
189 if let Some(prev_expr) = stmt_expr(prev) {
191 if let Some(level) = is_tracing_macro(prev_expr) {
192 return Some(match level {
193 TracingLevel::Trace => false,
194 TracingLevel::Debug => true,
195 TracingLevel::Info | TracingLevel::Warn | TracingLevel::Error => true,
196 });
197 }
198 }
199
200 if let Some(next_expr) = stmt_expr(next) {
202 if let Some(level) = is_tracing_macro(next_expr) {
203 return Some(match level {
204 TracingLevel::Trace => true,
205 TracingLevel::Debug => false,
206 TracingLevel::Info | TracingLevel::Warn | TracingLevel::Error => true,
207 });
208 }
209 }
210
211 None
212}
213
214fn classify_weight(stmt: &Stmt) -> StmtWeight {
219 match stmt {
220 Stmt::Local(local) => classify_local(local),
221 Stmt::Expr(expr, _) => {
222 if expr_is_heavy(expr) {
223 StmtWeight::Heavy
224 } else {
225 StmtWeight::Medium
226 }
227 }
228 Stmt::Item(_) => StmtWeight::Item,
229 Stmt::Macro(_) => StmtWeight::Medium,
230 }
231}
232
233fn is_jump_stmt(stmt: &Stmt) -> bool {
234 #[allow(non_exhaustive_omitted_patterns)] match stmt {
Stmt::Expr(Expr::Return(_) | Expr::Continue(_) | Expr::Break(_), _) =>
true,
_ => false,
}matches!(
235 stmt,
236 Stmt::Expr(
237 Expr::Return(_) | Expr::Continue(_) | Expr::Break(_),
238 _
239 )
240 )
241}
242
243fn is_let_else(stmt: &Stmt) -> bool {
244 if let Stmt::Local(local) = stmt {
245 if let Some(init) = &local.init {
246 return init.diverge.is_some();
247 }
248 }
249 false
250}
251
252fn should_blank_between_stmts(prev: &Stmt, next: &Stmt) -> bool {
253 if let Some(decision) = tracing_blank_line(prev, next) {
255 return decision;
256 }
257
258 if is_jump_stmt(next) {
260 return true;
261 }
262
263 if is_let_else(next) {
265 return true;
266 }
267
268 let pw = classify_weight(prev);
269 let nw = classify_weight(next);
270
271 if nw == StmtWeight::Item || pw == StmtWeight::Item {
273 return true;
274 }
275
276 if pw == StmtWeight::Heavy || nw == StmtWeight::Heavy {
278 return true;
279 }
280
281 if pw == nw {
283 return false;
284 }
285
286 true
288}
289
290pub fn stmt_blank_lines(stmts: &[Stmt]) -> Vec<bool> {
294 let len = stmts.len();
295 let mut blanks = ::alloc::vec::from_elem(false, len)vec![false; len];
296 if len <= 1 {
297 return blanks;
298 }
299 for i in 1..len {
300 blanks[i] = should_blank_between_stmts(&stmts[i - 1], &stmts[i]);
301 }
302 blanks
303}