@@ -3,7 +3,9 @@ use clippy_utils::higher::VecArgs;
33use clippy_utils:: source:: snippet_opt;
44use clippy_utils:: ty:: get_type_diagnostic_name;
55use 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+ } ;
79use rustc_errors:: Applicability ;
810use rustc_hir:: { BindingMode , Expr , ExprKind , FnRetTy , Param , PatKind , QPath , Safety , TyKind } ;
911use 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 ;
0 commit comments