@@ -3,7 +3,9 @@ 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:: {
7
+ get_path_from_caller_to_method_type, is_adjusted, is_no_std_crate, path_to_local, path_to_local_id,
8
+ } ;
7
9
use rustc_errors:: Applicability ;
8
10
use rustc_hir:: { BindingMode , Expr , ExprKind , FnRetTy , Param , PatKind , QPath , Safety , TyKind } ;
9
11
use rustc_infer:: infer:: TyCtxtInferExt ;
@@ -101,19 +103,20 @@ fn check_clousure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tc
101
103
} ;
102
104
103
105
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
+ ) ;
117
120
}
118
121
// skip `foo(|| macro!())`
119
122
return ;
0 commit comments