1 use crate::utils::{differing_macro_contexts, snippet_with_applicability, span_lint_and_then};
2 use crate::utils::{is_copy, is_type_diagnostic_item};
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_errors::Applicability;
5 use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
6 use rustc_hir::{self, HirId, Path};
7 use rustc_lint::LateContext;
8 use rustc_middle::hir::map::Map;
9 use rustc_span::source_map::Span;
10 use rustc_span::symbol::Symbol;
12 use super::MAP_UNWRAP_OR;
14 /// lint use of `map().unwrap_or()` for `Option`s
15 pub(super) fn lint<'tcx>(
16 cx: &LateContext<'tcx>,
17 expr: &rustc_hir::Expr<'_>,
18 map_args: &'tcx [rustc_hir::Expr<'_>],
19 unwrap_args: &'tcx [rustc_hir::Expr<'_>],
22 // lint if the caller of `map()` is an `Option`
23 if is_type_diagnostic_item(cx, cx.tables().expr_ty(&map_args[0]), sym!(option_type)) {
24 if !is_copy(cx, cx.tables().expr_ty(&unwrap_args[1])) {
25 // Do not lint if the `map` argument uses identifiers in the `map`
26 // argument that are also used in the `unwrap_or` argument
28 let mut unwrap_visitor = UnwrapVisitor {
30 identifiers: FxHashSet::default(),
32 unwrap_visitor.visit_expr(&unwrap_args[1]);
34 let mut map_expr_visitor = MapExprVisitor {
36 identifiers: unwrap_visitor.identifiers,
37 found_identifier: false,
39 map_expr_visitor.visit_expr(&map_args[1]);
41 if map_expr_visitor.found_identifier {
46 if differing_macro_contexts(unwrap_args[1].span, map_span) {
50 let mut applicability = Applicability::MachineApplicable;
51 // get snippet for unwrap_or()
52 let unwrap_snippet = snippet_with_applicability(cx, unwrap_args[1].span, "..", &mut applicability);
54 // comparing the snippet from source to raw text ("None") below is safe
55 // because we already have checked the type.
56 let arg = if unwrap_snippet == "None" { "None" } else { "a" };
57 let unwrap_snippet_none = unwrap_snippet == "None";
58 let suggest = if unwrap_snippet_none {
64 "called `map(f).unwrap_or({})` on an `Option` value. \
65 This can be done more directly by calling `{}` instead",
69 span_lint_and_then(cx, MAP_UNWRAP_OR, expr.span, msg, |diag| {
70 let map_arg_span = map_args[1].span;
72 let mut suggestion = vec![
75 String::from(if unwrap_snippet_none { "and_then" } else { "map_or" }),
77 (expr.span.with_lo(unwrap_args[0].span.hi()), String::from("")),
80 if !unwrap_snippet_none {
81 suggestion.push((map_arg_span.with_hi(map_arg_span.lo()), format!("{}, ", unwrap_snippet)));
84 diag.multipart_suggestion(&format!("use `{}` instead", suggest), suggestion, applicability);
89 struct UnwrapVisitor<'a, 'tcx> {
90 cx: &'a LateContext<'tcx>,
91 identifiers: FxHashSet<Symbol>,
94 impl<'a, 'tcx> Visitor<'tcx> for UnwrapVisitor<'a, 'tcx> {
97 fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) {
98 self.identifiers.insert(ident(path));
99 walk_path(self, path);
102 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
103 NestedVisitorMap::All(self.cx.tcx.hir())
107 struct MapExprVisitor<'a, 'tcx> {
108 cx: &'a LateContext<'tcx>,
109 identifiers: FxHashSet<Symbol>,
110 found_identifier: bool,
113 impl<'a, 'tcx> Visitor<'tcx> for MapExprVisitor<'a, 'tcx> {
114 type Map = Map<'tcx>;
116 fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) {
117 if self.identifiers.contains(&ident(path)) {
118 self.found_identifier = true;
121 walk_path(self, path);
124 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
125 NestedVisitorMap::All(self.cx.tcx.hir())
129 fn ident(path: &Path<'_>) -> Symbol {
132 .expect("segments should be composed of at least 1 element")