rust-lang / rust-lang/rust-clippy

redundant_closure_for_method_calls suggests wrong path if code inside module

Open
#9,955 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

.

Lint Name

redundant_closure_for_method_calls

Reproducer

I tried this code:

#![allow(clippy::stable_sort_primitive)]

use std::cell::Ref;
mod issue_5754 {}

// The closure parameter is not dereferenced anymore, so non-Copy types can be linted
mod issue_6001 {
    struct Test(String);

    impl Test {
        // Return an owned type so that we don't hit the fix for 5754
        fn name(&self) -> String {
            self.0.clone()
        }
    }

    pub fn test() {
        let mut args: Vec<Test> = vec![];

        // Forward
        args.sort_by_key(|a| a.name());
        args.sort_unstable_by_key(|a| a.name());
        // Reverse
        args.sort_by(|a, b| b.name().cmp(&a.name()));
        args.sort_unstable_by(|a, b| b.name().cmp(&a.name()));
    }
}

fn main() {
    issue_6001::test();
}

cargo clippy --fix -- -Aclippy::unnecessary_sort_by -Wclippy::pedantic

The following errors were reported:
error[E0433]: failed to resolve: use of undeclared crate or module `issue_6001`
  --> src/main.rs:21:26
   |
21 |         args.sort_by_key(issue_6001::Test::name);
   |                          ^^^^^^^^^^ use of undeclared crate or module `issue_6001`

error[E0433]: failed to resolve: use of undeclared crate or module `issue_6001`
  --> src/main.rs:22:35
   |
22 |         args.sort_unstable_by_key(issue_6001::Test::name);
   |                                   ^^^^^^^^^^ use of undeclared crate or module `issue_6001`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0433`.
Original diagnostics will follow.

warning: unused import: `std::cell::Ref`
 --> src/main.rs:3:5
  |
3 | use std::cell::Ref;
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: redundant closure
  --> src/main.rs:21:26
   |
21 |         args.sort_by_key(|a| a.name());
   |                          ^^^^^^^^^^^^ help: replace the closure with the method itself: `issue_6001::Test::name`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls
   = note: `-W clippy::redundant-closure-for-method-calls` implied by `-W clippy::pedantic`

warning: redundant closure
  --> src/main.rs:22:35
   |
22 |         args.sort_unstable_by_key(|a| a.name());
   |                                   ^^^^^^^^^^^^ help: replace the closure with the method itself: `issue_6001::Test::name`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls
Version
rustc 1.67.0-nightly (b3bc6bf31 2022-11-24)
binary: rustc
commit-hash: b3bc6bf31265ac10946a0832092dbcedf9b26805
commit-date: 2022-11-24
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
Additional Labels

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

Start with the redundant_closure_for_method_calls lint and reproduce the diagnostic using the Rust code and cargo clippy command in the issue. Check how method paths are formed for calls inside issue_6001, then ensure the suggested replacement resolves from that module without E0433. Re-run the reproducer and verify the lint's replacement compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.