rust-lang / rust-lang/rust-clippy

No need to ref to function

Open
#15,464 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Suggests to remove a & from the code in one case.

Advantage

Simpler code.

Drawbacks

I don't know if there are drawbacks.

Example
#![warn(clippy::all)]
#![warn(clippy::nursery)]
#![warn(clippy::pedantic)]

fn exchange_sort(arr: &mut [u32], is_less: &impl Fn(&u32, &u32) -> bool) {
    for i in 0 .. arr.len() - 1 {
        for j in i + 1 .. arr.len() {
            if is_less(&arr[j], &arr[i]) {
                arr.swap(i, j);
            }
        }
    }
}

const fn my_less(a: &u32, b: &u32) -> bool {
    *a < *b
}

fn main() {
    let mut a = [1, 10, 3, 2, 0, 12];
    exchange_sort(&mut a, &my_less);
    println!("{a:?}"); // Prints: [0, 1, 2, 3, 10, 12]
}

Could be written as:

#![warn(clippy::all)]
#![warn(clippy::nursery)]
#![warn(clippy::pedantic)]

fn exchange_sort(arr: &mut [u32], is_less: impl Fn(&u32, &u32) -> bool) {
    for i in 0 .. arr.len() - 1 {
        for j in i + 1 .. arr.len() {
            if is_less(&arr[j], &arr[i]) {
                arr.swap(i, j);
            }
        }
    }
}

const fn my_less(a: &u32, b: &u32) -> bool {
    *a < *b
}

fn main() {
    let mut a = [1, 10, 3, 2, 0, 12];
    exchange_sort(&mut a, my_less);
    println!("{a:?}"); // Prints: [0, 1, 2, 3, 10, 12]
}
Comparison with existing lints

No response

Additional Context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files, tests, or entry points are named. Start by reviewing the two Rust examples and clarify the exact lint scope and expected behavior; done should include an agreed implementation and tests covering the proposed transformation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.