Skip to content

Commit b9bb298

Browse files
committed
Merge pull request #622 from mcarton/regex
Lint about trivial regexes
2 parents 0494071 + a02b812 commit b9bb298

File tree

8 files changed

+137
-48
lines changed

8 files changed

+137
-48
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A collection of lints to catch common mistakes and improve your Rust code.
66
[Jump to usage instructions](#usage)
77

88
##Lints
9-
There are 112 lints included in this crate:
9+
There are 113 lints included in this crate:
1010

1111
name | default | meaning
1212
---------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -104,6 +104,7 @@ name
104104
[string_to_string](https://github.com/Manishearth/rust-clippy/wiki#string_to_string) | warn | calling `String::to_string` which is inefficient
105105
[temporary_assignment](https://github.com/Manishearth/rust-clippy/wiki#temporary_assignment) | warn | assignments to temporaries
106106
[toplevel_ref_arg](https://github.com/Manishearth/rust-clippy/wiki#toplevel_ref_arg) | warn | An entire binding was declared as `ref`, in a function argument (`fn foo(ref x: Bar)`), or a `let` statement (`let ref x = foo()`). In such cases, it is preferred to take references with `&`.
107+
[trivial_regex](https://github.com/Manishearth/rust-clippy/wiki#trivial_regex) | warn | finds trivial regular expressions in `Regex::new(_)` invocations
107108
[type_complexity](https://github.com/Manishearth/rust-clippy/wiki#type_complexity) | warn | usage of very complex types; recommends factoring out parts into `type` definitions
108109
[unicode_not_nfc](https://github.com/Manishearth/rust-clippy/wiki#unicode_not_nfc) | allow | using a unicode literal not in NFC normal form (see http://www.unicode.org/reports/tr15/ for further information)
109110
[unit_cmp](https://github.com/Manishearth/rust-clippy/wiki#unit_cmp) | warn | comparing unit values (which is always `true` or `false`, respectively)

src/cyclomatic_complexity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ impl CyclomaticComplexity {
6262
span_help_and_lint(cx,
6363
CYCLOMATIC_COMPLEXITY,
6464
span,
65-
&format!("The function has a cyclomatic complexity of {}", rust_cc),
66-
"You could split it up into multiple smaller functions");
65+
&format!("the function has a cyclomatic complexity of {}", rust_cc),
66+
"you could split it up into multiple smaller functions");
6767
}
6868
}
6969
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
257257
ranges::RANGE_STEP_BY_ZERO,
258258
ranges::RANGE_ZIP_WITH_LEN,
259259
regex::INVALID_REGEX,
260+
regex::TRIVIAL_REGEX,
260261
returns::LET_AND_RETURN,
261262
returns::NEEDLESS_RETURN,
262263
strings::STRING_LIT_AS_BYTES,

src/open_options.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn check_open_options(cx: &LateContext, options: &[(OpenOption, Argument)], span
120120
span_lint(cx,
121121
NONSENSICAL_OPEN_OPTIONS,
122122
span,
123-
"The method \"create\" is called more than once");
123+
"the method \"create\" is called more than once");
124124
} else {
125125
create = true
126126
}
@@ -131,7 +131,7 @@ fn check_open_options(cx: &LateContext, options: &[(OpenOption, Argument)], span
131131
span_lint(cx,
132132
NONSENSICAL_OPEN_OPTIONS,
133133
span,
134-
"The method \"append\" is called more than once");
134+
"the method \"append\" is called more than once");
135135
} else {
136136
append = true
137137
}
@@ -142,7 +142,7 @@ fn check_open_options(cx: &LateContext, options: &[(OpenOption, Argument)], span
142142
span_lint(cx,
143143
NONSENSICAL_OPEN_OPTIONS,
144144
span,
145-
"The method \"truncate\" is called more than once");
145+
"the method \"truncate\" is called more than once");
146146
} else {
147147
truncate = true
148148
}
@@ -153,7 +153,7 @@ fn check_open_options(cx: &LateContext, options: &[(OpenOption, Argument)], span
153153
span_lint(cx,
154154
NONSENSICAL_OPEN_OPTIONS,
155155
span,
156-
"The method \"read\" is called more than once");
156+
"the method \"read\" is called more than once");
157157
} else {
158158
read = true
159159
}
@@ -164,7 +164,7 @@ fn check_open_options(cx: &LateContext, options: &[(OpenOption, Argument)], span
164164
span_lint(cx,
165165
NONSENSICAL_OPEN_OPTIONS,
166166
span,
167-
"The method \"write\" is called more than once");
167+
"the method \"write\" is called more than once");
168168
} else {
169169
write = true
170170
}
@@ -174,12 +174,12 @@ fn check_open_options(cx: &LateContext, options: &[(OpenOption, Argument)], span
174174
}
175175

176176
if read && truncate && read_arg && truncate_arg {
177-
span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "File opened with \"truncate\" and \"read\"");
177+
span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "file opened with \"truncate\" and \"read\"");
178178
}
179179
if append && truncate && append_arg && truncate_arg {
180180
span_lint(cx,
181181
NONSENSICAL_OPEN_OPTIONS,
182182
span,
183-
"File opened with \"append\" and \"truncate\"");
183+
"file opened with \"append\" and \"truncate\"");
184184
}
185185
}

