rust-lang / rust-lang/rust-clippy

New lint: `path_attribute`

Open
#17,628 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Warns when the built-in #[path = "..."] attribute is used to override the source file of an out-of-line module.

Advantage
  • Using Rust's conventional module file layout makes the relationship between module names and source files predictable for readers and tooling.
Drawbacks

#[path] is useful for generated code, platform-specific implementations, tests, and projects with non-standard source layouts. This lint would therefore belong to the restriction group and remain allow-by-default.

Example
#[path = "implementation.rs"]
mod parser;

Could be written as:

mod parser;

with the implementation moved to parser.rs or parser/mod.rs.

Comparison with existing lints

Unlike self_named_module_files, mod_module_files, and inline_modules, this lint specifically detects use of the built-in #[path] attribute.
None of those lints prohibit overriding a module's source path.

Additional Context

This is useful for projects that enforce a conventional module layout in CI.
For example, in AI-assisted development workflows, generated patches may introduce #[path] even when the conventional layout would work.

Natural-language or contributor guidelines can discourage the pattern, but cannot enforce it across every change path.
An allow-by-default restriction lint provides a machine-enforceable policy.

A more general alternative would be a configurable disallowed_attributes lint analogous to disallowed_methods, disallowed_types, and disallowed_macros.
If that is preferable to a dedicated path_attribute lint, it would also address this use case.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing self_named_module_files, mod_module_files, and inline_modules lints and the proposal's disallowed_attributes alternative. Determine whether the change should be a dedicated path_attribute lint or a configurable mechanism, then verify that the selected approach warns on out-of-line modules using #[path] while remaining allow-by-default.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.