@@ -32,8 +32,7 @@ use crate::utils::{
32
32
is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, match_qpath,
33
33
match_trait_method, match_type, match_var, method_calls, method_chain_args, paths, remove_blocks, return_ty,
34
34
single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
35
- span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then, sugg, walk_ptrs_ty_depth,
36
- SpanlessEq ,
35
+ span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg, walk_ptrs_ty_depth, SpanlessEq ,
37
36
} ;
38
37
39
38
declare_clippy_lint ! {
@@ -1735,7 +1734,7 @@ fn lint_or_fun_call<'tcx>(
1735
1734
"try this" ,
1736
1735
format!(
1737
1736
"{}.unwrap_or_default()" ,
1738
- snippet_with_applicability( cx, self_expr. span, "_ " , & mut applicability)
1737
+ snippet_with_applicability( cx, self_expr. span, ".. " , & mut applicability)
1739
1738
) ,
1740
1739
applicability,
1741
1740
) ;
@@ -2142,7 +2141,7 @@ fn lint_clone_on_ref_ptr(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::
2142
2141
return ;
2143
2142
} ;
2144
2143
2145
- let snippet = snippet_with_macro_callsite ( cx, arg. span , "_ " ) ;
2144
+ let snippet = snippet_with_macro_callsite ( cx, arg. span , ".. " ) ;
2146
2145
2147
2146
span_lint_and_sugg (
2148
2147
cx,
@@ -2178,9 +2177,9 @@ fn lint_string_extend(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::E
2178
2177
"try this" ,
2179
2178
format ! (
2180
2179
"{}.push_str({}{})" ,
2181
- snippet_with_applicability( cx, args[ 0 ] . span, "_ " , & mut applicability) ,
2180
+ snippet_with_applicability( cx, args[ 0 ] . span, ".. " , & mut applicability) ,
2182
2181
ref_str,
2183
- snippet_with_applicability( cx, target. span, "_ " , & mut applicability)
2182
+ snippet_with_applicability( cx, target. span, ".. " , & mut applicability)
2184
2183
) ,
2185
2184
applicability,
2186
2185
) ;
@@ -2427,7 +2426,7 @@ fn lint_get_unwrap<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, get_args:
2427
2426
let mut applicability = Applicability :: MachineApplicable ;
2428
2427
let expr_ty = cx. typeck_results ( ) . expr_ty ( & get_args[ 0 ] ) ;
2429
2428
let get_args_str = if get_args. len ( ) > 1 {
2430
- snippet_with_applicability ( cx, get_args[ 1 ] . span , "_ " , & mut applicability)
2429
+ snippet_with_applicability ( cx, get_args[ 1 ] . span , ".. " , & mut applicability)
2431
2430
} else {
2432
2431
return ; // not linting on a .get().unwrap() chain or variant
2433
2432
} ;
@@ -2487,7 +2486,7 @@ fn lint_get_unwrap<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, get_args:
2487
2486
format ! (
2488
2487
"{}{}[{}]" ,
2489
2488
borrow_str,
2490
- snippet_with_applicability( cx, get_args[ 0 ] . span, "_ " , & mut applicability) ,
2489
+ snippet_with_applicability( cx, get_args[ 0 ] . span, ".. " , & mut applicability) ,
2491
2490
get_args_str
2492
2491
) ,
2493
2492
applicability,
@@ -2503,7 +2502,7 @@ fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[
2503
2502
cx,
2504
2503
ITER_SKIP_NEXT ,
2505
2504
expr. span . trim_start ( caller. span ) . unwrap ( ) ,
2506
- "called `skip(x ).next()` on an iterator" ,
2505
+ "called `skip(.. ).next()` on an iterator" ,
2507
2506
"use `nth` instead" ,
2508
2507
hint,
2509
2508
Applicability :: MachineApplicable ,
@@ -2706,11 +2705,11 @@ fn lint_map_unwrap_or_else<'tcx>(
2706
2705
2707
2706
// lint message
2708
2707
let msg = if is_option {
2709
- "called `map(f ).unwrap_or_else(g )` on an `Option` value. This can be done more directly by calling \
2710
- `map_or_else(g, f )` instead"
2708
+ "called `map(<f> ).unwrap_or_else(<g> )` on an `Option` value. This can be done more directly by calling \
2709
+ `map_or_else(<g>, <f> )` instead"
2711
2710
} else {
2712
- "called `map(f ).unwrap_or_else(g )` on a `Result` value. This can be done more directly by calling \
2713
- `.map_or_else(g, f )` instead"
2711
+ "called `map(<f> ).unwrap_or_else(<g> )` on a `Result` value. This can be done more directly by calling \
2712
+ `.map_or_else(<g>, <f> )` instead"
2714
2713
} ;
2715
2714
// get snippets for args to map() and unwrap_or_else()
2716
2715
let map_snippet = snippet ( cx, map_args[ 1 ] . span , ".." ) ;
@@ -2720,16 +2719,15 @@ fn lint_map_unwrap_or_else<'tcx>(
2720
2719
let multiline = map_snippet. lines ( ) . count ( ) > 1 || unwrap_snippet. lines ( ) . count ( ) > 1 ;
2721
2720
let same_span = map_args[ 1 ] . span . ctxt ( ) == unwrap_args[ 1 ] . span . ctxt ( ) ;
2722
2721
if same_span && !multiline {
2723
- span_lint_and_note (
2722
+ let var_snippet = snippet ( cx, map_args[ 0 ] . span , ".." ) ;
2723
+ span_lint_and_sugg (
2724
2724
cx,
2725
2725
MAP_UNWRAP_OR ,
2726
2726
expr. span ,
2727
2727
msg,
2728
- None ,
2729
- & format ! (
2730
- "replace `map({0}).unwrap_or_else({1})` with `map_or_else({1}, {0})`" ,
2731
- map_snippet, unwrap_snippet,
2732
- ) ,
2728
+ "try this" ,
2729
+ format ! ( "{}.map_or_else({}, {})" , var_snippet, unwrap_snippet, map_snippet) ,
2730
+ Applicability :: MachineApplicable ,
2733
2731
) ;
2734
2732
return true ;
2735
2733
} else if same_span && multiline {
@@ -2776,8 +2774,8 @@ fn lint_map_or_none<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map
2776
2774
if is_option {
2777
2775
let self_snippet = snippet ( cx, map_or_args[ 0 ] . span , ".." ) ;
2778
2776
let func_snippet = snippet ( cx, map_or_args[ 2 ] . span , ".." ) ;
2779
- let msg = "called `map_or(None, f )` on an `Option` value. This can be done more directly by calling \
2780
- `and_then(f )` instead";
2777
+ let msg = "called `map_or(None, .. )` on an `Option` value. This can be done more directly by calling \
2778
+ `and_then(.. )` instead";
2781
2779
(
2782
2780
OPTION_MAP_OR_NONE ,
2783
2781
msg,
@@ -2815,18 +2813,20 @@ fn lint_map_or_none<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map
2815
2813
fn lint_filter_next < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx hir:: Expr < ' _ > , filter_args : & ' tcx [ hir:: Expr < ' _ > ] ) {
2816
2814
// lint if caller of `.filter().next()` is an Iterator
2817
2815
if match_trait_method ( cx, expr, & paths:: ITERATOR ) {
2818
- let msg = "called `filter(p ).next()` on an `Iterator`. This is more succinctly expressed by calling \
2819
- `.find(p )` instead.";
2816
+ let msg = "called `filter(.. ).next()` on an `Iterator`. This is more succinctly expressed by calling \
2817
+ `.find(.. )` instead.";
2820
2818
let filter_snippet = snippet ( cx, filter_args[ 1 ] . span , ".." ) ;
2821
2819
if filter_snippet. lines ( ) . count ( ) <= 1 {
2820
+ let iter_snippet = snippet ( cx, filter_args[ 0 ] . span , ".." ) ;
2822
2821
// add note if not multi-line
2823
- span_lint_and_note (
2822
+ span_lint_and_sugg (
2824
2823
cx,
2825
2824
FILTER_NEXT ,
2826
2825
expr. span ,
2827
2826
msg,
2828
- None ,
2829
- & format ! ( "replace `filter({0}).next()` with `find({0})`" , filter_snippet) ,
2827
+ "try this" ,
2828
+ format ! ( "{}.find({})" , iter_snippet, filter_snippet) ,
2829
+ Applicability :: MachineApplicable ,
2830
2830
) ;
2831
2831
} else {
2832
2832
span_lint ( cx, FILTER_NEXT , expr. span , msg) ;
@@ -2846,9 +2846,9 @@ fn lint_skip_while_next<'tcx>(
2846
2846
cx,
2847
2847
SKIP_WHILE_NEXT ,
2848
2848
expr. span ,
2849
- "called `skip_while(p ).next()` on an `Iterator`" ,
2849
+ "called `skip_while(<p> ).next()` on an `Iterator`" ,
2850
2850
None ,
2851
- "this is more succinctly expressed by calling `.find(!p )` instead" ,
2851
+ "this is more succinctly expressed by calling `.find(!<p> )` instead" ,
2852
2852
) ;
2853
2853
}
2854
2854
}
@@ -2862,7 +2862,7 @@ fn lint_filter_map<'tcx>(
2862
2862
) {
2863
2863
// lint if caller of `.filter().map()` is an Iterator
2864
2864
if match_trait_method ( cx, expr, & paths:: ITERATOR ) {
2865
- let msg = "called `filter(p ).map(q )` on an `Iterator`" ;
2865
+ let msg = "called `filter(.. ).map(.. )` on an `Iterator`" ;
2866
2866
let hint = "this is more succinctly expressed by calling `.filter_map(..)` instead" ;
2867
2867
span_lint_and_help ( cx, FILTER_MAP , expr. span , msg, None , hint) ;
2868
2868
}
@@ -2871,17 +2871,19 @@ fn lint_filter_map<'tcx>(
2871
2871
/// lint use of `filter_map().next()` for `Iterators`
2872
2872
fn lint_filter_map_next < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx hir:: Expr < ' _ > , filter_args : & ' tcx [ hir:: Expr < ' _ > ] ) {
2873
2873
if match_trait_method ( cx, expr, & paths:: ITERATOR ) {
2874
- let msg = "called `filter_map(p ).next()` on an `Iterator`. This is more succinctly expressed by calling \
2875
- `.find_map(p )` instead.";
2874
+ let msg = "called `filter_map(.. ).next()` on an `Iterator`. This is more succinctly expressed by calling \
2875
+ `.find_map(.. )` instead.";
2876
2876
let filter_snippet = snippet ( cx, filter_args[ 1 ] . span , ".." ) ;
2877
2877
if filter_snippet. lines ( ) . count ( ) <= 1 {
2878
- span_lint_and_note (
2878
+ let iter_snippet = snippet ( cx, filter_args[ 0 ] . span , ".." ) ;
2879
+ span_lint_and_sugg (
2879
2880
cx,
2880
2881
FILTER_MAP_NEXT ,
2881
2882
expr. span ,
2882
2883
msg,
2883
- None ,
2884
- & format ! ( "replace `filter_map({0}).next()` with `find_map({0})`" , filter_snippet) ,
2884
+ "try this" ,
2885
+ format ! ( "{}.find_map({})" , iter_snippet, filter_snippet) ,
2886
+ Applicability :: MachineApplicable ,
2885
2887
) ;
2886
2888
} else {
2887
2889
span_lint ( cx, FILTER_MAP_NEXT , expr. span , msg) ;
@@ -2898,7 +2900,7 @@ fn lint_find_map<'tcx>(
2898
2900
) {
2899
2901
// lint if caller of `.filter().map()` is an Iterator
2900
2902
if match_trait_method ( cx, & map_args[ 0 ] , & paths:: ITERATOR ) {
2901
- let msg = "called `find(p ).map(q )` on an `Iterator`" ;
2903
+ let msg = "called `find(.. ).map(.. )` on an `Iterator`" ;
2902
2904
let hint = "this is more succinctly expressed by calling `.find_map(..)` instead" ;
2903
2905
span_lint_and_help ( cx, FIND_MAP , expr. span , msg, None , hint) ;
2904
2906
}
@@ -2913,7 +2915,7 @@ fn lint_filter_map_map<'tcx>(
2913
2915
) {
2914
2916
// lint if caller of `.filter().map()` is an Iterator
2915
2917
if match_trait_method ( cx, expr, & paths:: ITERATOR ) {
2916
- let msg = "called `filter_map(p ).map(q )` on an `Iterator`" ;
2918
+ let msg = "called `filter_map(.. ).map(.. )` on an `Iterator`" ;
2917
2919
let hint = "this is more succinctly expressed by only calling `.filter_map(..)` instead" ;
2918
2920
span_lint_and_help ( cx, FILTER_MAP , expr. span , msg, None , hint) ;
2919
2921
}
@@ -2928,7 +2930,7 @@ fn lint_filter_flat_map<'tcx>(
2928
2930
) {
2929
2931
// lint if caller of `.filter().flat_map()` is an Iterator
2930
2932
if match_trait_method ( cx, expr, & paths:: ITERATOR ) {
2931
- let msg = "called `filter(p ).flat_map(q )` on an `Iterator`" ;
2933
+ let msg = "called `filter(.. ).flat_map(.. )` on an `Iterator`" ;
2932
2934
let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
2933
2935
and filtering by returning `iter::empty()`";
2934
2936
span_lint_and_help ( cx, FILTER_MAP , expr. span , msg, None , hint) ;
@@ -2944,7 +2946,7 @@ fn lint_filter_map_flat_map<'tcx>(
2944
2946
) {
2945
2947
// lint if caller of `.filter_map().flat_map()` is an Iterator
2946
2948
if match_trait_method ( cx, expr, & paths:: ITERATOR ) {
2947
- let msg = "called `filter_map(p ).flat_map(q )` on an `Iterator`" ;
2949
+ let msg = "called `filter_map(.. ).flat_map(.. )` on an `Iterator`" ;
2948
2950
let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
2949
2951
and filtering by returning `iter::empty()`";
2950
2952
span_lint_and_help ( cx, FILTER_MAP , expr. span , msg, None , hint) ;
@@ -3115,9 +3117,9 @@ fn lint_chars_cmp(
3115
3117
"like this" ,
3116
3118
format!( "{}{}.{}({})" ,
3117
3119
if info. eq { "" } else { "!" } ,
3118
- snippet_with_applicability( cx, args[ 0 ] [ 0 ] . span, "_ " , & mut applicability) ,
3120
+ snippet_with_applicability( cx, args[ 0 ] [ 0 ] . span, ".. " , & mut applicability) ,
3119
3121
suggest,
3120
- snippet_with_applicability( cx, arg_char[ 0 ] . span, "_ " , & mut applicability) ) ,
3122
+ snippet_with_applicability( cx, arg_char[ 0 ] . span, ".. " , & mut applicability) ) ,
3121
3123
applicability,
3122
3124
) ;
3123
3125
@@ -3164,7 +3166,7 @@ fn lint_chars_cmp_with_unwrap<'tcx>(
3164
3166
"like this" ,
3165
3167
format!( "{}{}.{}('{}')" ,
3166
3168
if info. eq { "" } else { "!" } ,
3167
- snippet_with_applicability( cx, args[ 0 ] [ 0 ] . span, "_ " , & mut applicability) ,
3169
+ snippet_with_applicability( cx, args[ 0 ] [ 0 ] . span, ".. " , & mut applicability) ,
3168
3170
suggest,
3169
3171
c) ,
3170
3172
applicability,
@@ -3239,7 +3241,7 @@ fn lint_single_char_pattern(cx: &LateContext<'_>, _expr: &hir::Expr<'_>, arg: &h
3239
3241
fn lint_single_char_push_string ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > , args : & [ hir:: Expr < ' _ > ] ) {
3240
3242
let mut applicability = Applicability :: MachineApplicable ;
3241
3243
if let Some ( extension_string) = get_hint_if_single_char_arg ( cx, & args[ 1 ] , & mut applicability) {
3242
- let base_string_snippet = snippet_with_applicability ( cx, args[ 0 ] . span , "_ " , & mut applicability) ;
3244
+ let base_string_snippet = snippet_with_applicability ( cx, args[ 0 ] . span , ".. " , & mut applicability) ;
3243
3245
let sugg = format ! ( "{}.push({})" , base_string_snippet, extension_string) ;
3244
3246
span_lint_and_sugg (
3245
3247
cx,
@@ -3282,7 +3284,7 @@ fn lint_asref(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_re
3282
3284
expr. span ,
3283
3285
& format ! ( "this call to `{}` does nothing" , call_name) ,
3284
3286
"try this" ,
3285
- snippet_with_applicability ( cx, recvr. span , "_ " , & mut applicability) . to_string ( ) ,
3287
+ snippet_with_applicability ( cx, recvr. span , ".. " , & mut applicability) . to_string ( ) ,
3286
3288
applicability,
3287
3289
) ;
3288
3290
}
0 commit comments