src/regex.rs

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc::middle::const_eval::{eval_const_expr_partial, ConstVal};
88
use rustc::middle::const_eval::EvalHint::ExprTypeChecked;
99
use rustc::lint::*;
1010

11-
use utils::{match_path, REGEX_NEW_PATH, span_lint};
11+
use utils::{match_path, REGEX_NEW_PATH, span_lint, span_help_and_lint};
1212

1313
/// **What it does:** This lint checks `Regex::new(_)` invocations for correct regex syntax. It is `deny` by default.
1414
///
@@ -23,12 +23,26 @@ declare_lint! {
2323
"finds invalid regular expressions in `Regex::new(_)` invocations"
2424
}
2525

26+
/// **What it does:** This lint checks for `Regex::new(_)` invocations with trivial regex.
27+
///
28+
/// **Why is this bad?** This can likely be replaced by `==` or `str::starts_with`,
29+
/// `str::ends_with` or `std::contains` or other `str` methods.
30+
///
31+
/// **Known problems:** None.
32+
///
33+
/// **Example:** `Regex::new("^foobar")`
34+
declare_lint! {
35+
pub TRIVIAL_REGEX,
36+
Warn,
37+
"finds trivial regular expressions in `Regex::new(_)` invocations"
38+
}
39+
2640
#[derive(Copy,Clone)]
2741
pub struct RegexPass;
2842

2943
impl LintPass for RegexPass {
3044
fn get_lints(&self) -> LintArray {
31-
lint_array!(INVALID_REGEX)
45+
lint_array!(INVALID_REGEX, TRIVIAL_REGEX)
3246
}
3347
}
3448

@@ -45,22 +59,29 @@ impl LateLintPass for RegexPass {
4559
span_lint(cx,
4660
INVALID_REGEX,
4761
str_span(args[0].span, &r, e.position()),
48-
&format!("Regex syntax error: {}",
62+
&format!("regex syntax error: {}",
4963
e.description()));
5064
}
65+
else if let Some(repl) = is_trivial_regex(r) {
66+
span_help_and_lint(cx, TRIVIAL_REGEX, args[0].span,
67+
&"trivial regex",
68+
&format!("consider using {}", repl));
69+
}
5170
}
52-
} else {
53-
if_let_chain!{[
54-
let Some(r) = const_str(cx, &*args[0]),
55-
let Err(e) = regex_syntax::Expr::parse(&r)
56-
], {
71+
} else if let Some(r) = const_str(cx, &*args[0]) {
72+
if let Err(e) = regex_syntax::Expr::parse(&r) {
5773
span_lint(cx,
5874
INVALID_REGEX,
5975
args[0].span,
60-
&format!("Regex syntax error on position {}: {}",
76+
&format!("regex syntax error on position {}: {}",
6177
e.position(),
6278
e.description()));
63-
}}
79+
}
80+
else if let Some(repl) = is_trivial_regex(&r) {
81+
span_help_and_lint(cx, TRIVIAL_REGEX, args[0].span,
82+
&"trivial regex",
83+
&format!("{}", repl));
84+
}
6485
}
6586
}}
6687
}
@@ -81,3 +102,26 @@ fn const_str(cx: &LateContext, e: &Expr) -> Option<InternedString> {
81102
_ => None
82103
}
83104
}
105+
106+
fn is_trivial_regex(s: &str) -> Option<&'static str> {
107+
// some unlikely but valid corner cases
108+
match s {
109+
"" | "^" | "$" => return Some("the regex is unlikely to be useful as it is"),
110+
"^$" => return Some("consider using `str::is_empty`"),
111+
_ => (),
112+
}
113+
114+
let (start, end, repl) = match (s.starts_with('^'), s.ends_with('$')) {
115+
(true, true) => (1, s.len()-1, "consider using `==` on `str`s"),
116+
(false, true) => (0, s.len()-1, "consider using `str::ends_with`"),
117+
(true, false) => (1, s.len(), "consider using `str::starts_with`"),
118+
(false, false) => (0, s.len(), "consider using `str::contains`"),
119+
};
120+
121+
if !s.chars().take(end).skip(start).any(regex_syntax::is_punct) {
122+
Some(repl)
123+
}
124+
else {
125+
None
126+
}
127+
}

