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 d09013b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
33 changes: 19 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,24 @@ 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 let Some("std") = std_or_core(cx) {
"std"
} else {
"alloc"
};
// 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

0 comments on commit d09013b

Please sign in to comment.