Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clippy applies wrong fix for useless_vec for Edition 2021 #111034

Closed
yongqli opened this issue Apr 30, 2023 · 8 comments · Fixed by rust-lang/rust-clippy#10909
Closed

Clippy applies wrong fix for useless_vec for Edition 2021 #111034

yongqli opened this issue Apr 30, 2023 · 8 comments · Fixed by rust-lang/rust-clippy#10909
Labels
A-clippy Area: Clippy C-bug Category: This is a bug.

Comments

@yongqli
Copy link

yongqli commented Apr 30, 2023

Clippy applies the wrong fix to the following code:

fn g() {
    for x in vec![0, 1] {
        f(x);
    }
}

fn f(x: usize) { }

with the error:

The following errors were reported:
error[E0308]: mismatched types
  --> crate/src/lib.rs:8:11
   |
8  |         f(x);
   |         - ^ expected `usize`, found `&{integer}`
   |         |
   |         arguments to this function are incorrect
   |
note: function defined here
  --> crate/src/lib.rs:12:4
   |
12 | fn f(_x: usize) { }
   |    ^ ---------
help: consider dereferencing the borrow
   |
8  |         f(*x);
   |           +

It seems that clippy replaced vec![0, 1] with &[0, 1], which is not correct. For Edition 2021, it should replace it with [0, 1] instead.

@yongqli yongqli added the C-bug Category: This is a bug. label Apr 30, 2023
@kehrazy
Copy link

kehrazy commented Apr 30, 2023

possibly this can be decorated further with a suggestion, seeing as the current implementation is, indeed, correct - the slice seems to be preferable here.

span_lint_and_sugg(
    cx,
    USELESS_VEC,
    span,
    "useless use of `vec!`",
    "you can use a slice directly",
    "consider making the value a reference",
    snippet,
    applicability,
);

@yongqli
Copy link
Author

yongqli commented May 1, 2023

Now that array.into_iter() iterates by value, vec![]s can be replaced by arrays, although this might bloat compile times by creating many new types.

@jyn514 jyn514 added the A-clippy Area: Clippy label May 1, 2023
bors added a commit to rust-lang/rust-clippy that referenced this issue Jun 8, 2023
Fix `useless_vec` suggestion in `for _ in vec![..]`

Fixes rust-lang/rust#111034

changelog: [`useless_vec`]: Fix suggestion in `for _ in vec![..]`
@sweihub
Copy link

sweihub commented Aug 28, 2023

I have a wrong fix too, please fix the clippy,thanks!

diff --git a/src/operator/mod.rs b/src/operator/mod.rs
index 2b6d6ec..f69838b 100644
--- a/src/operator/mod.rs
+++ b/src/operator/mod.rs
@@ -565,7 +565,7 @@ impl Operator {

 fn is_valid_start(id: &str) -> bool {
     match id.chars().next() {
-        Some(c) => ('a'..='z').contains(&c) || ('A'..='Z').contains(&c),
+        Some(c) => c.is_ascii_lowercase() || c.is_ascii_uppercase(),
         None => false,
     }
 }

@kehrazy
Copy link

kehrazy commented Aug 30, 2023

I have a wrong fix too, please fix the clippy,thanks!

diff --git a/src/operator/mod.rs b/src/operator/mod.rs
index 2b6d6ec..f69838b 100644
--- a/src/operator/mod.rs
+++ b/src/operator/mod.rs
@@ -565,7 +565,7 @@ impl Operator {

 fn is_valid_start(id: &str) -> bool {
     match id.chars().next() {
-        Some(c) => ('a'..='z').contains(&c) || ('A'..='Z').contains(&c),
+        Some(c) => c.is_ascii_lowercase() || c.is_ascii_uppercase(),
         None => false,
     }
 }
  1. offtopic
  2. clippy is right.

@sweihub
Copy link

sweihub commented Aug 30, 2023

I want letter inside [a-zA-Z],no numbers no other characters, however clippy fix as lower/upper case only, how do you call that is right?

@kehrazy
Copy link

kehrazy commented Aug 31, 2023

I want letter inside [a-zA-Z],no numbers no other characters, however clippy fix as lower/upper case only, how do you call that is right?

a.is_ascii_lowercase() corresponds to [a-z].
a.is_ascii_uppercase() corresponds to [A-Z].
[a-z] | [A-Z] corresponds to [a-zA-Z].

How do you call this incorrect?

@sweihub
Copy link

sweihub commented Sep 5, 2023

OK, sorry, the .is_ascii_ is misleading, it should be is_alphabet_.

@kehrazy
Copy link

kehrazy commented Sep 5, 2023

@sweihub Stop bumping the thread, please. Open your own issue, if you believe that the naming is not right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-clippy Area: Clippy C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants