refactor: clarify ambiguous literal argument APIs

Use named enums and methods where this follow-up touches real code,
and reframe the new argument-comment lint examples around legacy APIs
so the docs do not present `bool` and `Option` positional literals as the
preferred design.

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
Josh McKinney
2026-03-13 19:32:57 -07:00
co-authored by Codex
parent 4e594bc93b
commit 300d93ed36
15 changed files with 151 additions and 62 deletions
+25 -6
View File
@@ -16,31 +16,50 @@ self-descriptive at the callsite.
## Behavior
Given:
When you own the API, prefer a clearer shape over positional literal arguments:
```rust
fn create_openai_url(base_url: Option<String>, retry_count: usize) -> String {
enum BaseUrl {
Default,
Custom(String),
}
struct RetryCount(usize);
fn create_openai_url(base_url: BaseUrl, retry_count: RetryCount) -> String {
let _ = (base_url, retry_count);
String::new()
}
```
This is accepted:
```rust
create_openai_url(BaseUrl::Default, RetryCount(3));
```
When a minimal refactor needs to keep a legacy signature, `/*param=*/` comments
make those call sites readable:
```rust
create_openai_url(/*base_url=*/ None, /*retry_count=*/ 3);
fn legacy_create_openai_url(base_url: Option<String>, retry_count: usize) -> String {
let _ = (base_url, retry_count);
String::new()
}
```
```rust
legacy_create_openai_url(/*base_url=*/ None, /*retry_count=*/ 3);
```
This is warned on by `argument_comment_mismatch`:
```rust
create_openai_url(/*api_base=*/ None, 3);
legacy_create_openai_url(/*api_base=*/ None, 3);
```
This is only warned on when `uncommented_anonymous_literal_argument` is enabled:
```rust
create_openai_url(None, 3);
legacy_create_openai_url(None, 3);
```
## Development
+14 -8
View File
@@ -58,24 +58,28 @@ rustc_session::declare_lint! {
/// This lint only runs when the callee resolves to a concrete function or
/// method with available parameter names.
///
/// If you own the API, prefer a clearer signature such as enums, named
/// methods, or newtypes. This lint is primarily for legacy signatures
/// where a minimal refactor is the better tradeoff.
///
/// ### Example
///
/// ```rust
/// fn create_openai_url(base_url: Option<String>) -> String {
/// fn legacy_create_openai_url(base_url: Option<String>) -> String {
/// String::new()
/// }
///
/// create_openai_url(/*api_base=*/ None);
/// legacy_create_openai_url(/*api_base=*/ None);
/// ```
///
/// Use instead:
///
/// ```rust
/// fn create_openai_url(base_url: Option<String>) -> String {
/// fn legacy_create_openai_url(base_url: Option<String>) -> String {
/// String::new()
/// }
///
/// create_openai_url(/*base_url=*/ None);
/// legacy_create_openai_url(/*base_url=*/ None);
/// ```
pub ARGUMENT_COMMENT_MISMATCH,
Warn,
@@ -96,25 +100,27 @@ rustc_session::declare_lint! {
/// ### Known problems
///
/// This lint is opinionated, so it is `allow` by default.
/// Prefer reshaping APIs you control instead of relying on comments for
/// new code; this lint exists for legacy call sites and minimal refactors.
///
/// ### Example
///
/// ```rust
/// fn create_openai_url(base_url: Option<String>) -> String {
/// fn legacy_create_openai_url(base_url: Option<String>) -> String {
/// String::new()
/// }
///
/// create_openai_url(None);
/// legacy_create_openai_url(None);
/// ```
///
/// Use instead:
///
/// ```rust
/// fn create_openai_url(base_url: Option<String>) -> String {
/// fn legacy_create_openai_url(base_url: Option<String>) -> String {
/// String::new()
/// }
///
/// create_openai_url(/*base_url=*/ None);
/// legacy_create_openai_url(/*base_url=*/ None);
/// ```
pub UNCOMMENTED_ANONYMOUS_LITERAL_ARGUMENT,
Allow,
@@ -1,12 +1,12 @@
#![warn(argument_comment_mismatch)]
fn create_openai_url(base_url: Option<String>, retry_count: usize) -> String {
fn legacy_create_openai_url(base_url: Option<String>, retry_count: usize) -> String {
let _ = (base_url, retry_count);
String::new()
}
fn main() {
let base_url = Some(String::from("https://api.openai.com"));
create_openai_url(base_url, 3);
create_openai_url(/*base_url=*/ None, 3);
legacy_create_openai_url(base_url, 3);
legacy_create_openai_url(/*base_url=*/ None, 3);
}
@@ -1,13 +1,13 @@
#![warn(argument_comment_mismatch)]
#![warn(uncommented_anonymous_literal_argument)]
fn run_git_for_stdout(repo_root: &str, args: Vec<&str>, env: Option<&str>) -> String {
fn legacy_run_git_for_stdout(repo_root: &str, args: Vec<&str>, env: Option<&str>) -> String {
let _ = (repo_root, args, env);
String::new()
}
fn main() {
let _ = run_git_for_stdout(
let _ = legacy_run_git_for_stdout(
"/tmp/repo",
vec!["rev-parse", "HEAD"],
/*env=*/ None,
@@ -1,10 +1,10 @@
#![warn(argument_comment_mismatch)]
fn create_openai_url(base_url: Option<String>) -> String {
fn legacy_create_openai_url(base_url: Option<String>) -> String {
let _ = base_url;
String::new()
}
fn main() {
let _ = create_openai_url(/*api_base=*/ None);
let _ = legacy_create_openai_url(/*api_base=*/ None);
}
@@ -1,8 +1,8 @@
warning: argument comment `/*api_base=*/` does not match parameter `base_url`
--> $DIR/comment_mismatch.rs:9:45
--> $DIR/comment_mismatch.rs:9:52
|
LL | let _ = create_openai_url(/*api_base=*/ None);
| ^^^^
LL | let _ = legacy_create_openai_url(/*api_base=*/ None);
| ^^^^
|
= help: use `/*base_url=*/`
note: the lint level is defined here
@@ -3,16 +3,16 @@
struct Client;
impl Client {
fn set_flag(&self, enabled: bool) {}
fn set_legacy_flag(&self, enabled: bool) {}
}
fn create_openai_url(base_url: Option<String>, retry_count: usize) -> String {
fn legacy_create_openai_url(base_url: Option<String>, retry_count: usize) -> String {
let _ = (base_url, retry_count);
String::new()
}
fn main() {
let client = Client;
let _ = create_openai_url(None, 3);
client.set_flag(true);
let _ = legacy_create_openai_url(None, 3);
client.set_legacy_flag(true);
}
@@ -1,8 +1,8 @@
warning: anonymous literal-like argument for parameter `base_url`
--> $DIR/uncommented_literal.rs:16:31
--> $DIR/uncommented_literal.rs:16:38
|
LL | let _ = create_openai_url(None, 3);
| ^^^^ help: prepend the parameter name comment: `/*base_url=*/ None`
LL | let _ = legacy_create_openai_url(None, 3);
| ^^^^ help: prepend the parameter name comment: `/*base_url=*/ None`
|
note: the lint level is defined here
--> $DIR/uncommented_literal.rs:1:9
@@ -11,16 +11,16 @@ LL | #![warn(uncommented_anonymous_literal_argument)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: anonymous literal-like argument for parameter `retry_count`
--> $DIR/uncommented_literal.rs:16:37
--> $DIR/uncommented_literal.rs:16:44
|
LL | let _ = create_openai_url(None, 3);
| ^ help: prepend the parameter name comment: `/*retry_count=*/ 3`
LL | let _ = legacy_create_openai_url(None, 3);
| ^ help: prepend the parameter name comment: `/*retry_count=*/ 3`
warning: anonymous literal-like argument for parameter `enabled`
--> $DIR/uncommented_literal.rs:17:21
--> $DIR/uncommented_literal.rs:17:28
|
LL | client.set_flag(true);
| ^^^^ help: prepend the parameter name comment: `/*enabled=*/ true`
LL | client.set_legacy_flag(true);
| ^^^^ help: prepend the parameter name comment: `/*enabled=*/ true`
warning: 3 warnings emitted