Skip to content
Merged
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 @@ -12,6 +12,7 @@ use rustc_hir::attrs::AttributeKind;
use rustc_hir::{BindingMode, Expr, ExprKind, FnRetTy, GenericArgs, Param, PatKind, QPath, Safety, TyKind, find_attr};
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 @@ -148,10 +149,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 @@ -230,7 +230,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));
}

let replace_with = match callee_ty_adjusted.kind() {
ty::FnDef(def, _) => cx.tcx.def_descr(*def),
_ => "function",
Expand Down
45 changes: 45 additions & 0 deletions tests/ui/eta.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,51 @@ fn issue_14789() {
);
}

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
}

fn issue8817() {
fn f(_: u32) -> u32 {
todo!()
Expand Down
45 changes: 45 additions & 0 deletions tests/ui/eta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,51 @@ fn issue_14789() {
);
}

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
}

fn issue8817() {
fn f(_: u32) -> u32 {
todo!()
Expand Down
28 changes: 23 additions & 5 deletions tests/ui/eta.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -215,28 +215,46 @@ LL | let _field = bind.or_else(|| get_default()).unwrap();
| ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `get_default`

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

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

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

error: redundant closure
--> tests/ui/eta.rs:633:14
|
LL | .map(|n| MyError::A(n))
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the tuple variant itself: `MyError::A`

error: redundant closure
--> tests/ui/eta.rs:585:14
--> tests/ui/eta.rs:630:14
|
LL | .map(|n| S(n))
| ^^^^^^^^ help: replace the closure with the tuple struct itself: `S`

error: redundant closure
--> tests/ui/eta.rs:582:14
--> tests/ui/eta.rs:627:14
|
LL | .map(|n| g(n))
| ^^^^^^^^ help: replace the closure with the function itself: `g`

error: redundant closure
--> tests/ui/eta.rs:579:14
--> tests/ui/eta.rs:624:14
|
LL | .map(|n| f(n))
| ^^^^^^^^ help: replace the closure with the function itself: `f`

error: aborting due to 39 previous errors
error: aborting due to 42 previous errors