|
| 1 | +use clippy_config::Conf; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 3 | +use rustc_ast::Crate; |
| 4 | +use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; |
| 5 | +use rustc_session::impl_lint_pass; |
| 6 | +use rustc_span::def_id::LOCAL_CRATE; |
| 7 | +use rustc_span::{FileName, Span, SyntaxContext}; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// Checks for files that exceed a configurable line count threshold. |
| 12 | + /// |
| 13 | + /// Note: This is a restriction lint that is allow-by-default. You need to enable it |
| 14 | + /// explicitly in your configuration and set the threshold in clippy.toml. |
| 15 | + /// |
| 16 | + /// ### Why restrict this? |
| 17 | + /// Large files can be harder to navigate and understand. They often indicate that |
| 18 | + /// the code could benefit from being split into multiple smaller, more focused modules. |
| 19 | + /// This improves maintainability and makes the codebase easier to understand. |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// An example clippy.toml configuration: |
| 23 | + /// ```toml |
| 24 | + /// # clippy.toml |
| 25 | + /// excessive-file-length-threshold = 500 |
| 26 | + /// ``` |
| 27 | + /// |
| 28 | + /// If a file exceeds this threshold, the lint will suggest splitting it into |
| 29 | + /// smaller modules. |
| 30 | + #[clippy::version = "1.84.0"] |
| 31 | + pub EXCESSIVE_FILE_LENGTH, |
| 32 | + restriction, |
| 33 | + "checks for files that exceed a configurable line count threshold" |
| 34 | +} |
| 35 | + |
| 36 | +impl_lint_pass!(ExcessiveFileLength => [EXCESSIVE_FILE_LENGTH]); |
| 37 | + |
| 38 | +pub struct ExcessiveFileLength { |
| 39 | + pub excessive_file_length_threshold: u64, |
| 40 | +} |
| 41 | + |
| 42 | +impl ExcessiveFileLength { |
| 43 | + pub fn new(conf: &'static Conf) -> Self { |
| 44 | + Self { |
| 45 | + excessive_file_length_threshold: conf.excessive_file_length_threshold, |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl EarlyLintPass for ExcessiveFileLength { |
| 51 | + fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) { |
| 52 | + // Only check if threshold is set (non-zero) |
| 53 | + if self.excessive_file_length_threshold == 0 { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // Get all source files for the local crate |
| 58 | + let source_map = cx.sess().source_map(); |
| 59 | + |
| 60 | + // We want to check each file in the current crate |
| 61 | + for file in source_map.files().iter() { |
| 62 | + // Only check files from the local crate, not external dependencies |
| 63 | + if file.cnum != LOCAL_CRATE { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + // Skip non-real files (generated code, etc.) |
| 68 | + if !matches!(&file.name, FileName::Real(_)) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + // Count total lines in the file |
| 73 | + let line_count = file.count_lines() as u64; |
| 74 | + |
| 75 | + // Check if file exceeds threshold |
| 76 | + if line_count > self.excessive_file_length_threshold { |
| 77 | + // Create a span at the start of the file for the lint |
| 78 | + let span = Span::new( |
| 79 | + file.start_pos, |
| 80 | + file.start_pos, |
| 81 | + SyntaxContext::root(), |
| 82 | + None, |
| 83 | + ); |
| 84 | + |
| 85 | + span_lint_and_help( |
| 86 | + cx, |
| 87 | + EXCESSIVE_FILE_LENGTH, |
| 88 | + span, |
| 89 | + format!( |
| 90 | + "this file has too many lines ({}/{})", |
| 91 | + line_count, self.excessive_file_length_threshold |
| 92 | + ), |
| 93 | + None, |
| 94 | + "consider splitting this file into smaller modules to improve maintainability", |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments