@@ -34,11 +34,12 @@ use crate::utils::usage::mutated_variables;
34
34
use crate :: utils:: {
35
35
contains_return, contains_ty, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
36
36
in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path,
37
- match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args,
38
- path_to_local_id , paths , remove_blocks, return_ty, single_segment_path, snippet, snippet_block,
39
- snippet_with_applicability , snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg,
40
- span_lint_and_then , strip_pat_refs , sugg, walk_ptrs_ty_depth, SpanlessEq ,
37
+ match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id , paths ,
38
+ remove_blocks, return_ty, single_segment_path, snippet, snippet_block, snippet_with_applicability ,
39
+ snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then , strip_pat_refs ,
40
+ sugg, walk_ptrs_ty_depth, SpanlessEq ,
41
41
} ;
42
+ use clippy_utils:: paths:: { OPTION_IS_SOME , OPTION_UNWRAP } ;
42
43
43
44
declare_clippy_lint ! {
44
45
/// **What it does:** Checks for `.unwrap()` calls on `Option`s and on `Result`s.
@@ -3213,17 +3214,12 @@ fn is_method<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, method_path: &[
3213
3214
}
3214
3215
}
3215
3216
3216
- // these two paths can't be checked and thus aren't in `paths`
3217
- const OPTION_IS_SOME : [ & str ; 4 ] = [ "core" , "option" , "Option" , "is_some" ] ;
3218
- const OPTION_UNWRAP : [ & str ; 4 ] = [ "core" , "option" , "Option" , "unwrap" ] ;
3219
-
3220
3217
fn is_option_filter_map < ' tcx > (
3221
3218
cx : & LateContext < ' tcx > ,
3222
3219
filter_arg : & ' tcx hir:: Expr < ' _ > ,
3223
3220
map_arg : & ' tcx hir:: Expr < ' _ > ,
3224
3221
) -> bool {
3225
- is_method ( cx, map_arg, & OPTION_UNWRAP , sym ! ( unwrap) ) &&
3226
- is_method ( cx, filter_arg, & OPTION_IS_SOME , sym ! ( is_some) )
3222
+ is_method ( cx, map_arg, & OPTION_UNWRAP , sym ! ( unwrap) ) && is_method ( cx, filter_arg, & OPTION_IS_SOME , sym ! ( is_some) )
3227
3223
}
3228
3224
3229
3225
/// lint use of `filter().map()` for `Iterators`
@@ -3238,28 +3234,22 @@ fn lint_filter_some_map_unwrap<'tcx>(
3238
3234
let iterator = match_trait_method ( cx, expr, & paths:: ITERATOR ) ;
3239
3235
let iterator_or_option =
3240
3236
iterator || is_type_diagnostic_item ( cx, cx. typeck_results ( ) . expr_ty ( & filter_recv) , sym:: option_type) ;
3241
- if iterator_or_option {
3242
- if is_option_filter_map ( cx, filter_arg, map_arg) {
3243
- let msg = "`filter` for `Some` followed by `unwrap`" ;
3244
- let help = "consider using `flatten` instead" ;
3245
- let sugg = format ! (
3246
- "{}.flatten()" ,
3247
- snippet_block( cx, filter_recv. span, ".." , Some ( target_span) )
3248
- ) ;
3249
- span_lint_and_sugg (
3250
- cx,
3251
- OPTION_FILTER_MAP ,
3252
- expr. span . with_hi ( expr. span . hi ( ) ) ,
3253
- msg,
3254
- help,
3255
- sugg,
3256
- Applicability :: MachineApplicable ,
3257
- ) ;
3258
- } else if iterator {
3259
- let msg = "called `filter(..).map(..)` on an `Iterator`" ;
3260
- let hint = "this is more succinctly expressed by calling `.filter_map(..)` instead" ;
3261
- span_lint_and_help ( cx, FILTER_MAP , expr. span , msg, None , hint) ;
3262
- }
3237
+ if iterator_or_option && is_option_filter_map ( cx, filter_arg, map_arg) {
3238
+ let msg = "`filter` for `Some` followed by `unwrap`" ;
3239
+ let help = "consider using `flatten` instead" ;
3240
+ let sugg = format ! (
3241
+ "{}.flatten()" ,
3242
+ snippet_block( cx, filter_recv. span, ".." , Some ( target_span) )
3243
+ ) ;
3244
+ span_lint_and_sugg (
3245
+ cx,
3246
+ OPTION_FILTER_MAP ,
3247
+ expr. span . with_hi ( expr. span . hi ( ) ) ,
3248
+ msg,
3249
+ help,
3250
+ sugg,
3251
+ Applicability :: MachineApplicable ,
3252
+ ) ;
3263
3253
}
3264
3254
}
3265
3255
0 commit comments