rust-lang / rust-lang/rust-clippy
`trivial_regex` false positive and/or incorrect hint for `replace`?
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
I love the new lints! But I found what seems to be a false positive:
For the following code:
pub fn from_bigml<S: Into<String>>(name: S) -> Result<FieldName> {
// Again, we have a fair bit of internal knowledge about what kinds of
// field names can actually exist. Don't hesitate to add new types.
lazy_static! {
static ref PRIVATE_RE: Regex = Regex::new("^private_").unwrap();
}
let name = name.into();
Self::from_unescaped(PRIVATE_RE.replace(&name, "private/"))
}
...the trivial_regex lint suggests:
warning: trivial regex
--> src/types/field_name.rs:80:55
|
80 | static ref PRIVATE_RE: Regex = Regex::new("^private_").unwrap();
| ^^^^^^^^^^^
|
= note: #[warn(trivial_regex)] on by default
= help: consider using consider using `str::starts_with`
= help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#trivial_regex
Not all regular expressions are used for matches, and I'm not aware of anything in std that implements "replace this string only at the beginning of another string."
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the warning from the Rust example in src/types/field_name.rs:80, then inspect the trivial_regex lint and how it handles Regex::replace with a ^-anchored expression. Check whether the starts_with hint matches replacement semantics; done means the false positive or incorrect hint is resolved and the reported case has regression coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100