rust-lang / rust-lang/rust-clippy

redundant_closure_for_method_calls suggest replacing closure with dependency of dependency method.

Open
#8,447 1 comment 1 reaction 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

Clippy suggests replacing a closure with a method itself, but the method itself comes from a crate that I do not have imported.

But I cannot possibly refer to that method.

Reproducer

I tried this code:

use alcoholic_jwt::ValidJWT;

fn main() {
    let token = ValidJWT {
        headers: Default::default(),
        claims: Default::default(),
    };

    let claim = token
        .claims // this is a serde_json object
        .as_object()
        .and_then(|claims| claims.get("my_claim"))
        .and_then(|mc| mc.as_str());

    println!("{:?}", claim);
}

The only thing in my Cargo.toml's dependencies is alcoholic_jwt.

I expected to see this happen:
No clippy warning!

Instead, this happened:
Clippy said:


warning: calling `serde_json::value::Value::default()` is more clear than this expression
 --> src/main.rs:5:18
  |
5 |         headers: Default::default(),
  |                  ^^^^^^^^^^^^^^^^^^ help: try: `serde_json::value::Value::default()`
  |
  = note: `-W clippy::default-trait-access` implied by `-W clippy::pedantic`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_trait_access

warning: calling `serde_json::value::Value::default()` is more clear than this expression
 --> src/main.rs:6:17
  |
6 |         claims: Default::default(),
  |                 ^^^^^^^^^^^^^^^^^^ help: try: `serde_json::value::Value::default()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_trait_access

warning: redundant closure
  --> src/main.rs:13:19
   |
13 |         .and_then(|mc| mc.as_str());
   |                   ^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `serde_json::value::Value::as_str`
   |
   = note: `-W clippy::redundant-closure-for-method-calls` implied by `-W clippy::pedantic`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls

Version
rustc 1.58.1 (db9d1b20b 2022-01-20)
binary: rustc
commit-hash: db9d1b20bba1968c1ec1fc49616d4742c1725b4b
commit-date: 2022-01-20
host: x86_64-unknown-linux-gnu
release: 1.58.1
LLVM version: 13.0.0
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 by reproducing the provided Rust example and inspect the redundant_closure_for_method_calls lint's suggestion logic. The fix is complete when the lint no longer suggests an inaccessible method from an unimported dependency, while valid method suggestions remain available.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.