Skip to content

New lint: Suggest str::strip_* over str::{starts,ends}_with and slicing it #5734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tesuji opened this issue Jun 21, 2020 · 1 comment
Closed
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-complexity Lint: Belongs in the complexity lint group

Comments

@tesuji
Copy link
Contributor

tesuji commented Jun 21, 2020

Categories: Performance, Correctness, complexity

Using str::strip removes the panic code inserted by slicing with pattern length.
Also it removes the need for duplicating/storing pattern used by str::{starts,ends}_with and (str[len..] or str[..len]).
Which it may removes the typing mistakes (do not repeat yourself).

Auto-apply suggestion ability

  • Unspecified
  • It's hard to make correct suggestion because:
    • it requires changing code/block structure.
    • it introduces a new binding which you have to give a name.

What it does

Suggest using strip_{prefix,suffix} over str::{starts,ends}_with and slicing by pattern's length.

Known problems

None.

Example

let s = "hello, world!";
if s.starts_with("hello, ") {
    // Or s[7..] ...
    assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
}

Could be written as:

let s = "hello, world!";
if let Some(tail) = s.strip_prefix("hello, ") {
    assert_eq!(tail.to_uppercase(), "WORLD!");
}
@tesuji tesuji changed the title Suggest str::strip_* over str::{starts,ends}_with and slicing it New lint: Suggest str::strip_* over str::{starts,ends}_with and slicing it Jun 21, 2020
@flip1995 flip1995 added L-complexity Lint: Belongs in the complexity lint group E-medium Call for participation: Medium difficulty level problem and requires some initial experience. A-lint Area: New lints labels Jun 22, 2020
@ghost
Copy link

ghost commented Sep 9, 2020

I started on this. I should have a pull request ready sometime soon.

@bors bors closed this as completed in d1f0f04 Sep 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-complexity Lint: Belongs in the complexity lint group
Projects
None yet
Development

No branches or pull requests

2 participants