Files
codex/prs/bolinfest/study/PR-2465-study.md
2025-09-02 15:17:45 -07:00

121 lines
3.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
**Rust 1.89 Upgrade: DOs & DON'Ts**
**DOs**
- Bold: Upgrade toolchains: Update `rust-toolchain.toml` and CI to 1.89.
```
# codex-rs/rust-toolchain.toml
[toolchain]
channel = "1.89.0"
components = ["clippy", "rustfmt", "rust-src"]
# .github/workflows/*
- uses: dtolnay/rust-toolchain@1.89
```
- Bold: Keep Clippy clean: Prefer letchains over nested conditionals.
```rust
// Before
if let Some(output) = url_result {
if output.status.success() {
if let Ok(url) = String::from_utf8(output.stdout) {
git_info.repository_url = Some(url.trim().to_string());
}
}
}
// After (let-chains)
if let Some(output) = url_result
&& output.status.success()
&& let Ok(url) = String::from_utf8(output.stdout)
{
git_info.repository_url = Some(url.trim().to_string());
}
```
- Bold: Combine Option checks with value guards.
```rust
// Before
if let Ok(val) = std::env::var("CODEX_HOME") {
if !val.is_empty() {
return PathBuf::from(val).canonicalize();
}
}
// After
if let Ok(val) = std::env::var("CODEX_HOME") && !val.is_empty() {
return PathBuf::from(val).canonicalize();
}
```
- Bold: Use `is_some_and` to express predicate checks succinctly.
```rust
if let ParsedCommand::Unknown { cmd } = &commands[0]
&& shlex_split(cmd).is_some_and(|t| t.first().map(|s| s.as_str()) == Some("echo"))
{
return Some(commands[1..].to_vec());
}
```
- Bold: Prefer `std::slice::from_ref` for singleelement slices.
```rust
// Before
seek_sequence(lines, &[ctx_line.clone()], start, false);
// After
seek_sequence(lines, std::slice::from_ref(ctx_line), start, false);
// In tests
checker.check(exec, &cwd, std::slice::from_ref(&root), &[]);
```
- Bold: Adjust APIs for the 1.89 lifetime lint when needed.
```rust
// Before
pub fn get_frame(&mut self) -> Frame {
// After
pub fn get_frame(&mut self) -> Frame<'_> {
```
- Bold: Keep formatting simple: inline variables in `format!` braces.
```rust
with_context(|| format!("Failed to write file {}", path.display()))
```
- Bold: Run the standard hygiene commands locally.
```bash
# In codex-rs/
just fmt
cargo clippy --tests
```
**DON'Ts**
- Bold: Dont fight Clippy by nesting `if`/`if let`; use guard chains instead.
```rust
// Dont do this
if let Some(parent) = path.parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent)?;
}
}
// Do this
if let Some(parent) = path.parent() && !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent)?;
}
```
- Bold: Dont disable lints to preserve older patterns; embrace 1.89 idioms.
```rust
// Avoid adding #[allow(clippy::collapsible_if)] just to keep nested conditionals.
```
- Bold: Dont pass manual singleelement arrays where a reference slice is clearer.
```rust
// Dont
checker.check(exec, &cwd, &[root.clone()], &[]);
// Do
checker.check(exec, &cwd, std::slice::from_ref(&root), &[]);
```
- Bold: Dont rely on implicit lifetimes that now trigger the mismatched-lifetime lint.
```rust
// Fix signatures like -> Frame to -> Frame<'_> where applicable.
```