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

needless_borrow: suggest removing several references at a time. #5327

Closed
disconnect3d opened this issue Mar 17, 2020 · 5 comments · Fixed by #7977
Closed

needless_borrow: suggest removing several references at a time. #5327

disconnect3d opened this issue Mar 17, 2020 · 5 comments · Fixed by #7977
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-style Lint: Belongs in the style lint group L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@disconnect3d
Copy link

Hey,

It would be nice if Clippy could detect needless references as in here:

fn main() {
    let st = String::from("zomg");
    myfunc(&&&&&st);
}

fn myfunc(param: &str) {
    println!("param: {}", param);
}

The myfunc(&&&&&st); should be myfunc(&st);.

Side note: there is a similar, needless lifetime parameter detecting, that would trigger in e.g. fn myfunc<'a>(param: &'a str)

@matthiaskrgr
Copy link
Member

Hi, this is already implemented but you have to opt-in manually (#[warn(clippy::needless_borrow)] )

#[warn(clippy::needless_borrow)]
fn main() {
    let st = String::from("zomg");
    myfunc(&&&&&st);
}

fn myfunc(param: &str) {
    println!("param: {}", param);
}

should make clippy warn about it. :)

@disconnect3d
Copy link
Author

@matthiaskrgr is there a way to opt in with a CLI argument? Also, how do I list all linters/detectors?

@disconnect3d
Copy link
Author

disconnect3d commented Mar 17, 2020

@matthiaskrgr Also, the suggestion given is not the best one and applying it and re-running cargo clippy will still warn:

➜  refs git:(master) ✗ cargo clippy
    Checking refs v0.1.0 (/Users/dc/playground/rust-2/refs)
warning: this expression borrows a reference that is immediately dereferenced by the compiler
 --> src/main.rs:6:12
  |
6 |     myfunc(&&&&&st);
  |            ^^^^^^^ help: change this to: `&&&&st`
  |
note: lint level defined here
 --> src/main.rs:1:8
  |
1 | #[warn(clippy::needless_borrow)]
  |        ^^^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

    Finished dev [unoptimized + debuginfo] target(s) in 0.12s

seems like something to be improved

@matthiaskrgr matthiaskrgr reopened this Mar 17, 2020
@matthiaskrgr
Copy link
Member

matthiaskrgr commented Mar 17, 2020

cargo clippy -- -Wclippy::needless-borrow should work.

Mhh, looks like the lint only looks at one reference at a time.. 🤔

EDIT: link -> lint

@matthiaskrgr matthiaskrgr changed the title Detect needless references needless_borrow: suggest removing several references at a time. Mar 17, 2020
@matthiaskrgr matthiaskrgr added L-style Lint: Belongs in the style lint group L-suggestion Lint: Improving, adding or fixing lint suggestions C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels Mar 17, 2020
@flip1995 flip1995 added the E-medium Call for participation: Medium difficulty level problem and requires some initial experience. label Mar 17, 2020
@flip1995
Copy link
Member

All lints are listed here: https://rust-lang.github.io/rust-clippy/

Just choose the version of Clippy you're using and it will show you all lints in this version with their documentation. (master ~= nightly)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-style Lint: Belongs in the style lint group L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants