Skip to content

correct suggestion for redundant_closure lint in a no_std environment #13896

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

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 17 additions & 14 deletions clippy_lints/src/eta_reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use clippy_utils::higher::VecArgs;
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::get_type_diagnostic_name;
use clippy_utils::usage::{local_used_after_expr, local_used_in};
use clippy_utils::{get_path_from_caller_to_method_type, is_adjusted, path_to_local, path_to_local_id};
use clippy_utils::{
get_path_from_caller_to_method_type, is_adjusted, is_no_std_crate, path_to_local, path_to_local_id,
};
use rustc_errors::Applicability;
use rustc_hir::{BindingMode, Expr, ExprKind, FnRetTy, Param, PatKind, QPath, Safety, TyKind};
use rustc_infer::infer::TyCtxtInferExt;
Expand Down Expand Up @@ -101,19 +103,20 @@ fn check_clousure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tc
};

if body.value.span.from_expansion() {
if body.params.is_empty() {
if let Some(VecArgs::Vec(&[])) = VecArgs::hir(cx, body.value) {
// replace `|| vec![]` with `Vec::new`
span_lint_and_sugg(
cx,
REDUNDANT_CLOSURE,
expr.span,
"redundant closure",
"replace the closure with `Vec::new`",
"std::vec::Vec::new".into(),
Applicability::MachineApplicable,
);
}
if body.params.is_empty()
&& let Some(VecArgs::Vec(&[])) = VecArgs::hir(cx, body.value)
{
let vec_crate = if is_no_std_crate(cx) { "alloc" } else { "std" };
// replace `|| vec![]` with `Vec::new`
span_lint_and_sugg(
cx,
REDUNDANT_CLOSURE,
expr.span,
"redundant closure",
"replace the closure with `Vec::new`",
format!("{vec_crate}::vec::Vec::new"),
Applicability::MachineApplicable,
);
}
// skip `foo(|| macro!())`
return;
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/eta_nostd.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![warn(clippy::redundant_closure)]
#![no_std]

extern crate alloc;
use alloc::vec;
use alloc::vec::Vec;

fn issue_13895() {
let _: Option<Vec<u8>> = true.then(alloc::vec::Vec::new);
}
10 changes: 10 additions & 0 deletions tests/ui/eta_nostd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![warn(clippy::redundant_closure)]
#![no_std]

extern crate alloc;
use alloc::vec;
use alloc::vec::Vec;

fn issue_13895() {
let _: Option<Vec<u8>> = true.then(|| vec![]);
}
11 changes: 11 additions & 0 deletions tests/ui/eta_nostd.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: redundant closure
--> tests/ui/eta_nostd.rs:9:40
|
LL | let _: Option<Vec<u8>> = true.then(|| vec![]);
| ^^^^^^^^^ help: replace the closure with `Vec::new`: `alloc::vec::Vec::new`
|
= note: `-D clippy::redundant-closure` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::redundant_closure)]`

error: aborting due to 1 previous error

Loading