|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_help;
|
2 | 2 | use rustc_ast::{
|
3 |
| - ast::{ItemKind, UseTree, UseTreeKind}, |
4 |
| - Item, |
| 3 | + visit::{walk_item, Visitor}, |
| 4 | + *, |
5 | 5 | };
|
6 |
| -use rustc_hir::*; |
7 | 6 | use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
|
8 | 7 | use rustc_middle::lint::in_external_macro;
|
9 | 8 | use rustc_session::{declare_tool_lint, impl_lint_pass};
|
@@ -64,77 +63,70 @@ pub struct ExcessiveIndentation {
|
64 | 63 | impl EarlyLintPass for ExcessiveIndentation {
|
65 | 64 | fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
|
66 | 65 | match &item.kind {
|
67 |
| - // TODO: |
68 |
| - // Use ✅ |
69 |
| - // Static ❌ |
70 |
| - // Const ❌ |
71 |
| - // Fn ❌ |
72 |
| - // Mod ❌ |
73 |
| - // ForeignMod ❌ |
74 |
| - // Enum ❌ |
75 |
| - // Struct ❌ |
76 |
| - // Union ❌ |
77 |
| - // Trait ❌ |
78 |
| - // Impl ❌ |
79 |
| - // MacroDef ❓ |
80 |
| - // Others don't increase indentation (unless your formatting is really weird) so they won't be included. |
81 |
| - ItemKind::Use(use_tree) => walk_use_tree(self, cx, use_tree, 0), |
82 | 66 | _ => (),
|
83 | 67 | }
|
84 | 68 | }
|
85 | 69 | }
|
86 | 70 |
|
87 |
| -fn check_indent(conf: &mut ExcessiveIndentation, cx: &EarlyContext<'_>, span: Span, indent: u64) -> bool { |
88 |
| - if indent > conf.excessive_indentation_threshold { |
89 |
| - span_lint_and_help(cx, EXCESSIVE_INDENTATION, span, "a", None, "b"); |
90 |
| - |
91 |
| - return true; |
92 |
| - } |
93 |
| - |
94 |
| - false |
| 71 | +struct IndentationVisitor { |
| 72 | + indent: u64, |
95 | 73 | }
|
96 | 74 |
|
97 |
| -fn walk_use_tree(conf: &mut ExcessiveIndentation, cx: &EarlyContext<'_>, use_tree: &UseTree, indent: u64) { |
98 |
| - if check_indent(conf, cx, use_tree.span, indent) { |
99 |
| - return; |
100 |
| - } |
101 |
| - |
102 |
| - if let UseTreeKind::Nested(use_trees) = use_tree.kind.clone() { |
103 |
| - for use_tree in use_trees { |
104 |
| - walk_use_tree(conf, cx, &use_tree.0, indent + 1); |
105 |
| - } |
106 |
| - } |
107 |
| -} |
108 |
| - |
109 |
| -impl LateLintPass<'_> for ExcessiveIndentation { |
110 |
| - fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { |
111 |
| - if in_external_macro(cx.sess(), stmt.span) { |
112 |
| - return; |
113 |
| - } |
114 |
| - |
115 |
| - if let Ok(lines) = cx.sess().source_map().span_to_lines(stmt.span).map(|info| info.lines) { |
116 |
| - for line in &lines { |
117 |
| - let indentation = if self.excessive_width_ignore_indentation { |
118 |
| - line.start_col |
119 |
| - } else { |
120 |
| - CharPos::from_usize(0) |
121 |
| - }; |
122 |
| - |
123 |
| - // TODO: yeah, no. |
124 |
| - if (line.end_col.to_usize() |
125 |
| - - line.start_col.to_usize() * self.excessive_width_ignore_indentation as usize) |
126 |
| - > self.excessive_width_threshold as usize |
127 |
| - { |
128 |
| - span_lint_and_help( |
129 |
| - cx, |
130 |
| - EXCESSIVE_WIDTH, |
131 |
| - stmt.span, |
132 |
| - "this line is too long", |
133 |
| - None, |
134 |
| - "consider running rustfmt or refactoring this", |
135 |
| - ); |
136 |
| - } |
| 75 | +impl Visitor<'_> for IndentationVisitor { |
| 76 | + fn visit_crate(&mut self, krate: &Crate) { |
| 77 | + for item in krate.items { |
| 78 | + match item.kind { |
| 79 | + // TODO: |
| 80 | + // Use ✅ |
| 81 | + // Static ❌ |
| 82 | + // Const ❌ |
| 83 | + // Fn ❌ |
| 84 | + // Mod ❌ |
| 85 | + // ForeignMod ❌ |
| 86 | + // Enum ❌ |
| 87 | + // Struct ❌ |
| 88 | + // Union ❌ |
| 89 | + // Trait ❌ |
| 90 | + // Impl ❌ |
| 91 | + // MacroDef ❓ |
| 92 | + // Others don't increase indentation (unless your formatting is really weird) so they won't be included. |
| 93 | + ItemKind::Use(use_tree) => todo!(), |
| 94 | + _ => (), |
137 | 95 | }
|
138 | 96 | }
|
139 | 97 | }
|
140 | 98 | }
|
| 99 | + |
| 100 | +// impl LateLintPass<'_> for ExcessiveIndentation { |
| 101 | +// fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { |
| 102 | +// if in_external_macro(cx.sess(), stmt.span) { |
| 103 | +// return; |
| 104 | +// } |
| 105 | +// |
| 106 | +// if let Ok(lines) = cx.sess().source_map().span_to_lines(stmt.span).map(|info| info.lines) |
| 107 | +// { for line in &lines { |
| 108 | +// let indentation = if self.excessive_width_ignore_indentation { |
| 109 | +// line.start_col |
| 110 | +// } else { |
| 111 | +// CharPos::from_usize(0) |
| 112 | +// }; |
| 113 | +// |
| 114 | +// // TODO: yeah, no. |
| 115 | +// if (line.end_col.to_usize() |
| 116 | +// - line.start_col.to_usize() * self.excessive_width_ignore_indentation as |
| 117 | +// usize) |
| 118 | +// > self.excessive_width_threshold as usize |
| 119 | +// { |
| 120 | +// span_lint_and_help( |
| 121 | +// cx, |
| 122 | +// EXCESSIVE_WIDTH, |
| 123 | +// stmt.span, |
| 124 | +// "this line is too long", |
| 125 | +// None, |
| 126 | +// "consider running rustfmt or refactoring this", |
| 127 | +// ); |
| 128 | +// } |
| 129 | +// } |
| 130 | +// } |
| 131 | +// } |
| 132 | +// } |
0 commit comments