mirror of
https://github.com/openai/codex.git
synced 2026-12-01 00:20:48 +00:00
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:
@@ -841,7 +841,7 @@ async fn enable_feature_in_config(interactive: &TuiCli, feature: &str) -> anyhow
|
||||
let codex_home = find_codex_home()?;
|
||||
ConfigEditsBuilder::new(&codex_home)
|
||||
.with_profile(interactive.config_profile.as_deref())
|
||||
.set_feature_enabled(feature, true)
|
||||
.enable_feature(feature)
|
||||
.apply()
|
||||
.await?;
|
||||
println!("Enabled feature `{feature}` in config.toml.");
|
||||
@@ -854,7 +854,7 @@ async fn disable_feature_in_config(interactive: &TuiCli, feature: &str) -> anyho
|
||||
let codex_home = find_codex_home()?;
|
||||
ConfigEditsBuilder::new(&codex_home)
|
||||
.with_profile(interactive.config_profile.as_deref())
|
||||
.set_feature_enabled(feature, false)
|
||||
.disable_feature(feature)
|
||||
.apply()
|
||||
.await?;
|
||||
println!("Disabled feature `{feature}` in config.toml.");
|
||||
|
||||
@@ -857,11 +857,20 @@ impl ConfigEditsBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
/// Enable or disable a feature flag by key under the `[features]` table.
|
||||
pub fn set_feature_enabled(mut self, key: &str, enabled: bool) -> Self {
|
||||
/// Enable a feature flag by key under the `[features]` table.
|
||||
pub fn enable_feature(mut self, key: &str) -> Self {
|
||||
self.edits.push(ConfigEdit::SetPath {
|
||||
segments: vec!["features".to_string(), key.to_string()],
|
||||
value: value(enabled),
|
||||
value: value(true),
|
||||
});
|
||||
self
|
||||
}
|
||||
|
||||
/// Disable a feature flag by key under the `[features]` table.
|
||||
pub fn disable_feature(mut self, key: &str) -> Self {
|
||||
self.edits.push(ConfigEdit::SetPath {
|
||||
segments: vec!["features".to_string(), key.to_string()],
|
||||
value: value(false),
|
||||
});
|
||||
self
|
||||
}
|
||||
|
||||
@@ -102,7 +102,11 @@ fn shell_command_for_invocation(invocation: &ToolInvocation) -> Option<(Vec<Stri
|
||||
let command = crate::tools::handlers::unified_exec::get_command(
|
||||
¶ms,
|
||||
invocation.session.user_shell(),
|
||||
invocation.turn.tools_config.allow_login_shell,
|
||||
if invocation.turn.tools_config.allow_login_shell {
|
||||
crate::tools::handlers::unified_exec::LoginShellPolicy::Allow
|
||||
} else {
|
||||
crate::tools::handlers::unified_exec::LoginShellPolicy::Disallow
|
||||
},
|
||||
)
|
||||
.ok()?;
|
||||
Some((command, invocation.turn.resolve_path(params.workdir)))
|
||||
|
||||
@@ -107,7 +107,11 @@ impl ToolHandler for UnifiedExecHandler {
|
||||
let command = match get_command(
|
||||
¶ms,
|
||||
invocation.session.user_shell(),
|
||||
invocation.turn.tools_config.allow_login_shell,
|
||||
if invocation.turn.tools_config.allow_login_shell {
|
||||
LoginShellPolicy::Allow
|
||||
} else {
|
||||
LoginShellPolicy::Disallow
|
||||
},
|
||||
) {
|
||||
Ok(command) => command,
|
||||
Err(_) => return true,
|
||||
@@ -154,7 +158,11 @@ impl ToolHandler for UnifiedExecHandler {
|
||||
let command = get_command(
|
||||
&args,
|
||||
session.user_shell(),
|
||||
turn.tools_config.allow_login_shell,
|
||||
if turn.tools_config.allow_login_shell {
|
||||
LoginShellPolicy::Allow
|
||||
} else {
|
||||
LoginShellPolicy::Disallow
|
||||
},
|
||||
)
|
||||
.map_err(FunctionCallError::RespondToModel)?;
|
||||
|
||||
@@ -317,10 +325,22 @@ impl ToolHandler for UnifiedExecHandler {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(crate) enum LoginShellPolicy {
|
||||
Allow,
|
||||
Disallow,
|
||||
}
|
||||
|
||||
impl LoginShellPolicy {
|
||||
fn allows_login_shell(self) -> bool {
|
||||
matches!(self, Self::Allow)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_command(
|
||||
args: &ExecCommandArgs,
|
||||
session_shell: Arc<Shell>,
|
||||
allow_login_shell: bool,
|
||||
login_shell_policy: LoginShellPolicy,
|
||||
) -> Result<Vec<String>, String> {
|
||||
let model_shell = args.shell.as_ref().map(|shell_str| {
|
||||
let mut shell = get_shell_by_model_provided_path(&PathBuf::from(shell_str));
|
||||
@@ -329,6 +349,7 @@ pub(crate) fn get_command(
|
||||
});
|
||||
|
||||
let shell = model_shell.as_ref().unwrap_or(session_shell.as_ref());
|
||||
let allow_login_shell = login_shell_policy.allows_login_shell();
|
||||
let use_login_shell = match args.login {
|
||||
Some(true) if !allow_login_shell => {
|
||||
return Err(
|
||||
|
||||
@@ -18,8 +18,12 @@ fn test_get_command_uses_default_shell_when_unspecified() -> anyhow::Result<()>
|
||||
|
||||
assert!(args.shell.is_none());
|
||||
|
||||
let command =
|
||||
get_command(&args, Arc::new(default_user_shell()), true).map_err(anyhow::Error::msg)?;
|
||||
let command = get_command(
|
||||
&args,
|
||||
Arc::new(default_user_shell()),
|
||||
LoginShellPolicy::Allow,
|
||||
)
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
|
||||
assert_eq!(command.len(), 3);
|
||||
assert_eq!(command[2], "echo hello");
|
||||
@@ -34,8 +38,12 @@ fn test_get_command_respects_explicit_bash_shell() -> anyhow::Result<()> {
|
||||
|
||||
assert_eq!(args.shell.as_deref(), Some("/bin/bash"));
|
||||
|
||||
let command =
|
||||
get_command(&args, Arc::new(default_user_shell()), true).map_err(anyhow::Error::msg)?;
|
||||
let command = get_command(
|
||||
&args,
|
||||
Arc::new(default_user_shell()),
|
||||
LoginShellPolicy::Allow,
|
||||
)
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
|
||||
assert_eq!(command.last(), Some(&"echo hello".to_string()));
|
||||
if command
|
||||
@@ -55,8 +63,12 @@ fn test_get_command_respects_explicit_powershell_shell() -> anyhow::Result<()> {
|
||||
|
||||
assert_eq!(args.shell.as_deref(), Some("powershell"));
|
||||
|
||||
let command =
|
||||
get_command(&args, Arc::new(default_user_shell()), true).map_err(anyhow::Error::msg)?;
|
||||
let command = get_command(
|
||||
&args,
|
||||
Arc::new(default_user_shell()),
|
||||
LoginShellPolicy::Allow,
|
||||
)
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
|
||||
assert_eq!(command[2], "echo hello");
|
||||
Ok(())
|
||||
@@ -70,8 +82,12 @@ fn test_get_command_respects_explicit_cmd_shell() -> anyhow::Result<()> {
|
||||
|
||||
assert_eq!(args.shell.as_deref(), Some("cmd"));
|
||||
|
||||
let command =
|
||||
get_command(&args, Arc::new(default_user_shell()), true).map_err(anyhow::Error::msg)?;
|
||||
let command = get_command(
|
||||
&args,
|
||||
Arc::new(default_user_shell()),
|
||||
LoginShellPolicy::Allow,
|
||||
)
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
|
||||
assert_eq!(command[2], "echo hello");
|
||||
Ok(())
|
||||
@@ -82,8 +98,12 @@ fn test_get_command_rejects_explicit_login_when_disallowed() -> anyhow::Result<(
|
||||
let json = r#"{"cmd": "echo hello", "login": true}"#;
|
||||
|
||||
let args: ExecCommandArgs = parse_arguments(json)?;
|
||||
let err = get_command(&args, Arc::new(default_user_shell()), false)
|
||||
.expect_err("explicit login should be rejected");
|
||||
let err = get_command(
|
||||
&args,
|
||||
Arc::new(default_user_shell()),
|
||||
LoginShellPolicy::Disallow,
|
||||
)
|
||||
.expect_err("explicit login should be rejected");
|
||||
|
||||
assert!(
|
||||
err.contains("login shell is disabled by config"),
|
||||
|
||||
@@ -860,12 +860,15 @@ impl App {
|
||||
continue;
|
||||
}
|
||||
let effective_enabled = self.config.features.enabled(feature);
|
||||
self.chat_widget
|
||||
.set_feature_enabled(feature, effective_enabled);
|
||||
if effective_enabled {
|
||||
builder = builder.set_feature_enabled(feature_key, true);
|
||||
self.chat_widget.enable_feature(feature);
|
||||
} else {
|
||||
self.chat_widget.disable_feature(feature);
|
||||
}
|
||||
if effective_enabled {
|
||||
builder = builder.enable_feature(feature_key);
|
||||
} else if feature.default_enabled() {
|
||||
builder = builder.set_feature_enabled(feature_key, false);
|
||||
builder = builder.disable_feature(feature_key);
|
||||
} else {
|
||||
// If the feature already default to `false`, we drop the key
|
||||
// in the config file so that the user does not miss the feature
|
||||
@@ -5156,8 +5159,7 @@ mod tests {
|
||||
app.config
|
||||
.features
|
||||
.set_enabled(Feature::GuardianApproval, true)?;
|
||||
app.chat_widget
|
||||
.set_feature_enabled(Feature::GuardianApproval, true);
|
||||
app.chat_widget.enable_feature(Feature::GuardianApproval);
|
||||
let current_session_policy = app.config.permissions.approval_policy.value();
|
||||
|
||||
app.update_feature_flags(vec![(Feature::GuardianApproval, false)])
|
||||
|
||||
@@ -7649,6 +7649,14 @@ impl ChatWidget {
|
||||
true
|
||||
}
|
||||
|
||||
pub(crate) fn enable_feature(&mut self, feature: Feature) -> bool {
|
||||
self.set_feature_enabled(feature, true)
|
||||
}
|
||||
|
||||
pub(crate) fn disable_feature(&mut self, feature: Feature) -> bool {
|
||||
self.set_feature_enabled(feature, false)
|
||||
}
|
||||
|
||||
fn initial_collaboration_mask(
|
||||
_config: &Config,
|
||||
models_manager: &ModelsManager,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user