lint: allow self-documenting builder arguments (#27507)

Builder-style setters often repeat the setting name in both the method
and its sole argument. Calls such as `.enabled(false)` are already
self-documenting, so requiring `/*enabled*/` adds noise without
clarifying the call.

## What changed

- Exempt a method's sole non-self argument when its resolved parameter
name matches the method name.
- Continue validating any explicit argument comment against the resolved
parameter name.
- Continue requiring comments when method and parameter names differ or
when a method has multiple non-self arguments.
- Document the exception in `AGENTS.md` and the lint's own behavior
documentation.

## Examples

Before this change we'd need redundant comments like this:

```rust
builder.enabled(/*false*/ false);
builder.retry_count(/*retry_count*/ 3);
builder.base_url(/*base_url*/ None);
```

Now can be written like this:

```rust
builder.enabled(false);
builder.retry_count(3);
builder.base_url(None);
```

Still disallowed:

```rust
client.set_flag(true); // Method name does not match parameter `enabled`.
options.enabled(false, /*retry_count*/ 3); // More than one non-self argument.
options.enabled(/*value*/ false); // Explicit comment does not match `enabled`.
```

## Validation

Added UI coverage for boolean, numeric, and `None` builder arguments,
multi-argument methods, and explicit comment mismatches. Ran `rustup run
nightly-2025-09-18 cargo test` in `tools/argument-comment-lint`.
This commit is contained in:
Adam Perry @ OpenAI authored and GitHub committed 2026-06-11 10:24:42 -07:00
1 parent 9d88e299a7
commit 52db447c77
8 files changed
+109 -6

No files matched your search

@@ -0,0 +1,25 @@
#![warn(argument_comment_mismatch)]
#![warn(uncommented_anonymous_literal_argument)]
struct Builder;
impl Builder {
fn enabled(self, enabled: bool) -> Self {
let _ = enabled;
self
}
fn retry_count(self, retry_count: usize) -> Self {
let _ = retry_count;
self
}
fn base_url(self, base_url: Option<String>) -> Self {
let _ = base_url;
self
}
}
fn main() {
let _ = Builder.enabled(false).retry_count(3).base_url(None);
}
@@ -5,6 +5,16 @@ fn create_openai_url(base_url: Option<String>) -> String {
String::new()
}
struct Options;
impl Options {
fn enabled(self, enabled: bool) -> Self {
let _ = enabled;
self
}
}
fn main() {
let _ = create_openai_url(/*api_base*/ None);
let _ = Options.enabled(/*value*/ false);
}
@@ -1,5 +1,5 @@
warning: argument comment `/*api_base*/` does not match parameter `base_url`
--> $DIR/comment_mismatch.rs:9:44
--> $DIR/comment_mismatch.rs:18:44
|
LL | let _ = create_openai_url(/*api_base*/ None);
| ^^^^
@@ -11,5 +11,13 @@ note: the lint level is defined here
LL | #![warn(argument_comment_mismatch)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
warning: 1 warning emitted
warning: argument comment `/*value*/` does not match parameter `enabled`
--> $DIR/comment_mismatch.rs:19:39
|
LL | let _ = Options.enabled(/*value*/ false);
| ^^^^^
|
= help: use `/*enabled*/`
warning: 2 warnings emitted
@@ -0,0 +1,14 @@
#![warn(uncommented_anonymous_literal_argument)]
struct Options;
impl Options {
fn enabled(self, enabled: bool, retry_count: usize) -> Self {
let _ = (enabled, retry_count);
self
}
}
fn main() {
let _ = Options.enabled(false, /*retry_count*/ 3);
}
@@ -0,0 +1,14 @@
warning: anonymous literal-like argument for parameter `enabled`
--> $DIR/multiple_method_arguments.rs:13:29
|
LL | let _ = Options.enabled(false, /*retry_count*/ 3);
| ^^^^^ help: prepend the parameter name comment: `/*enabled*/ false`
|
note: the lint level is defined here
--> $DIR/multiple_method_arguments.rs:1:9
|
LL | #![warn(uncommented_anonymous_literal_argument)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: 1 warning emitted