rust-lang / rust-lang/rust-clippy

trivial_regex prefers very slow code

Open
#6,690 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-enhancement E-medium I-false-positive L-complexity
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Lint name: trivial_regex

#![feature(test)]

extern crate test;

use regex::Regex;
use test::{black_box, Bencher};

#[bench]
fn bench_contains(b: &mut Bencher) {
    let s = "0".repeat(100000);
    b.iter(|| black_box(&s).contains("asdf"));
}

#[bench]
fn bench_regex(b: &mut Bencher) {
    let s = "0".repeat(100000);
    let regex = Regex::new("asdf").unwrap();
    b.iter(|| regex.is_match(black_box(&s)));
}
$ cargo clippy --benches

warning: trivial regex
  --> src/main.rs:17:28
   |
17 |     let regex = Regex::new("asdf").unwrap();
   |                            ^^^^^^
   |
   = note: `#[warn(clippy::trivial_regex)]` on by default
   = help: consider using `str::contains`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivial_regex

Clippy wants the regex replaced with str::contains, which is >10x slower.

$ cargo bench
     Running target/release/deps/testing-d8b23d81ce9bc37f

running 2 tests
test bench_contains ... bench:      12,031 ns/iter (+/- 101)
test bench_regex    ... bench:       1,126 ns/iter (+/- 8)

  • cargo clippy -V: clippy 0.1.52 (a73c2e5 2021-02-06)
  • rustc -Vv:
    rustc 1.52.0-nightly (a73c2e555 2021-02-06)
    binary: rustc
    commit-hash: a73c2e555c26ef0c8b98c91c97a7d24b7017267f
    commit-date: 2021-02-06
    host: x86_64-unknown-linux-gnu
    release: 1.52.0-nightly
    LLVM version: 11.0.1
    

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported trivial_regex warning with the src/main.rs benchmark and compare the str::contains and regex timings using cargo bench. Then trace the trivial_regex lint and its diagnostic guidance, and verify that the completed change no longer recommends the slower alternative for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
performance, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.