tests/compile-fail/cyclomatic_complexity.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(unused)]
66

77

8-
fn main() { //~ERROR The function has a cyclomatic complexity of 28
8+
fn main() { //~ERROR the function has a cyclomatic complexity of 28
99
if true {
1010
println!("a");
1111
}
@@ -90,7 +90,7 @@ fn main() { //~ERROR The function has a cyclomatic complexity of 28
9090
}
9191

9292
#[cyclomatic_complexity = "0"]
93-
fn kaboom() { //~ ERROR: The function has a cyclomatic complexity of 8
93+
fn kaboom() { //~ ERROR: the function has a cyclomatic complexity of 8
9494
let n = 0;
9595
'a: for i in 0..20 {
9696
'b: for j in i..20 {
@@ -136,7 +136,7 @@ fn bloo() {
136136
}
137137

138138
#[cyclomatic_complexity = "0"]
139-
fn baa() { //~ ERROR: The function has a cyclomatic complexity of 2
139+
fn baa() { //~ ERROR: the function has a cyclomatic complexity of 2
140140
let x = || match 99 {
141141
0 => true,
142142
1 => false,
@@ -154,15 +154,15 @@ fn baa() { //~ ERROR: The function has a cyclomatic complexity of 2
154154
}
155155

156156
#[cyclomatic_complexity = "0"]
157-
fn bar() { //~ ERROR: The function has a cyclomatic complexity of 2
157+
fn bar() { //~ ERROR: the function has a cyclomatic complexity of 2
158158
match 99 {
159159
0 => println!("hi"),
160160
_ => println!("bye"),
161161
}
162162
}
163163

164164
#[cyclomatic_complexity = "0"]
165-
fn barr() { //~ ERROR: The function has a cyclomatic complexity of 2
165+
fn barr() { //~ ERROR: the function has a cyclomatic complexity of 2
166166
match 99 {
167167
0 => println!("hi"),
168168
1 => println!("bla"),
@@ -172,7 +172,7 @@ fn barr() { //~ ERROR: The function has a cyclomatic complexity of 2
172172
}
173173

174174
#[cyclomatic_complexity = "0"]
175-
fn barr2() { //~ ERROR: The function has a cyclomatic complexity of 3
175+
fn barr2() { //~ ERROR: the function has a cyclomatic complexity of 3
176176
match 99 {
177177
0 => println!("hi"),
178178
1 => println!("bla"),
@@ -188,7 +188,7 @@ fn barr2() { //~ ERROR: The function has a cyclomatic complexity of 3
188188
}
189189

190190
#[cyclomatic_complexity = "0"]
191-
fn barrr() { //~ ERROR: The function has a cyclomatic complexity of 2
191+
fn barrr() { //~ ERROR: the function has a cyclomatic complexity of 2
192192
match 99 {
193193
0 => println!("hi"),
194194
1 => panic!("bla"),
@@ -198,7 +198,7 @@ fn barrr() { //~ ERROR: The function has a cyclomatic complexity of 2
198198
}
199199

200200
#[cyclomatic_complexity = "0"]
201-
fn barrr2() { //~ ERROR: The function has a cyclomatic complexity of 3
201+
fn barrr2() { //~ ERROR: the function has a cyclomatic complexity of 3
202202
match 99 {
203203
0 => println!("hi"),
204204
1 => panic!("bla"),
@@ -214,7 +214,7 @@ fn barrr2() { //~ ERROR: The function has a cyclomatic complexity of 3
214214
}
215215

216216
#[cyclomatic_complexity = "0"]
217-
fn barrrr() { //~ ERROR: The function has a cyclomatic complexity of 2
217+
fn barrrr() { //~ ERROR: the function has a cyclomatic complexity of 2
218218
match 99 {
219219
0 => println!("hi"),
220220
1 => println!("bla"),
@@ -224,7 +224,7 @@ fn barrrr() { //~ ERROR: The function has a cyclomatic complexity of 2
224224
}
225225

226226
#[cyclomatic_complexity = "0"]
227-
fn barrrr2() { //~ ERROR: The function has a cyclomatic complexity of 3
227+
fn barrrr2() { //~ ERROR: the function has a cyclomatic complexity of 3
228228
match 99 {
229229
0 => println!("hi"),
230230
1 => println!("bla"),
@@ -240,7 +240,7 @@ fn barrrr2() { //~ ERROR: The function has a cyclomatic complexity of 3
240240
}
241241

242242
#[cyclomatic_complexity = "0"]
243-
fn cake() { //~ ERROR: The function has a cyclomatic complexity of 2
243+
fn cake() { //~ ERROR: the function has a cyclomatic complexity of 2
244244
if 4 == 5 {
245245
println!("yea");
246246
} else {
@@ -251,7 +251,7 @@ fn cake() { //~ ERROR: The function has a cyclomatic complexity of 2
251251

252252

253253
#[cyclomatic_complexity = "0"]
254-
pub fn read_file(input_path: &str) -> String { //~ ERROR: The function has a cyclomatic complexity of 4
254+
pub fn read_file(input_path: &str) -> String { //~ ERROR: the function has a cyclomatic complexity of 4
255255
use std::fs::File;
256256
use std::io::{Read, Write};
257257
use std::path::Path;
@@ -282,7 +282,7 @@ pub fn read_file(input_path: &str) -> String { //~ ERROR: The function has a cyc
282282
enum Void {}
283283

284284
#[cyclomatic_complexity = "0"]
285-
fn void(void: Void) { //~ ERROR: The function has a cyclomatic complexity of 1
285+
fn void(void: Void) { //~ ERROR: the function has a cyclomatic complexity of 1
286286
if true {
287287
match void {
288288
}

tests/compile-fail/open_options.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use std::fs::OpenOptions;
55
#[allow(unused_must_use)]
66
#[deny(nonsensical_open_options)]
77
fn main() {
8-
OpenOptions::new().read(true).truncate(true).open("foo.txt"); //~ERROR File opened with "truncate" and "read"
9-
OpenOptions::new().append(true).truncate(true).open("foo.txt"); //~ERROR File opened with "append" and "truncate"
10-
11-
OpenOptions::new().read(true).read(false).open("foo.txt"); //~ERROR The method "read" is called more than once
12-
OpenOptions::new().create(true).create(false).open("foo.txt"); //~ERROR The method "create" is called more than once
13-
OpenOptions::new().write(true).write(false).open("foo.txt"); //~ERROR The method "write" is called more than once
14-
OpenOptions::new().append(true).append(false).open("foo.txt"); //~ERROR The method "append" is called more than once
15-
OpenOptions::new().truncate(true).truncate(false).open("foo.txt"); //~ERROR The method "truncate" is called more than once
8+
OpenOptions::new().read(true).truncate(true).open("foo.txt"); //~ERROR file opened with "truncate" and "read"
9+
OpenOptions::new().append(true).truncate(true).open("foo.txt"); //~ERROR file opened with "append" and "truncate"
10+
11+
OpenOptions::new().read(true).read(false).open("foo.txt"); //~ERROR the method "read" is called more than once
12+
OpenOptions::new().create(true).create(false).open("foo.txt"); //~ERROR the method "create" is called more than once
13+
OpenOptions::new().write(true).write(false).open("foo.txt"); //~ERROR the method "write" is called more than once
14+
OpenOptions::new().append(true).append(false).open("foo.txt"); //~ERROR the method "append" is called more than once
15+
OpenOptions::new().truncate(true).truncate(false).open("foo.txt"); //~ERROR the method "truncate" is called more than once
1616
}

0 commit comments

Comments
 (0)