|
1 | 1 | use clippy_utils::{ |
2 | 2 | diagnostics::span_lint_and_sugg, |
3 | 3 | is_expr_final_block_expr, is_expr_used_or_unified, match_def_path, paths, peel_hir_expr_while, |
4 | | - source::{snippet_indent, snippet_with_applicability, snippet_with_context}, |
| 4 | + source::{reindent_multiline, snippet_indent, snippet_with_applicability, snippet_with_context}, |
5 | 5 | SpanlessEq, |
6 | 6 | }; |
7 | 7 | use rustc_errors::Applicability; |
@@ -75,7 +75,84 @@ impl<'tcx> LateLintPass<'tcx> for HashMapPass { |
75 | 75 | let mut app = Applicability::MachineApplicable; |
76 | 76 | let map_str = snippet_with_context(cx, contains_expr.map.span, contains_expr.call_ctxt, "..", &mut app).0; |
77 | 77 | let key_str = snippet_with_context(cx, contains_expr.key.span, contains_expr.call_ctxt, "..", &mut app).0; |
78 | | - let sugg = if !contains_expr.negated || else_expr.is_some() || then_search.insertions.is_empty() { |
| 78 | + let sugg = if let Some(else_expr) = else_expr { |
| 79 | + // if .. { .. } else { .. } |
| 80 | + let else_search = match find_insert_calls(cx, &contains_expr, else_expr) { |
| 81 | + Some(search) if !(then_search.insertions.is_empty() && search.insertions.is_empty()) => search, |
| 82 | + _ => return, |
| 83 | + }; |
| 84 | + |
| 85 | + if then_search.insertions.is_empty() || else_search.insertions.is_empty() { |
| 86 | + // if .. { insert } else { .. } or if .. { .. } else { then } of |
| 87 | + let (then_str, else_str, entry_kind) = if else_search.insertions.is_empty() { |
| 88 | + if contains_expr.negated { |
| 89 | + ( |
| 90 | + then_search.snippet_vacant(cx, then_expr.span, &mut app), |
| 91 | + snippet_with_applicability(cx, else_expr.span, "{ .. }", &mut app), |
| 92 | + "Vacant(e)", |
| 93 | + ) |
| 94 | + } else { |
| 95 | + ( |
| 96 | + then_search.snippet_occupied(cx, then_expr.span, &mut app), |
| 97 | + snippet_with_applicability(cx, else_expr.span, "{ .. }", &mut app), |
| 98 | + "Occupied(mut e)", |
| 99 | + ) |
| 100 | + } |
| 101 | + } else if contains_expr.negated { |
| 102 | + ( |
| 103 | + else_search.snippet_occupied(cx, else_expr.span, &mut app), |
| 104 | + snippet_with_applicability(cx, then_expr.span, "{ .. }", &mut app), |
| 105 | + "Occupied(mut e)", |
| 106 | + ) |
| 107 | + } else { |
| 108 | + ( |
| 109 | + else_search.snippet_vacant(cx, else_expr.span, &mut app), |
| 110 | + snippet_with_applicability(cx, then_expr.span, "{ .. }", &mut app), |
| 111 | + "Vacant(e)", |
| 112 | + ) |
| 113 | + }; |
| 114 | + format!( |
| 115 | + "if let {}::{} = {}.entry({}) {} else {}", |
| 116 | + map_ty.entry_path(), |
| 117 | + entry_kind, |
| 118 | + map_str, |
| 119 | + key_str, |
| 120 | + then_str, |
| 121 | + else_str, |
| 122 | + ) |
| 123 | + } else { |
| 124 | + // if .. { insert } else { insert } |
| 125 | + let (then_str, else_str, then_entry, else_entry) = if contains_expr.negated { |
| 126 | + ( |
| 127 | + then_search.snippet_vacant(cx, then_expr.span, &mut app), |
| 128 | + else_search.snippet_occupied(cx, else_expr.span, &mut app), |
| 129 | + "Vacant(e)", |
| 130 | + "Occupied(mut e)", |
| 131 | + ) |
| 132 | + } else { |
| 133 | + ( |
| 134 | + then_search.snippet_occupied(cx, then_expr.span, &mut app), |
| 135 | + else_search.snippet_vacant(cx, else_expr.span, &mut app), |
| 136 | + "Occupied(mut e)", |
| 137 | + "Vacant(e)", |
| 138 | + ) |
| 139 | + }; |
| 140 | + let indent_str = snippet_indent(cx, expr.span); |
| 141 | + let indent_str = indent_str.as_deref().unwrap_or(""); |
| 142 | + format!( |
| 143 | + "match {}.entry({}) {{\n{indent} {entry}::{} => {}\n\ |
| 144 | + {indent} {entry}::{} => {}\n{indent}}}", |
| 145 | + map_str, |
| 146 | + key_str, |
| 147 | + then_entry, |
| 148 | + reindent_multiline(then_str.into(), true, Some(4 + indent_str.len())), |
| 149 | + else_entry, |
| 150 | + reindent_multiline(else_str.into(), true, Some(4 + indent_str.len())), |
| 151 | + entry = map_ty.entry_path(), |
| 152 | + indent = indent_str, |
| 153 | + ) |
| 154 | + } |
| 155 | + } else if then_search.insertions.is_empty() || !contains_expr.negated { |
79 | 156 | return; |
80 | 157 | } else { |
81 | 158 | // if .. { insert } |
|
0 commit comments