|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet; |
| 3 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 4 | +use clippy_utils::{get_parent_expr, match_def_path, paths, SpanlessEq}; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir as hir; |
| 7 | +use rustc_hir::def_id::DefId; |
| 8 | +use rustc_hir::ExprKind::Assign; |
| 9 | +use rustc_lint::{LateContext, LateLintPass}; |
| 10 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 11 | +use rustc_span::symbol::sym; |
| 12 | + |
| 13 | +const ACCEPTABLE_METHODS: [&[&str]; 4] = [ |
| 14 | + &paths::HASHSET_ITER, |
| 15 | + &paths::BTREESET_ITER, |
| 16 | + &paths::SLICE_INTO, |
| 17 | + &paths::VEC_DEQUE_ITER, |
| 18 | +]; |
| 19 | +const ACCEPTABLE_TYPES: [rustc_span::Symbol; 6] = [ |
| 20 | + sym::BTreeSet, |
| 21 | + sym::BTreeMap, |
| 22 | + sym::HashSet, |
| 23 | + sym::HashMap, |
| 24 | + sym::Vec, |
| 25 | + sym::VecDeque, |
| 26 | +]; |
| 27 | + |
| 28 | +declare_clippy_lint! { |
| 29 | + /// ### What it does |
| 30 | + /// Checks for code to be replaced by `.retain()`. |
| 31 | + /// ### Why is this bad? |
| 32 | + /// `.retain()` is simpler. |
| 33 | + /// ### Example |
| 34 | + /// ```rust |
| 35 | + /// let mut vec = vec![0, 1, 2]; |
| 36 | + /// vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect(); |
| 37 | + /// vec = vec.into_iter().filter(|x| x % 2 == 0).collect(); |
| 38 | + /// ``` |
| 39 | + /// Use instead: |
| 40 | + /// ```rust |
| 41 | + /// let mut vec = vec![0, 1, 2]; |
| 42 | + /// vec.retain(|x| x % 2 == 0); |
| 43 | + /// ``` |
| 44 | + #[clippy::version = "1.63.0"] |
| 45 | + pub USE_RETAIN, |
| 46 | + style, |
| 47 | + "`retain()` is simpler and the same functionalitys" |
| 48 | +} |
| 49 | +declare_lint_pass!(UseRetain => [USE_RETAIN]); |
| 50 | + |
| 51 | +impl<'tcx> LateLintPass<'tcx> for UseRetain { |
| 52 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { |
| 53 | + if_chain! { |
| 54 | + if let Some(parent_expr) = get_parent_expr(cx, expr); |
| 55 | + if let Assign(left_expr, collect_expr, _) = &parent_expr.kind; |
| 56 | + if let hir::ExprKind::MethodCall(seg, _, _) = &collect_expr.kind; |
| 57 | + if seg.args.is_none(); |
| 58 | + |
| 59 | + if let hir::ExprKind::MethodCall(_, [target_expr], _) = &collect_expr.kind; |
| 60 | + if let Some(collect_def_id) = cx.typeck_results().type_dependent_def_id(collect_expr.hir_id); |
| 61 | + if match_def_path(cx, collect_def_id, &paths::CORE_ITER_COLLECT); |
| 62 | + |
| 63 | + then { |
| 64 | + check_into_iter(cx, parent_expr, left_expr, target_expr); |
| 65 | + check_iter(cx, parent_expr, left_expr, target_expr); |
| 66 | + check_to_owned(cx, parent_expr, left_expr, target_expr); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +fn check_into_iter( |
| 73 | + cx: &LateContext<'_>, |
| 74 | + parent_expr: &hir::Expr<'_>, |
| 75 | + left_expr: &hir::Expr<'_>, |
| 76 | + target_expr: &hir::Expr<'_>, |
| 77 | +) { |
| 78 | + if_chain! { |
| 79 | + if let hir::ExprKind::MethodCall(_, [into_iter_expr, _], _) = &target_expr.kind; |
| 80 | + if let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id); |
| 81 | + if match_def_path(cx, filter_def_id, &paths::CORE_ITER_FILTER); |
| 82 | + |
| 83 | + if let hir::ExprKind::MethodCall(_, [struct_expr], _) = &into_iter_expr.kind; |
| 84 | + if let Some(into_iter_def_id) = cx.typeck_results().type_dependent_def_id(into_iter_expr.hir_id); |
| 85 | + if match_def_path(cx, into_iter_def_id, &paths::CORE_ITER_INTO_ITER); |
| 86 | + if match_acceptable_type(cx, left_expr); |
| 87 | + |
| 88 | + if SpanlessEq::new(cx).eq_expr(left_expr, struct_expr); |
| 89 | + |
| 90 | + then { |
| 91 | + suggest(cx, parent_expr, left_expr, target_expr); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +fn check_iter( |
| 97 | + cx: &LateContext<'_>, |
| 98 | + parent_expr: &hir::Expr<'_>, |
| 99 | + left_expr: &hir::Expr<'_>, |
| 100 | + target_expr: &hir::Expr<'_>, |
| 101 | +) { |
| 102 | + if_chain! { |
| 103 | + if let hir::ExprKind::MethodCall(_, [filter_expr], _) = &target_expr.kind; |
| 104 | + if let Some(copied_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id); |
| 105 | + if match_def_path(cx, copied_def_id, &paths::CORE_ITER_COPIED); |
| 106 | + |
| 107 | + if let hir::ExprKind::MethodCall(_, [iter_expr, _], _) = &filter_expr.kind; |
| 108 | + if let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(filter_expr.hir_id); |
| 109 | + if match_def_path(cx, filter_def_id, &paths::CORE_ITER_FILTER); |
| 110 | + |
| 111 | + if let hir::ExprKind::MethodCall(_, [struct_expr], _) = &iter_expr.kind; |
| 112 | + if let Some(iter_expr_def_id) = cx.typeck_results().type_dependent_def_id(iter_expr.hir_id); |
| 113 | + if match_acceptable_def_path(cx, iter_expr_def_id); |
| 114 | + if match_acceptable_type(cx, left_expr); |
| 115 | + if SpanlessEq::new(cx).eq_expr(left_expr, struct_expr); |
| 116 | + |
| 117 | + then { |
| 118 | + suggest(cx, parent_expr, left_expr, filter_expr); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +fn check_to_owned( |
| 124 | + cx: &LateContext<'_>, |
| 125 | + parent_expr: &hir::Expr<'_>, |
| 126 | + left_expr: &hir::Expr<'_>, |
| 127 | + target_expr: &hir::Expr<'_>, |
| 128 | +) { |
| 129 | + if_chain! { |
| 130 | + if let hir::ExprKind::MethodCall(_, [filter_expr], _) = &target_expr.kind; |
| 131 | + if let Some(to_owned_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id); |
| 132 | + if match_def_path(cx, to_owned_def_id, &paths::TO_OWNED_METHOD); |
| 133 | + |
| 134 | + if let hir::ExprKind::MethodCall(_, [chars_expr, _], _) = &filter_expr.kind; |
| 135 | + if let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(filter_expr.hir_id); |
| 136 | + if match_def_path(cx, filter_def_id, &paths::CORE_ITER_FILTER); |
| 137 | + |
| 138 | + if let hir::ExprKind::MethodCall(_, [str_expr], _) = &chars_expr.kind; |
| 139 | + if let Some(chars_expr_def_id) = cx.typeck_results().type_dependent_def_id(chars_expr.hir_id); |
| 140 | + if match_def_path(cx, chars_expr_def_id, &paths::STR_CHARS); |
| 141 | + |
| 142 | + let ty = cx.typeck_results().expr_ty(str_expr).peel_refs(); |
| 143 | + if is_type_diagnostic_item(cx, ty, sym::String); |
| 144 | + if SpanlessEq::new(cx).eq_expr(left_expr, str_expr); |
| 145 | + |
| 146 | + then { |
| 147 | + suggest(cx, parent_expr, left_expr, filter_expr); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +fn suggest(cx: &LateContext<'_>, parent_expr: &hir::Expr<'_>, left_expr: &hir::Expr<'_>, filter_expr: &hir::Expr<'_>) { |
| 153 | + if_chain! { |
| 154 | + if let hir::ExprKind::MethodCall(_, [_, closure], _) = filter_expr.kind; |
| 155 | + if let hir::ExprKind::Closure(_, _, filter_body_id, ..) = closure.kind; |
| 156 | + let filter_body = cx.tcx.hir().body(filter_body_id); |
| 157 | + if let [filter_params] = filter_body.params; |
| 158 | + then { |
| 159 | + if let Some(sugg) = match filter_params.pat.kind { |
| 160 | + hir::PatKind::Binding(_, _, filter_param_ident, None) => { |
| 161 | + Some(format!("{}.retain(|{}| {})", snippet(cx, left_expr.span, ".."), filter_param_ident, snippet(cx, filter_body.value.span, ".."))) |
| 162 | + }, |
| 163 | + hir::PatKind::Tuple([key_pat, value_pat], _) => { |
| 164 | + make_sugg(cx, key_pat, value_pat, left_expr, filter_body) |
| 165 | + }, |
| 166 | + hir::PatKind::Ref(pat, _) => { |
| 167 | + match pat.kind { |
| 168 | + hir::PatKind::Binding(_, _, filter_param_ident, None) => { |
| 169 | + Some(format!("{}.retain(|{}| {})", snippet(cx, left_expr.span, ".."), filter_param_ident, snippet(cx, filter_body.value.span, ".."))) |
| 170 | + }, |
| 171 | + _ => None |
| 172 | + } |
| 173 | + }, |
| 174 | + _ => None |
| 175 | + } { |
| 176 | + span_lint_and_sugg( |
| 177 | + cx, |
| 178 | + USE_RETAIN, |
| 179 | + parent_expr.span, |
| 180 | + "this expression can be written more simply using `.retain()`", |
| 181 | + "consider calling `.retain()` instead", |
| 182 | + sugg, |
| 183 | + Applicability::MachineApplicable |
| 184 | + ); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +fn make_sugg( |
| 191 | + cx: &LateContext<'_>, |
| 192 | + key_pat: &rustc_hir::Pat<'_>, |
| 193 | + value_pat: &rustc_hir::Pat<'_>, |
| 194 | + left_expr: &hir::Expr<'_>, |
| 195 | + filter_body: &hir::Body<'_>, |
| 196 | +) -> Option<String> { |
| 197 | + match (&key_pat.kind, &value_pat.kind) { |
| 198 | + (hir::PatKind::Binding(_, _, key_param_ident, None), hir::PatKind::Binding(_, _, value_param_ident, None)) => { |
| 199 | + Some(format!( |
| 200 | + "{}.retain(|{}, &mut {}| {})", |
| 201 | + snippet(cx, left_expr.span, ".."), |
| 202 | + key_param_ident, |
| 203 | + value_param_ident, |
| 204 | + snippet(cx, filter_body.value.span, "..") |
| 205 | + )) |
| 206 | + }, |
| 207 | + (hir::PatKind::Binding(_, _, key_param_ident, None), hir::PatKind::Wild) => Some(format!( |
| 208 | + "{}.retain(|{}, _| {})", |
| 209 | + snippet(cx, left_expr.span, ".."), |
| 210 | + key_param_ident, |
| 211 | + snippet(cx, filter_body.value.span, "..") |
| 212 | + )), |
| 213 | + (hir::PatKind::Wild, hir::PatKind::Binding(_, _, value_param_ident, None)) => Some(format!( |
| 214 | + "{}.retain(|_, &mut {}| {})", |
| 215 | + snippet(cx, left_expr.span, ".."), |
| 216 | + value_param_ident, |
| 217 | + snippet(cx, filter_body.value.span, "..") |
| 218 | + )), |
| 219 | + _ => None, |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +fn match_acceptable_def_path(cx: &LateContext<'_>, collect_def_id: DefId) -> bool { |
| 224 | + return ACCEPTABLE_METHODS |
| 225 | + .iter() |
| 226 | + .any(|&method| match_def_path(cx, collect_def_id, method)); |
| 227 | +} |
| 228 | + |
| 229 | +fn match_acceptable_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool { |
| 230 | + let expr_ty = cx.typeck_results().expr_ty(expr).peel_refs(); |
| 231 | + return ACCEPTABLE_TYPES |
| 232 | + .iter() |
| 233 | + .any(|&ty| is_type_diagnostic_item(cx, expr_ty, ty)); |
| 234 | +} |
0 commit comments