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