@@ -2484,7 +2484,7 @@ fn lint_ok_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, ok_args: &[hir
24842484 }
24852485}
24862486
2487- /// lint use of `map().flatten()` for `Iterators`
2487+ /// lint use of `map().flatten()` for `Iterators` and 'Options'
24882488fn lint_map_flatten < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , expr : & ' tcx hir:: Expr < ' _ > , map_args : & ' tcx [ hir:: Expr < ' _ > ] ) {
24892489 // lint if caller of `.map().flatten()` is an Iterator
24902490 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<
25032503 Applicability :: MachineApplicable ,
25042504 ) ;
25052505 }
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+ }
25062524}
25072525
25082526/// lint use of `map().unwrap_or_else()` for `Option`s and `Result`s
0 commit comments