Skip to content

redundant_closure_for_method_calls suggests wrong path if code inside module #9955

Open

Description

@matthiaskrgr

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

Activity

  1. added
    C-bugCategory: Clippy is not doing the correct thing
    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
    I-false-positiveIssue: The lint was triggered on code it shouldn't have
    on Nov 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions