rust-lang / rust-lang/rust-clippy

redundant-closure-for-method-calls suggests <dyn trait>::fn which does not compile

Open
#10,446 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:

#![feature(error_generic_member_access, provide_any)]

use std::backtrace::{Backtrace};
use std::error::Error;

const _: fn(&dyn Error) -> Option<&Backtrace> = |err| err.request_ref::<Backtrace>();

pub fn main() {}

I saw this happen:

warning: redundant closure
 --> src/main.rs:6:49
  |
6 | const _: fn(&dyn Error) -> Option<&Backtrace> = |err| err.request_ref::<Backtrace>();
  |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<dyn std::error::Error>::request_ref`
  |

Suggested code does not compile

The following errors were reported:
error[E0308]: mismatched types
 --> src/main.rs:6:49
  |
6 | const _: fn(&dyn Error) -> Option<&Backtrace> = <dyn std::error::Error>::request_ref;
  |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected fn pointer `for<'a> fn(&'a (dyn std::error::Error + 'a)) -> std::option::Option<&'a std::backtrace::Backtrace>`
                found fn item `fn(&dyn std::error::Error) -> std::option::Option<&_> {<dyn std::error::Error>::request_ref::<_>}`
Version
rustc 1.69.0-nightly (b1719530f 2023-03-04)
binary: rustc
commit-hash: b1719530f44e3c8ec903f76020a52bd8764d5d10
commit-date: 2023-03-04
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7
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 running the Rust reproducer in the issue and comparing the redundant-closure-for-method-calls warning with the suggested method expression. Trace where this lint constructs its replacement, then verify the suggested code compiles for the shown higher-ranked function-pointer case. Done means the warning no longer suggests code that fails to compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
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.