|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use rustc_hir::*; |
| 3 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 4 | +use rustc_middle::lint::in_external_macro; |
| 5 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 6 | +use rustc_span::Pos; |
| 7 | + |
| 8 | +// TODO: This still needs to be implemented. |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// |
| 12 | + /// Checks for lines which are indented beyond a certain threshold. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// |
| 16 | + /// It can severely hinder readability. The default is very generous; if you |
| 17 | + /// exceed this, it's a sign you should refactor. |
| 18 | + /// |
| 19 | + /// ### Example |
| 20 | + /// TODO |
| 21 | + /// Use instead: |
| 22 | + /// TODO |
| 23 | + #[clippy::version = "1.70.0"] |
| 24 | + pub EXCESSIVE_INDENTATION, |
| 25 | + style, |
| 26 | + "check for lines intended beyond a certain threshold" |
| 27 | +} |
| 28 | +declare_clippy_lint! { |
| 29 | + /// ### What it does |
| 30 | + /// |
| 31 | + /// Checks for lines which are longer than a certain threshold. |
| 32 | + /// |
| 33 | + /// ### Why is this bad? |
| 34 | + /// |
| 35 | + /// It can severely hinder readability. Almost always, running rustfmt will get this |
| 36 | + /// below this threshold (or whatever you have set as max_width), but if it fails, |
| 37 | + /// it's probably a sign you should refactor. |
| 38 | + /// |
| 39 | + /// ### Example |
| 40 | + /// TODO |
| 41 | + /// Use instead: |
| 42 | + /// TODO |
| 43 | + #[clippy::version = "1.70.0"] |
| 44 | + pub EXCESSIVE_WIDTH, |
| 45 | + style, |
| 46 | + "check for lines longer than a certain threshold" |
| 47 | +} |
| 48 | +impl_lint_pass!(ExcessiveWidth => [EXCESSIVE_INDENTATION, EXCESSIVE_WIDTH]); |
| 49 | + |
| 50 | +#[derive(Clone, Copy)] |
| 51 | +pub struct ExcessiveWidth { |
| 52 | + pub excessive_width_threshold: u64, |
| 53 | + pub excessive_width_ignore_indentation: bool, |
| 54 | + pub excessive_indentation_threshold: u64, |
| 55 | +} |
| 56 | + |
| 57 | +impl LateLintPass<'_> for ExcessiveWidth { |
| 58 | + fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { |
| 59 | + if in_external_macro(cx.sess(), stmt.span) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if let Ok(lines) = cx.sess().source_map().span_to_lines(stmt.span).map(|info| info.lines) { |
| 64 | + for line in &lines { |
| 65 | + // TODO: yeah, no. |
| 66 | + if (line.end_col.to_usize() |
| 67 | + - line.start_col.to_usize() * self.excessive_width_ignore_indentation as usize) |
| 68 | + > self.excessive_width_threshold as usize |
| 69 | + { |
| 70 | + span_lint_and_help( |
| 71 | + cx, |
| 72 | + EXCESSIVE_WIDTH, |
| 73 | + stmt.span, |
| 74 | + "this line is too long", |
| 75 | + None, |
| 76 | + "consider running rustfmt or refactoring this", |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments