Auto merge of #9556 - evantypanski:et/issue-9369, r=Alexendoo
[`redundant_closure`] Fix suggestion causes error for `impl FnMut` Fixes #9369 changelog: [`redundant_closure`] Fix suggestion causes error with `impl FnMut` types
This commit is contained in:
commit
a78551bb14
@ -1,7 +1,7 @@
|
||||
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
|
||||
use clippy_utils::higher::VecArgs;
|
||||
use clippy_utils::source::snippet_opt;
|
||||
use clippy_utils::ty::is_type_diagnostic_item;
|
||||
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
|
||||
use clippy_utils::usage::local_used_after_expr;
|
||||
use clippy_utils::{higher, is_adjusted, path_to_local, path_to_local_id};
|
||||
use if_chain::if_chain;
|
||||
@ -11,7 +11,7 @@
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
|
||||
use rustc_middle::ty::binding::BindingMode;
|
||||
use rustc_middle::ty::{self, ClosureKind, Ty, TypeVisitable};
|
||||
use rustc_middle::ty::{self, Ty, TypeVisitable};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::symbol::sym;
|
||||
|
||||
@ -122,15 +122,12 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
then {
|
||||
span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure", |diag| {
|
||||
if let Some(mut snippet) = snippet_opt(cx, callee.span) {
|
||||
if_chain! {
|
||||
if let ty::Closure(_, substs) = callee_ty.peel_refs().kind();
|
||||
if substs.as_closure().kind() == ClosureKind::FnMut;
|
||||
if path_to_local(callee).map_or(false, |l| local_used_after_expr(cx, l, expr));
|
||||
|
||||
then {
|
||||
if let Some(fn_mut_id) = cx.tcx.lang_items().fn_mut_trait()
|
||||
&& implements_trait(cx, callee_ty.peel_refs(), fn_mut_id, &[])
|
||||
&& path_to_local(callee).map_or(false, |l| local_used_after_expr(cx, l, expr))
|
||||
{
|
||||
// Mutable closure is used after current expr; we cannot consume it.
|
||||
snippet = format!("&mut {snippet}");
|
||||
}
|
||||
}
|
||||
diag.span_suggestion(
|
||||
expr.span,
|
||||
|
@ -303,3 +303,16 @@ fn not_general_enough() {
|
||||
fn f(_: impl FnMut(&Path) -> std::io::Result<()>) {}
|
||||
f(|path| std::fs::remove_file(path));
|
||||
}
|
||||
|
||||
// https://github.com/rust-lang/rust-clippy/issues/9369
|
||||
pub fn mutable_impl_fn_mut(mut f: impl FnMut(), mut f_used_once: impl FnMut()) -> impl FnMut() {
|
||||
fn takes_fn_mut(_: impl FnMut()) {}
|
||||
takes_fn_mut(&mut f);
|
||||
|
||||
fn takes_fn_once(_: impl FnOnce()) {}
|
||||
takes_fn_once(&mut f);
|
||||
|
||||
f();
|
||||
|
||||
move || takes_fn_mut(&mut f_used_once)
|
||||
}
|
||||
|
@ -303,3 +303,16 @@ fn not_general_enough() {
|
||||
fn f(_: impl FnMut(&Path) -> std::io::Result<()>) {}
|
||||
f(|path| std::fs::remove_file(path));
|
||||
}
|
||||
|
||||
// https://github.com/rust-lang/rust-clippy/issues/9369
|
||||
pub fn mutable_impl_fn_mut(mut f: impl FnMut(), mut f_used_once: impl FnMut()) -> impl FnMut() {
|
||||
fn takes_fn_mut(_: impl FnMut()) {}
|
||||
takes_fn_mut(|| f());
|
||||
|
||||
fn takes_fn_once(_: impl FnOnce()) {}
|
||||
takes_fn_once(|| f());
|
||||
|
||||
f();
|
||||
|
||||
move || takes_fn_mut(|| f_used_once())
|
||||
}
|
||||
|
@ -116,5 +116,23 @@ error: redundant closure
|
||||
LL | Some(1).map(|n| in_loop(n));
|
||||
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `in_loop`
|
||||
|
||||
error: aborting due to 19 previous errors
|
||||
error: redundant closure
|
||||
--> $DIR/eta.rs:310:18
|
||||
|
|
||||
LL | takes_fn_mut(|| f());
|
||||
| ^^^^^^ help: replace the closure with the function itself: `&mut f`
|
||||
|
||||
error: redundant closure
|
||||
--> $DIR/eta.rs:313:19
|
||||
|
|
||||
LL | takes_fn_once(|| f());
|
||||
| ^^^^^^ help: replace the closure with the function itself: `&mut f`
|
||||
|
||||
error: redundant closure
|
||||
--> $DIR/eta.rs:317:26
|
||||
|
|
||||
LL | move || takes_fn_mut(|| f_used_once())
|
||||
| ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut f_used_once`
|
||||
|
||||
error: aborting due to 22 previous errors
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user