From d09013b95911a263fe31f4e1274aa2214b5dc630 Mon Sep 17 00:00:00 2001 From: lapla-cogito Date: Mon, 30 Dec 2024 00:14:46 +0900 Subject: [PATCH] don't suggest to use std::vec::Vec in a no_std environment --- clippy_lints/src/eta_reduction.rs | 33 ++++++++++++++++++------------- tests/ui/eta_nostd.fixed | 10 ++++++++++ tests/ui/eta_nostd.rs | 10 ++++++++++ tests/ui/eta_nostd.stderr | 11 +++++++++++ 4 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 tests/ui/eta_nostd.fixed create mode 100644 tests/ui/eta_nostd.rs create mode 100644 tests/ui/eta_nostd.stderr diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index 2cd48ef98e52..8b6199d1d906 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -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; @@ -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; diff --git a/tests/ui/eta_nostd.fixed b/tests/ui/eta_nostd.fixed new file mode 100644 index 000000000000..3bb8500898fe --- /dev/null +++ b/tests/ui/eta_nostd.fixed @@ -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> = true.then(alloc::vec::Vec::new); +} diff --git a/tests/ui/eta_nostd.rs b/tests/ui/eta_nostd.rs new file mode 100644 index 000000000000..d2bcef089cfc --- /dev/null +++ b/tests/ui/eta_nostd.rs @@ -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> = true.then(|| vec![]); +} diff --git a/tests/ui/eta_nostd.stderr b/tests/ui/eta_nostd.stderr new file mode 100644 index 000000000000..4dfef43efa47 --- /dev/null +++ b/tests/ui/eta_nostd.stderr @@ -0,0 +1,11 @@ +error: redundant closure + --> tests/ui/eta_nostd.rs:9:40 + | +LL | let _: Option> = 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 +