Skip to content

trivial_regex false positive and/or incorrect hint for replace? #1943

Open
@emk

Description

@emk

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."

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.T-middleType: Probably requires verifiying types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions