Skip to content

New lint: too_many_lines_in_file — warn when a source file exceeds a configurable line count #16674

@tupe12334

Description

@tupe12334

What it does

Warns when a source file contains more lines of code than a configurable threshold. Comment-only lines and blank lines would be excluded from the count (mirroring the behavior of too_many_lines for functions).

Advantage

  • Encourages splitting large files into smaller, more focused modules
  • Complements the existing too_many_lines lint, which operates at the function level, by also enforcing hygiene at the file/module level
  • Large files are often a sign of poor separation of concerns and can hurt navigability and reviewability

Drawbacks

  • This is inherently a style/restriction lint — reasonable threshold values vary widely by project
  • Some files (e.g. generated code, large lookup tables) legitimately need to be long; users would need to suppress the lint for those
  • Could be noisy on large existing codebases if adopted without tuning the threshold

Example

This lint fires on a file as a whole rather than a specific expression, so there is no before/after code rewrite. The diagnostic would look like:

warning: this file has too many lines (1200/1000)
  --> src/my_module.rs:1:1
   |
 1 | // src/my_module.rs
   | ^
   = help: consider splitting the module into smaller submodules

The intended fix is to split the file into smaller, more focused modules.

Comparison with existing lints

  • too_many_lines — lints on individual functions exceeding a line threshold. This proposed lint operates at the file level, which is a coarser and complementary granularity.
  • No existing Clippy lint checks overall file length.

Additional Context

  • Should belong to the restriction group (opt-in), similar to too_many_lines
  • The threshold should be configurable via clippy.toml, e.g. too-many-lines-in-file-threshold = 1000 (default could be 1000)
  • Counting logic should match too_many_lines: skip blank lines and comment-only lines, count only lines with actual code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions