- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.8k
          Add excessive_file_length lint
          #15922
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            andrewgazelka
  wants to merge
  1
  commit into
  rust-lang:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
andrewgazelka:excessive_line_count
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| use clippy_config::Conf; | ||
| use clippy_utils::diagnostics::span_lint_and_help; | ||
| use rustc_ast::Crate; | ||
| use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; | ||
| use rustc_session::impl_lint_pass; | ||
| use rustc_span::def_id::LOCAL_CRATE; | ||
| use rustc_span::{FileName, Span, SyntaxContext}; | ||
|  | ||
| declare_clippy_lint! { | ||
| /// ### What it does | ||
| /// Checks for files that exceed a configurable line count threshold. | ||
| /// | ||
| /// Note: This is a restriction lint that is allow-by-default. You need to enable it | ||
| /// explicitly in your configuration and set the threshold in clippy.toml. | ||
| /// | ||
| /// ### Why restrict this? | ||
| /// Large files can be harder to navigate and understand. They often indicate that | ||
| /// the code could benefit from being split into multiple smaller, more focused modules. | ||
| /// This improves maintainability and makes the codebase easier to understand. | ||
| /// | ||
| /// ### Example | ||
| /// An example clippy.toml configuration: | ||
| /// ```toml | ||
| /// # clippy.toml | ||
| /// excessive-file-length-threshold = 500 | ||
| /// ``` | ||
| /// | ||
| /// If a file exceeds this threshold, the lint will suggest splitting it into | ||
| /// smaller modules. | ||
| #[clippy::version = "1.84.0"] | ||
| pub EXCESSIVE_FILE_LENGTH, | ||
| restriction, | ||
| "checks for files that exceed a configurable line count threshold" | ||
| } | ||
|  | ||
| impl_lint_pass!(ExcessiveFileLength => [EXCESSIVE_FILE_LENGTH]); | ||
|  | ||
| pub struct ExcessiveFileLength { | ||
| pub excessive_file_length_threshold: u64, | ||
| } | ||
|  | ||
| impl ExcessiveFileLength { | ||
| pub fn new(conf: &'static Conf) -> Self { | ||
| Self { | ||
| excessive_file_length_threshold: conf.excessive_file_length_threshold, | ||
| } | ||
| } | ||
| } | ||
|  | ||
| impl EarlyLintPass for ExcessiveFileLength { | ||
| fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) { | ||
| // Only check if threshold is set (non-zero) | ||
| if self.excessive_file_length_threshold == 0 { | ||
| return; | ||
| } | ||
|  | ||
| // Get all source files for the local crate | ||
| let source_map = cx.sess().source_map(); | ||
|  | ||
| // We want to check each file in the current crate | ||
| for file in source_map.files().iter() { | ||
| // Only check files from the local crate, not external dependencies | ||
| if file.cnum != LOCAL_CRATE { | ||
| continue; | ||
| } | ||
|  | ||
| // Skip non-real files (generated code, etc.) | ||
| if !matches!(&file.name, FileName::Real(_)) { | ||
| continue; | ||
| } | ||
|  | ||
| // Count total lines in the file | ||
| let line_count = file.count_lines() as u64; | ||
|  | ||
| // Check if file exceeds threshold | ||
| if line_count > self.excessive_file_length_threshold { | ||
| // Create a span at the start of the file for the lint | ||
| let span = Span::new( | ||
| file.start_pos, | ||
| file.start_pos, | ||
| SyntaxContext::root(), | ||
| None, | ||
| ); | ||
|  | ||
| span_lint_and_help( | ||
| cx, | ||
| EXCESSIVE_FILE_LENGTH, | ||
| span, | ||
| format!( | ||
| "this file has too many lines ({}/{})", | ||
| line_count, self.excessive_file_length_threshold | ||
| ), | ||
| None, | ||
| "consider splitting this file into smaller modules to improve maintainability", | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1 @@ | ||
| excessive-file-length-threshold = 10 | 
        
          
          
            16 changes: 16 additions & 0 deletions
          
          16 
        
  tests/ui-toml/excessive_file_length/excessive_file_length.rs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #![warn(clippy::excessive_file_length)] | ||
| //~^ ERROR: this file has too many lines | ||
|  | ||
| // This file should trigger the lint because it has more than 10 lines | ||
| // (configured in clippy.toml) | ||
|  | ||
| fn main() { | ||
| println!("Line 1"); | ||
| println!("Line 2"); | ||
| println!("Line 3"); | ||
| println!("Line 4"); | ||
| println!("Line 5"); | ||
| println!("Line 6"); | ||
| println!("Line 7"); | ||
| // This file now has more than 10 lines and should trigger the lint | ||
| } | 
        
          
          
            12 changes: 12 additions & 0 deletions
          
          12 
        
  tests/ui-toml/excessive_file_length/excessive_file_length.stderr
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| error: this file has too many lines (16/10) | ||
| --> tests/ui-toml/excessive_file_length/excessive_file_length.rs:1:1 | ||
| | | ||
| LL | #![warn(clippy::excessive_file_length)] | ||
| | ^ | ||
| | | ||
| = help: consider splitting this file into smaller modules to improve maintainability | ||
| = note: `-D clippy::excessive-file-length` implied by `-D warnings` | ||
| = help: to override `-D warnings` add `#[allow(clippy::excessive_file_length)]` | ||
|  | ||
| error: aborting due to 1 previous error | ||
|  | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              Empty file.
          
    
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use
span_lint_hir_and_thenhere, that way you can use#[expect]&#[allow]. Now getting an HirId might be a bit tricky. If you can, maybe use the HirId of themoditem?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could probably use a
Visitorto search the HIR before linting, either incheck_crate+check_crate_postor in the same fn (justcheck_crate). From there you just store each module definition's name &HirId. There are other ways but AFAIK this should be the simplest.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it'd be quicker to just use
check_item+check_crate_post