Add needless_character_iteration lint

This commit is contained in:
Guillaume Gomez 2024-05-17 18:05:46 +02:00
parent caad063933
commit 566dfd9008
9 changed files with 330 additions and 18 deletions

View file

@ -0,0 +1,51 @@
#![warn(clippy::needless_character_iteration)]
#![allow(clippy::map_identity, clippy::unnecessary_operation)]
#[derive(Default)]
struct S {
field: &'static str,
}
impl S {
fn field(&self) -> &str {
self.field
}
}
fn magic(_: char) {}
fn main() {
"foo".is_ascii();
//~^ ERROR: checking if a string is ascii using iterators
!"foo".is_ascii();
//~^ ERROR: checking if a string is ascii using iterators
"foo".is_ascii();
//~^ ERROR: checking if a string is ascii using iterators
!"foo".is_ascii();
//~^ ERROR: checking if a string is ascii using iterators
let s = String::new();
s.is_ascii();
//~^ ERROR: checking if a string is ascii using iterators
!"foo".to_string().is_ascii();
//~^ ERROR: checking if a string is ascii using iterators
"foo".is_ascii();
!"foo".is_ascii();
S::default().field().is_ascii();
//~^ ERROR: checking if a string is ascii using iterators
// Should not lint!
"foo".chars().all(|c| {
let x = c;
magic(x);
x.is_ascii()
});
// Should not lint!
"foo".chars().all(|c| c.is_ascii() && c.is_alphabetic());
// Should not lint!
"foo".chars().map(|c| c).all(|c| !char::is_ascii(&c));
}

View file

@ -0,0 +1,59 @@
#![warn(clippy::needless_character_iteration)]
#![allow(clippy::map_identity, clippy::unnecessary_operation)]
#[derive(Default)]
struct S {
field: &'static str,
}
impl S {
fn field(&self) -> &str {
self.field
}
}
fn magic(_: char) {}
fn main() {
"foo".chars().all(|c| c.is_ascii());
//~^ ERROR: checking if a string is ascii using iterators
"foo".chars().all(|c| !c.is_ascii());
//~^ ERROR: checking if a string is ascii using iterators
"foo".chars().all(|c| char::is_ascii(&c));
//~^ ERROR: checking if a string is ascii using iterators
"foo".chars().all(|c| !char::is_ascii(&c));
//~^ ERROR: checking if a string is ascii using iterators
let s = String::new();
s.chars().all(|c| c.is_ascii());
//~^ ERROR: checking if a string is ascii using iterators
"foo".to_string().chars().all(|c| !c.is_ascii());
//~^ ERROR: checking if a string is ascii using iterators
"foo".chars().all(|c| {
//~^ ERROR: checking if a string is ascii using iterators
let x = c;
x.is_ascii()
});
"foo".chars().all(|c| {
//~^ ERROR: checking if a string is ascii using iterators
let x = c;
!x.is_ascii()
});
S::default().field().chars().all(|x| x.is_ascii());
//~^ ERROR: checking if a string is ascii using iterators
// Should not lint!
"foo".chars().all(|c| {
let x = c;
magic(x);
x.is_ascii()
});
// Should not lint!
"foo".chars().all(|c| c.is_ascii() && c.is_alphabetic());
// Should not lint!
"foo".chars().map(|c| c).all(|c| !char::is_ascii(&c));
}

View file

@ -0,0 +1,67 @@
error: checking if a string is ascii using iterators
--> tests/ui/needless_character_iteration.rs:18:5
|
LL | "foo".chars().all(|c| c.is_ascii());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"foo".is_ascii()`
|
= note: `-D clippy::needless-character-iteration` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::needless_character_iteration)]`
error: checking if a string is ascii using iterators
--> tests/ui/needless_character_iteration.rs:20:5
|
LL | "foo".chars().all(|c| !c.is_ascii());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `!"foo".is_ascii()`
error: checking if a string is ascii using iterators
--> tests/ui/needless_character_iteration.rs:22:5
|
LL | "foo".chars().all(|c| char::is_ascii(&c));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"foo".is_ascii()`
error: checking if a string is ascii using iterators
--> tests/ui/needless_character_iteration.rs:24:5
|
LL | "foo".chars().all(|c| !char::is_ascii(&c));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `!"foo".is_ascii()`
error: checking if a string is ascii using iterators
--> tests/ui/needless_character_iteration.rs:28:5
|
LL | s.chars().all(|c| c.is_ascii());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `s.is_ascii()`
error: checking if a string is ascii using iterators
--> tests/ui/needless_character_iteration.rs:30:5
|
LL | "foo".to_string().chars().all(|c| !c.is_ascii());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `!"foo".to_string().is_ascii()`
error: checking if a string is ascii using iterators
--> tests/ui/needless_character_iteration.rs:33:5
|
LL | / "foo".chars().all(|c| {
LL | |
LL | | let x = c;
LL | | x.is_ascii()
LL | | });
| |______^ help: try: `"foo".is_ascii()`
error: checking if a string is ascii using iterators
--> tests/ui/needless_character_iteration.rs:38:5
|
LL | / "foo".chars().all(|c| {
LL | |
LL | | let x = c;
LL | | !x.is_ascii()
LL | | });
| |______^ help: try: `!"foo".is_ascii()`
error: checking if a string is ascii using iterators
--> tests/ui/needless_character_iteration.rs:44:5
|
LL | S::default().field().chars().all(|x| x.is_ascii());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `S::default().field().is_ascii()`
error: aborting due to 9 previous errors