Skip to content

Commit d36cd2a

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

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

clippy_lints/src/eta_reduction.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ 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::{
7+
get_path_from_caller_to_method_type, is_adjusted, is_no_std_crate, path_to_local, path_to_local_id,
8+
};
79
use rustc_errors::Applicability;
810
use rustc_hir::{BindingMode, Expr, ExprKind, FnRetTy, Param, PatKind, QPath, Safety, TyKind};
911
use rustc_infer::infer::TyCtxtInferExt;
@@ -101,19 +103,20 @@ fn check_clousure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tc
101103
};
102104

103105
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-
}
106+
if body.params.is_empty()
107+
&& let Some(VecArgs::Vec(&[])) = VecArgs::hir(cx, body.value)
108+
{
109+
let vec_crate = if is_no_std_crate(cx) { "alloc" } else { "std" };
110+
// replace `|| vec![]` with `Vec::new`
111+
span_lint_and_sugg(
112+
cx,
113+
REDUNDANT_CLOSURE,
114+
expr.span,
115+
"redundant closure",
116+
"replace the closure with `Vec::new`",
117+
format!("{vec_crate}::vec::Vec::new"),
118+
Applicability::MachineApplicable,
119+
);
117120
}
118121
// skip `foo(|| macro!())`
119122
return;

tests/ui/eta_nostd.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![warn(clippy::redundant_closure)]
2+
#![no_std]
3+
4+
extern crate alloc;
5+
use alloc::vec;
6+
use alloc::vec::Vec;
7+
8+
fn issue_13895() {
9+
let _: Option<Vec<u8>> = true.then(alloc::vec::Vec::new);
10+
}

tests/ui/eta_nostd.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![warn(clippy::redundant_closure)]
2+
#![no_std]
3+
4+
extern crate alloc;
5+
use alloc::vec;
6+
use alloc::vec::Vec;
7+
8+
fn issue_13895() {
9+
let _: Option<Vec<u8>> = true.then(|| vec![]);
10+
}

tests/ui/eta_nostd.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: redundant closure
2+
--> tests/ui/eta_nostd.rs:9:40
3+
|
4+
LL | let _: Option<Vec<u8>> = true.then(|| vec![]);
5+
| ^^^^^^^^^ help: replace the closure with `Vec::new`: `alloc::vec::Vec::new`
6+
|
7+
= note: `-D clippy::redundant-closure` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::redundant_closure)]`
9+
10+
error: aborting due to 1 previous error
11+

0 commit comments

Comments
 (0)