Skip to content

Commit

Permalink
don't suggest to use std::vec::Vec in a no_std environment
Browse files Browse the repository at this point in the history
  • Loading branch information
lapla-cogito committed Dec 29, 2024
1 parent 998c780 commit f630370
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions clippy_lints/src/eta_reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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, path_to_local, path_to_local_id, std_or_core};
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 +101,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)
// FIXME: When we can correctly determine if `alloc` crate can use in a `no_std` environment, we suggest using `alloc::vec::Vec::new` here.
&& let Some("std")=std_or_core(cx)
{
// 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,
);
}
// skip `foo(|| macro!())`
return;
Expand Down

0 comments on commit f630370

Please sign in to comment.