@@ -2484,7 +2484,7 @@ fn lint_ok_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, ok_args: &[hir
2484
2484
}
2485
2485
}
2486
2486
2487
- /// lint use of `map().flatten()` for `Iterators`
2487
+ /// lint use of `map().flatten()` for `Iterators` and 'Options'
2488
2488
fn lint_map_flatten < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , expr : & ' tcx hir:: Expr < ' _ > , map_args : & ' tcx [ hir:: Expr < ' _ > ] ) {
2489
2489
// lint if caller of `.map().flatten()` is an Iterator
2490
2490
if match_trait_method ( cx, expr, & paths:: ITERATOR ) {
@@ -2503,6 +2503,24 @@ fn lint_map_flatten<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<
2503
2503
Applicability :: MachineApplicable ,
2504
2504
) ;
2505
2505
}
2506
+
2507
+ // lint if caller of `.map().flatten()` is an Option
2508
+ if match_type ( cx, cx. tables . expr_ty ( & map_args[ 0 ] ) , & paths:: OPTION ) {
2509
+ let msg = "called `map(..).flatten()` on an `Option`. \
2510
+ This is more succinctly expressed by calling `.and_then(..)`";
2511
+ let self_snippet = snippet ( cx, map_args[ 0 ] . span , ".." ) ;
2512
+ let func_snippet = snippet ( cx, map_args[ 1 ] . span , ".." ) ;
2513
+ let hint = format ! ( "{0}.and_then({1})" , self_snippet, func_snippet) ;
2514
+ span_lint_and_sugg (
2515
+ cx,
2516
+ MAP_FLATTEN ,
2517
+ expr. span ,
2518
+ msg,
2519
+ "try using `and_then` instead" ,
2520
+ hint,
2521
+ Applicability :: MachineApplicable ,
2522
+ ) ;
2523
+ }
2506
2524
}
2507
2525
2508
2526
/// lint use of `map().unwrap_or_else()` for `Option`s and `Result`s
0 commit comments