Skip to content

Fix redundant_closure suggests wrongly with deref overload #15077

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions clippy_lints/src/eta_reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rustc_errors::Applicability;
use rustc_hir::{BindingMode, Expr, ExprKind, FnRetTy, GenericArgs, Param, PatKind, QPath, Safety, TyKind};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::adjustment::Adjust;
use rustc_middle::ty::{
self, Binder, ClosureKind, FnSig, GenericArg, GenericArgKind, List, Region, Ty, TypeVisitableExt, TypeckResults,
};
Expand Down Expand Up @@ -147,10 +148,9 @@ fn check_closure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tcx
{
return;
}
let callee_ty_adjusted = typeck
.expr_adjustments(callee)
.last()
.map_or(callee_ty, |a| a.target.peel_refs());

let callee_ty_adjustments = typeck.expr_adjustments(callee);
let callee_ty_adjusted = callee_ty_adjustments.last().map_or(callee_ty, |a| a.target);

let sig = match callee_ty_adjusted.kind() {
ty::FnDef(def, _) => {
Expand Down Expand Up @@ -223,7 +223,20 @@ fn check_closure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tcx
},
_ => (),
}
} else if let n_refs =
callee_ty_adjustments
.iter()
.rev()
.fold(0, |acc, adjustment| match adjustment.kind {
Adjust::Deref(Some(_)) => acc + 1,
Adjust::Deref(_) if acc > 0 => acc + 1,
_ => acc,
})
&& n_refs > 0
{
snippet = format!("{}{snippet}", "*".repeat(n_refs));
}

diag.span_suggestion(
expr.span,
"replace the closure with the function itself",
Expand Down
45 changes: 45 additions & 0 deletions tests/ui/eta.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,48 @@ mod issue_13073 {
//~^ redundant_closure
}
}

fn issue_15072() {
use std::ops::Deref;

struct Foo;
impl Deref for Foo {
type Target = fn() -> &'static str;

fn deref(&self) -> &Self::Target {
fn hello() -> &'static str {
"Hello, world!"
}
&(hello as fn() -> &'static str)
}
}

fn accepts_fn(f: impl Fn() -> &'static str) {
println!("{}", f());
}

fn some_fn() -> &'static str {
todo!()
}

let f = &Foo;
accepts_fn(**f);
//~^ redundant_closure

let g = &some_fn;
accepts_fn(g);
//~^ redundant_closure

struct Bar(Foo);
impl Deref for Bar {
type Target = Foo;

fn deref(&self) -> &Self::Target {
&self.0
}
}

let b = &Bar(Foo);
accepts_fn(***b);
//~^ redundant_closure
}
45 changes: 45 additions & 0 deletions tests/ui/eta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,48 @@ mod issue_13073 {
//~^ redundant_closure
}
}

fn issue_15072() {
use std::ops::Deref;

struct Foo;
impl Deref for Foo {
type Target = fn() -> &'static str;

fn deref(&self) -> &Self::Target {
fn hello() -> &'static str {
"Hello, world!"
}
&(hello as fn() -> &'static str)
}
}

fn accepts_fn(f: impl Fn() -> &'static str) {
println!("{}", f());
}

fn some_fn() -> &'static str {
todo!()
}

let f = &Foo;
accepts_fn(|| f());
//~^ redundant_closure

let g = &some_fn;
accepts_fn(|| g());
//~^ redundant_closure

struct Bar(Foo);
impl Deref for Bar {
type Target = Foo;

fn deref(&self) -> &Self::Target {
&self.0
}
}

let b = &Bar(Foo);
accepts_fn(|| b());
//~^ redundant_closure
}
20 changes: 19 additions & 1 deletion tests/ui/eta.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,23 @@ error: redundant closure
LL | let _field = bind.or_else(|| get_default()).unwrap();
| ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `get_default`

error: aborting due to 35 previous errors
error: redundant closure
--> tests/ui/eta.rs:571:16
|
LL | accepts_fn(|| f());
| ^^^^^^ help: replace the closure with the function itself: `**f`

error: redundant closure
--> tests/ui/eta.rs:575:16
|
LL | accepts_fn(|| g());
| ^^^^^^ help: replace the closure with the function itself: `g`

error: redundant closure
--> tests/ui/eta.rs:588:16
|
LL | accepts_fn(|| b());
| ^^^^^^ help: replace the closure with the function itself: `***b`

error: aborting due to 38 previous errors