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

useless_vec doesn't suggest a necessary & #2256

Closed
isaacg1 opened this issue Dec 3, 2017 · 1 comment · Fixed by #10909
Closed

useless_vec doesn't suggest a necessary & #2256

isaacg1 opened this issue Dec 3, 2017 · 1 comment · Fixed by #10909
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-middle Type: Probably requires verifiying types

Comments

@isaacg1
Copy link

isaacg1 commented Dec 3, 2017

In some cases, the useless_vec warning will suggest changing a vec![...] to a &[...]. In this case, a * or a & may be required. This is not mentioned in the help text or in the extended help text at https://rust-lang-nursery.github.io/rust-clippy/master/index.html#useless_vec

A test case reproducing this:

fn main() {
    for a in vec![0] {
        foo(a)
    }
}

fn foo(a: i64) {
    println!("{}", a)
}

Clippy will suggest changing vec![0] to &[0]. A & before the a in for a in is also needed, or similar. While the compiler will suggest this change, it'd be better to make this clear up front.

@oli-obk oli-obk added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages T-middle Type: Probably requires verifiying types labels Dec 3, 2017
@camsteffen camsteffen added C-bug Category: Clippy is not doing the correct thing and removed C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels Nov 14, 2021
@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,
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-middle Type: Probably requires verifiying types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants