Skip to content

Commit f630370

Browse files
committed
don't suggest to use std::vec::Vec in a no_std environment
1 parent 998c780 commit f630370

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

clippy_lints/src/eta_reduction.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::higher::VecArgs;
33
use clippy_utils::source::snippet_opt;
44
use clippy_utils::ty::get_type_diagnostic_name;
55
use clippy_utils::usage::{local_used_after_expr, local_used_in};
6-
use clippy_utils::{get_path_from_caller_to_method_type, is_adjusted, path_to_local, path_to_local_id};
6+
use clippy_utils::{get_path_from_caller_to_method_type, is_adjusted, path_to_local, path_to_local_id, std_or_core};
77
use rustc_errors::Applicability;
88
use rustc_hir::{BindingMode, Expr, ExprKind, FnRetTy, Param, PatKind, QPath, Safety, TyKind};
99
use rustc_infer::infer::TyCtxtInferExt;
@@ -101,19 +101,20 @@ fn check_clousure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tc
101101
};
102102

103103
if body.value.span.from_expansion() {
104-
if body.params.is_empty() {
105-
if let Some(VecArgs::Vec(&[])) = VecArgs::hir(cx, body.value) {
106-
// replace `|| vec![]` with `Vec::new`
107-
span_lint_and_sugg(
108-
cx,
109-
REDUNDANT_CLOSURE,
110-
expr.span,
111-
"redundant closure",
112-
"replace the closure with `Vec::new`",
113-
"std::vec::Vec::new".into(),
114-
Applicability::MachineApplicable,
115-
);
116-
}
104+
if body.params.is_empty() &&let Some(VecArgs::Vec(&[])) = VecArgs::hir(cx, body.value)
105+
// FIXME: When we can correctly determine if `alloc` crate can use in a `no_std` environment, we suggest using `alloc::vec::Vec::new` here.
106+
&& let Some("std")=std_or_core(cx)
107+
{
108+
// replace `|| vec![]` with `Vec::new`
109+
span_lint_and_sugg(
110+
cx,
111+
REDUNDANT_CLOSURE,
112+
expr.span,
113+
"redundant closure",
114+
"replace the closure with `Vec::new`",
115+
"std::vec::Vec::new".into(),
116+
Applicability::MachineApplicable,
117+
);
117118
}
118119
// skip `foo(|| macro!())`
119120
return;

0 commit comments

Comments
 (0)