rust-lang / rust-lang/rust-clippy

`pub` in executable modules

Open
#13,644 1 comment 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

Warn of uses of pub in the top-level module of an executable-only target.

Specifically, pub outside of a mod block in any of these files would trigger the lint:

  • src/main.rs
  • src/bin/foo.rs
  • src/bin/foo/main.rs
Advantage
  • pub suppresses "unused" lints, so inadvertently marking something pub may mask that the user forgot to use it.
  • pub has no other effect (that I'm aware of) in top-level modules of executable-only targets.

I actually ran into this recently: I work on a project that has several executable targets, and I discovered that two of them were not actually using all of their declared arguments.

Drawbacks

There may be a use for pub inside of an executable-only module that I'm not aware of.

Example
use clap::Parser;

#[derive(Parser, Debug)]
struct Args {
    #[arg(short, long)]
    pub name: String,
}

Could be written as:

use clap::Parser;

#[derive(Parser, Debug)]
struct Args {
    #[arg(short, long)]
    name: String,
}

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

The issue identifies executable entry points as src/main.rs, src/bin/foo.rs, and src/bin/foo/main.rs; start by checking how these module forms are represented. Define the lint behavior for pub outside mod blocks and verify it against the Args example, including that pub inside a mod is excluded. No test file or implementation entry point is named, so locate the relevant Clippy lint tests before confirming warning and non-warning cases.

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
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.