@@ -3,7 +3,7 @@ use clippy_utils::higher::VecArgs;
3
3
use clippy_utils:: source:: snippet_opt;
4
4
use clippy_utils:: ty:: get_type_diagnostic_name;
5
5
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 } ;
7
7
use rustc_errors:: Applicability ;
8
8
use rustc_hir:: { BindingMode , Expr , ExprKind , FnRetTy , Param , PatKind , QPath , Safety , TyKind } ;
9
9
use rustc_infer:: infer:: TyCtxtInferExt ;
@@ -101,19 +101,24 @@ fn check_clousure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tc
101
101
} ;
102
102
103
103
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 ( )
105
+ && let Some ( VecArgs :: Vec ( & [ ] ) ) = VecArgs :: hir ( cx, body. value )
106
+ {
107
+ let vec_crate = if let Some ( "std" ) = std_or_core ( cx) {
108
+ "std"
109
+ } else {
110
+ "alloc"
111
+ } ;
112
+ // replace `|| vec![]` with `Vec::new`
113
+ span_lint_and_sugg (
114
+ cx,
115
+ REDUNDANT_CLOSURE ,
116
+ expr. span ,
117
+ "redundant closure" ,
118
+ "replace the closure with `Vec::new`" ,
119
+ format ! ( "{vec_crate}::vec::Vec::new" ) ,
120
+ Applicability :: MachineApplicable ,
121
+ ) ;
117
122
}
118
123
// skip `foo(|| macro!())`
119
124
return ;
0 commit comments