11
11
use crate :: rustc:: hir:: def_id:: DefId ;
12
12
use crate :: rustc:: hir:: * ;
13
13
use crate :: rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
14
- use crate :: rustc:: { declare_tool_lint, lint_array} ;
15
14
use crate :: rustc:: ty;
15
+ use crate :: rustc:: { declare_tool_lint, lint_array} ;
16
16
use crate :: rustc_data_structures:: fx:: FxHashSet ;
17
+ use crate :: rustc_errors:: Applicability ;
17
18
use crate :: syntax:: ast:: { Lit , LitKind , Name } ;
18
19
use crate :: syntax:: source_map:: { Span , Spanned } ;
19
- use crate :: utils:: { get_item_name, in_macro, snippet , span_lint, span_lint_and_sugg, walk_ptrs_ty} ;
20
+ use crate :: utils:: { get_item_name, in_macro, snippet_with_applicability , span_lint, span_lint_and_sugg, walk_ptrs_ty} ;
20
21
21
22
/// **What it does:** Checks for getting the length of something via `.len()`
22
23
/// just to compare to zero, and suggests using `.is_empty()` where applicable.
@@ -223,7 +224,15 @@ fn check_cmp(cx: &LateContext<'_, '_>, span: Span, method: &Expr, lit: &Expr, op
223
224
}
224
225
}
225
226
226
- fn check_len ( cx : & LateContext < ' _ , ' _ > , span : Span , method_name : Name , args : & [ Expr ] , lit : & Lit , op : & str , compare_to : u32 ) {
227
+ fn check_len (
228
+ cx : & LateContext < ' _ , ' _ > ,
229
+ span : Span ,
230
+ method_name : Name ,
231
+ args : & [ Expr ] ,
232
+ lit : & Lit ,
233
+ op : & str ,
234
+ compare_to : u32 ,
235
+ ) {
227
236
if let Spanned {
228
237
node : LitKind :: Int ( lit, _) ,
229
238
..
@@ -235,13 +244,15 @@ fn check_len(cx: &LateContext<'_, '_>, span: Span, method_name: Name, args: &[Ex
235
244
}
236
245
237
246
if method_name == "len" && args. len ( ) == 1 && has_is_empty ( cx, & args[ 0 ] ) {
247
+ let mut applicability = Applicability :: MachineApplicable ;
238
248
span_lint_and_sugg (
239
249
cx,
240
250
LEN_ZERO ,
241
251
span,
242
252
& format ! ( "length comparison to {}" , if compare_to == 0 { "zero" } else { "one" } ) ,
243
253
"using `is_empty` is clearer and more explicit" ,
244
- format ! ( "{}{}.is_empty()" , op, snippet( cx, args[ 0 ] . span, "_" ) ) ,
254
+ format ! ( "{}{}.is_empty()" , op, snippet_with_applicability( cx, args[ 0 ] . span, "_" , & mut applicability) ) ,
255
+ applicability,
245
256
) ;
246
257
}
247
258
}
0 commit comments