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

redundant_closure_for_method_calls suggests wrong path if code inside module #9955

Open
matthiaskrgr opened this issue Nov 25, 2022 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

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

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 25, 2022
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 I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

1 participant