rust/clippy_lints/src/methods/option_map_unwrap_or.rs

131 lines
4.3 KiB
Rust
Raw Normal View History

use crate::utils::{differing_macro_contexts, paths, snippet_with_applicability, span_lint_and_then};
use crate::utils::{is_copy, match_type};
2019-01-21 11:29:35 -06:00
use rustc::hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
use rustc::hir::{self, *};
use rustc::lint::LateContext;
2019-01-21 11:29:35 -06:00
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use syntax::source_map::Span;
2019-05-17 16:53:54 -05:00
use syntax_pos::symbol::Symbol;
use super::OPTION_MAP_UNWRAP_OR;
/// lint use of `map().unwrap_or()` for `Option`s
2019-01-21 11:29:35 -06:00
pub(super) fn lint<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>,
2019-12-27 01:12:26 -06:00
expr: &hir::Expr<'_>,
map_args: &'tcx [hir::Expr<'_>],
unwrap_args: &'tcx [hir::Expr<'_>],
map_span: Span,
2019-01-21 11:29:35 -06:00
) {
// lint if the caller of `map()` is an `Option`
2019-05-17 16:53:54 -05:00
if match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::OPTION) {
2019-01-21 11:29:35 -06:00
if !is_copy(cx, cx.tables.expr_ty(&unwrap_args[1])) {
// Do not lint if the `map` argument uses identifiers in the `map`
// argument that are also used in the `unwrap_or` argument
let mut unwrap_visitor = UnwrapVisitor {
cx,
identifiers: FxHashSet::default(),
};
unwrap_visitor.visit_expr(&unwrap_args[1]);
let mut map_expr_visitor = MapExprVisitor {
cx,
identifiers: unwrap_visitor.identifiers,
found_identifier: false,
};
map_expr_visitor.visit_expr(&map_args[1]);
if map_expr_visitor.found_identifier {
return;
}
}
if differing_macro_contexts(unwrap_args[1].span, map_span) {
return;
}
let mut applicability = Applicability::MachineApplicable;
// get snippet for unwrap_or()
let unwrap_snippet = snippet_with_applicability(cx, unwrap_args[1].span, "..", &mut applicability);
// lint message
// comparing the snippet from source to raw text ("None") below is safe
// because we already have checked the type.
let arg = if unwrap_snippet == "None" { "None" } else { "a" };
let unwrap_snippet_none = unwrap_snippet == "None";
let suggest = if unwrap_snippet_none {
"and_then(f)"
} else {
"map_or(a, f)"
};
let msg = &format!(
"called `map(f).unwrap_or({})` on an Option value. \
This can be done more directly by calling `{}` instead",
arg, suggest
);
span_lint_and_then(cx, OPTION_MAP_UNWRAP_OR, expr.span, msg, |db| {
let map_arg_span = map_args[1].span;
let mut suggestion = vec![
(
map_span,
String::from(if unwrap_snippet_none { "and_then" } else { "map_or" }),
),
(expr.span.with_lo(unwrap_args[0].span.hi()), String::from("")),
];
if !unwrap_snippet_none {
suggestion.push((map_arg_span.with_hi(map_arg_span.lo()), format!("{}, ", unwrap_snippet)));
}
db.multipart_suggestion(&format!("use `{}` instead", suggest), suggestion, applicability);
});
}
}
2019-01-21 11:29:35 -06:00
struct UnwrapVisitor<'a, 'tcx> {
2019-01-21 11:29:35 -06:00
cx: &'a LateContext<'a, 'tcx>,
2019-02-05 09:59:23 -06:00
identifiers: FxHashSet<Symbol>,
2019-01-21 11:29:35 -06:00
}
impl<'a, 'tcx> Visitor<'tcx> for UnwrapVisitor<'a, 'tcx> {
2019-12-29 22:02:10 -06:00
fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) {
2019-02-05 09:59:23 -06:00
self.identifiers.insert(ident(path));
2019-01-21 11:29:35 -06:00
walk_path(self, path);
}
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
NestedVisitorMap::All(&self.cx.tcx.hir())
}
}
struct MapExprVisitor<'a, 'tcx> {
2019-01-21 11:29:35 -06:00
cx: &'a LateContext<'a, 'tcx>,
2019-02-05 09:59:23 -06:00
identifiers: FxHashSet<Symbol>,
2019-01-21 11:29:35 -06:00
found_identifier: bool,
}
impl<'a, 'tcx> Visitor<'tcx> for MapExprVisitor<'a, 'tcx> {
2019-12-29 22:02:10 -06:00
fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) {
2019-02-05 09:59:23 -06:00
if self.identifiers.contains(&ident(path)) {
self.found_identifier = true;
return;
2019-01-21 11:29:35 -06:00
}
walk_path(self, path);
}
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
NestedVisitorMap::All(&self.cx.tcx.hir())
}
}
2019-02-05 09:59:23 -06:00
2019-12-29 22:02:10 -06:00
fn ident(path: &Path<'_>) -> Symbol {
2019-02-05 09:59:23 -06:00
path.segments
.last()
.expect("segments should be composed of at least 1 element")
.ident
.name
}