forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#6226 - Urcra:master, r=flip1995
Add lint for comparing to empty slices instead of using .is_empty() Hey first time making a clippy lint I added the implementation of the lint the `len_zero` since it shared a lot of the code, I would otherwise have to rewrite. Just tell me if the lint should use it's own file instead changelog: Add lint for comparing to empty slices Fixes rust-lang#6217
- Loading branch information
Showing
7 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::comparison_to_empty)] | ||
|
||
fn main() { | ||
// Disallow comparisons to empty | ||
let s = String::new(); | ||
let _ = s.is_empty(); | ||
let _ = !s.is_empty(); | ||
|
||
let v = vec![0]; | ||
let _ = v.is_empty(); | ||
let _ = !v.is_empty(); | ||
|
||
// Allow comparisons to non-empty | ||
let s = String::new(); | ||
let _ = s == " "; | ||
let _ = s != " "; | ||
|
||
let v = vec![0]; | ||
let _ = v == [0]; | ||
let _ = v != [0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::comparison_to_empty)] | ||
|
||
fn main() { | ||
// Disallow comparisons to empty | ||
let s = String::new(); | ||
let _ = s == ""; | ||
let _ = s != ""; | ||
|
||
let v = vec![0]; | ||
let _ = v == []; | ||
let _ = v != []; | ||
|
||
// Allow comparisons to non-empty | ||
let s = String::new(); | ||
let _ = s == " "; | ||
let _ = s != " "; | ||
|
||
let v = vec![0]; | ||
let _ = v == [0]; | ||
let _ = v != [0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error: comparison to empty slice | ||
--> $DIR/comparison_to_empty.rs:8:13 | ||
| | ||
LL | let _ = s == ""; | ||
| ^^^^^^^ help: using `is_empty` is clearer and more explicit: `s.is_empty()` | ||
| | ||
= note: `-D clippy::comparison-to-empty` implied by `-D warnings` | ||
|
||
error: comparison to empty slice | ||
--> $DIR/comparison_to_empty.rs:9:13 | ||
| | ||
LL | let _ = s != ""; | ||
| ^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!s.is_empty()` | ||
|
||
error: comparison to empty slice | ||
--> $DIR/comparison_to_empty.rs:12:13 | ||
| | ||
LL | let _ = v == []; | ||
| ^^^^^^^ help: using `is_empty` is clearer and more explicit: `v.is_empty()` | ||
|
||
error: comparison to empty slice | ||
--> $DIR/comparison_to_empty.rs:13:13 | ||
| | ||
LL | let _ = v != []; | ||
| ^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!v.is_empty()` | ||
|
||
error: aborting due to 4 previous errors | ||
|