rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Lint for replacing `x.strip_[prefix/suffix](...).unwrap_or(x)` with `x.trim_[prefix/suffix](...)`

Open ChayimFriedman2 opened this issue 2 months ago • 4 comments

What it does

Rust 1.100.0 will stabilize the str::trim_prefix() and trim_suffix() methods. The previous workaround for them was s.strip_[prefix/suffix](...).unwrap_or(s). I think we should have a lint for the new method.

Advantage

It's just simpler, and perhaps also faster.

Drawbacks

No response

Example

let s: &str;
s.strip_prefix("foo").unwrap_or(s)

Could be written as:

let s: &str;
s.trim_prefix("foo")

Comparison with existing lints

No response

Additional Context

No response

ChayimFriedman2 avatar Sep 24 '26 13:09 ChayimFriedman2

@rustbot label good first issue

That sounds like a good and simple methods lint to me. Needs MSRV-checking though, but that is not hard ;)

CommanderStorm avatar Sep 24 '26 14:09 CommanderStorm

Error: Unknown labels: first, good, issue

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

rustbot avatar Sep 24 '26 14:09 rustbot

@rustbot label "good first issue"

CommanderStorm avatar Sep 24 '26 20:09 CommanderStorm

@rustbot claim

Mitshah2406 avatar Sep 25 '26 14:09 Mitshah2406