Auto merge of #8711 - kyoto7250:new-lint-bytes-count-to-len, r=giraffate

Take over: New lint bytes count to len

take over #8375
close #8083

This PR adds new lint about  considering replacing `.bytes().count()` with `.len()`.

Thank you in advance.

---

r! `@Manishearth`

changelog: adds new lint [`bytes_count_to_len`] to consider replacing `.bytes().count()` with `.len()`
This commit is contained in:
bors 2022-04-19 12:44:07 +00:00
commit e17b97c8e0
10 changed files with 174 additions and 0 deletions

View file

@ -0,0 +1,34 @@
// run-rustfix
#![warn(clippy::bytes_count_to_len)]
use std::fs::File;
use std::io::Read;
fn main() {
// should fix, because type is String
let _ = String::from("foo").len();
let s1 = String::from("foo");
let _ = s1.len();
// should fix, because type is &str
let _ = "foo".len();
let s2 = "foo";
let _ = s2.len();
// make sure using count() normally doesn't trigger warning
let vector = [0, 1, 2];
let _ = vector.iter().count();
// The type is slice, so should not fix
let _ = &[1, 2, 3].bytes().count();
let bytes: &[u8] = &[1, 2, 3];
bytes.bytes().count();
// The type is File, so should not fix
let _ = File::open("foobar").unwrap().bytes().count();
let f = File::open("foobar").unwrap();
let _ = f.bytes().count();
}

View file

@ -0,0 +1,34 @@
// run-rustfix
#![warn(clippy::bytes_count_to_len)]
use std::fs::File;
use std::io::Read;
fn main() {
// should fix, because type is String
let _ = String::from("foo").bytes().count();
let s1 = String::from("foo");
let _ = s1.bytes().count();
// should fix, because type is &str
let _ = "foo".bytes().count();
let s2 = "foo";
let _ = s2.bytes().count();
// make sure using count() normally doesn't trigger warning
let vector = [0, 1, 2];
let _ = vector.iter().count();
// The type is slice, so should not fix
let _ = &[1, 2, 3].bytes().count();
let bytes: &[u8] = &[1, 2, 3];
bytes.bytes().count();
// The type is File, so should not fix
let _ = File::open("foobar").unwrap().bytes().count();
let f = File::open("foobar").unwrap();
let _ = f.bytes().count();
}

View file

@ -0,0 +1,28 @@
error: using long and hard to read `.bytes().count()`
--> $DIR/bytes_count_to_len.rs:8:13
|
LL | let _ = String::from("foo").bytes().count();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.len()` instead: `String::from("foo").len()`
|
= note: `-D clippy::bytes-count-to-len` implied by `-D warnings`
error: using long and hard to read `.bytes().count()`
--> $DIR/bytes_count_to_len.rs:11:13
|
LL | let _ = s1.bytes().count();
| ^^^^^^^^^^^^^^^^^^ help: consider calling `.len()` instead: `s1.len()`
error: using long and hard to read `.bytes().count()`
--> $DIR/bytes_count_to_len.rs:14:13
|
LL | let _ = "foo".bytes().count();
| ^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.len()` instead: `"foo".len()`
error: using long and hard to read `.bytes().count()`
--> $DIR/bytes_count_to_len.rs:17:13
|
LL | let _ = s2.bytes().count();
| ^^^^^^^^^^^^^^^^^^ help: consider calling `.len()` instead: `s2.len()`
error: aborting due to 4 previous errors