+use clippy_utils::higher;
use clippy_utils::{
can_move_expr_to_closure_no_visit,
diagnostics::span_lint_and_sugg,
source::{reindent_multiline, snippet_indent, snippet_with_applicability, snippet_with_context},
SpanlessEq,
};
+use core::fmt::Write;
use rustc_errors::Applicability;
use rustc_hir::{
intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor},
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{Span, SyntaxContext, DUMMY_SP};
-use std::fmt::Write;
declare_clippy_lint! {
- /// **What it does:** Checks for uses of `contains_key` + `insert` on `HashMap`
+ /// ### What it does
+ /// Checks for uses of `contains_key` + `insert` on `HashMap`
/// or `BTreeMap`.
///
- /// **Why is this bad?** Using `entry` is more efficient.
+ /// ### Why is this bad?
+ /// Using `entry` is more efficient.
///
- /// **Known problems:** The suggestion may have type inference errors in some cases. e.g.
+ /// ### Known problems
+ /// The suggestion may have type inference errors in some cases. e.g.
/// ```rust
/// let mut map = std::collections::HashMap::new();
/// let _ = if !map.contains_key(&0) {
/// };
/// ```
///
- /// **Example:**
+ /// ### Example
/// ```rust
/// # use std::collections::HashMap;
/// # let mut map = HashMap::new();
impl<'tcx> LateLintPass<'tcx> for HashMapPass {
#[allow(clippy::too_many_lines)]
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
- let (cond_expr, then_expr, else_expr) = match expr.kind {
- ExprKind::If(c, t, e) => (c, t, e),
+ let (cond_expr, then_expr, else_expr) = match higher::If::hir(expr) {
+ Some(higher::If { cond, then, r#else }) => (cond, then, r#else),
_ => return,
};
+
let (map_ty, contains_expr) = match try_parse_contains(cx, cond_expr) {
Some(x) => x,
None => return,