1111use crate :: rustc:: hir:: def_id:: DefId ;
1212use crate :: rustc:: hir:: * ;
1313use crate :: rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
14- use crate :: rustc:: { declare_tool_lint, lint_array} ;
1514use crate :: rustc:: ty;
15+ use crate :: rustc:: { declare_tool_lint, lint_array} ;
1616use crate :: rustc_data_structures:: fx:: FxHashSet ;
17+ use crate :: rustc_errors:: Applicability ;
1718use crate :: syntax:: ast:: { Lit , LitKind , Name } ;
1819use 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} ;
2021
2122/// **What it does:** Checks for getting the length of something via `.len()`
2223/// 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
223224 }
224225}
225226
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+ ) {
227236 if let Spanned {
228237 node : LitKind :: Int ( lit, _) ,
229238 ..
@@ -235,13 +244,15 @@ fn check_len(cx: &LateContext<'_, '_>, span: Span, method_name: Name, args: &[Ex
235244 }
236245
237246 if method_name == "len" && args. len ( ) == 1 && has_is_empty ( cx, & args[ 0 ] ) {
247+ let mut applicability = Applicability :: MachineApplicable ;
238248 span_lint_and_sugg (
239249 cx,
240250 LEN_ZERO ,
241251 span,
242252 & format ! ( "length comparison to {}" , if compare_to == 0 { "zero" } else { "one" } ) ,
243253 "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,
245256 ) ;
246257 }
247258 }
0 commit comments