|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +// use clippy_utils::source::snippet_with_context; |
| 3 | +use clippy_utils::visitors::for_each_expr; |
| 4 | +use core::ops::ControlFlow; |
| 5 | +use if_chain::if_chain; |
| 6 | +use rustc_ast::ast::LitKind; |
| 7 | +use rustc_errors::Applicability; |
1 | 8 | use rustc_hir as hir;
|
2 | 9 | use rustc_hir::*;
|
3 |
| -use rustc_lint::{LateContext, LateLintPass}; |
4 |
| -use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 10 | +use rustc_lint::LateContext; |
| 11 | +use rustc_middle::ty; |
| 12 | +use rustc_span::source_map::Spanned; |
| 13 | +use std::unreachable; |
| 14 | +// use rustc_span::Span; |
5 | 15 |
|
6 |
| -pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) {} |
| 16 | +use super::method_call; |
| 17 | +use super::COLLAPSIBLE_STR_REPLACE; |
| 18 | + |
| 19 | +pub(super) fn check<'tcx>( |
| 20 | + cx: &LateContext<'tcx>, |
| 21 | + expr: &'tcx hir::Expr<'tcx>, |
| 22 | + name: &str, |
| 23 | + recv: &'tcx hir::Expr<'tcx>, |
| 24 | + args: &'tcx [hir::Expr<'tcx>], |
| 25 | +) { |
| 26 | + match (name, args) { |
| 27 | + ("replace", [from, to]) => { |
| 28 | + // Check for `str::replace` calls with char slice for linting |
| 29 | + let original_recv = find_original_recv(recv); |
| 30 | + let original_recv_ty = cx.typeck_results().expr_ty(original_recv).peel_refs(); |
| 31 | + if_chain! { |
| 32 | + // Check the receiver of the method call is `str` type |
| 33 | + if matches!(original_recv_ty.kind(), ty::Str); |
| 34 | + let from_ty = cx.typeck_results().expr_ty(from).peel_refs(); |
| 35 | + if let ty::Array(array_ty, _) = from_ty.kind(); |
| 36 | + if matches!(array_ty.kind(), ty::Char); |
| 37 | + then { |
| 38 | + check_replace_call_with_char_slice(cx, from, to); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + match method_call(recv) { |
| 43 | + // Check if there's an earlier `str::replace` call |
| 44 | + Some(("replace", [prev_recv, prev_from, prev_to], prev_span)) => { |
| 45 | + println!("Consecutive replace calls"); |
| 46 | + // Check that the original receiver is of `ty::Str` type |
| 47 | + // Check that all the `from` args are char literals |
| 48 | + // Check that all the `to` args are the same variable or has the same &str value |
| 49 | + // If so, then lint |
| 50 | + }, |
| 51 | + _ => {}, |
| 52 | + } |
| 53 | + }, |
| 54 | + _ => {}, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +fn find_original_recv<'tcx>(recv: &'tcx hir::Expr<'tcx>) -> &'tcx hir::Expr<'tcx> { |
| 59 | + let mut original_recv = recv; |
| 60 | + |
| 61 | + let _: Option<()> = for_each_expr(recv, |e| { |
| 62 | + if let Some((name, [prev_recv, args @ ..], _)) = method_call(e) { |
| 63 | + match (name, args) { |
| 64 | + ("replace", [_, _]) => { |
| 65 | + original_recv = prev_recv; |
| 66 | + ControlFlow::Continue(()) |
| 67 | + }, |
| 68 | + _ => ControlFlow::BREAK, |
| 69 | + } |
| 70 | + } else { |
| 71 | + ControlFlow::Continue(()) |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + original_recv |
| 76 | +} |
| 77 | + |
| 78 | +fn check_replace_call_with_char_slice<'tcx>( |
| 79 | + cx: &LateContext<'tcx>, |
| 80 | + from_arg: &'tcx hir::Expr<'tcx>, |
| 81 | + to_arg: &'tcx hir::Expr<'tcx>, |
| 82 | +) { |
| 83 | + let mut has_no_var = true; |
| 84 | + let mut char_list: Vec<char> = Vec::new(); |
| 85 | + // Go through the `from_arg` to collect all char literals |
| 86 | + let _: Option<()> = for_each_expr(from_arg, |e| { |
| 87 | + if let ExprKind::Lit(Spanned { |
| 88 | + node: LitKind::Char(val), |
| 89 | + .. |
| 90 | + }) = e.kind |
| 91 | + { |
| 92 | + char_list.push(val); |
| 93 | + ControlFlow::Continue(()) |
| 94 | + } else if let ExprKind::Path(..) = e.kind { |
| 95 | + // If a variable is found in the char slice, no lint for first version of this lint |
| 96 | + has_no_var = false; |
| 97 | + ControlFlow::BREAK |
| 98 | + } else { |
| 99 | + ControlFlow::Continue(()) |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + if has_no_var { |
| 104 | + let to_arg_repr = match to_arg.kind { |
| 105 | + ExprKind::Lit(Spanned { |
| 106 | + node: LitKind::Str(to_arg_val, _), |
| 107 | + .. |
| 108 | + }) => { |
| 109 | + let repr = to_arg_val.as_str(); |
| 110 | + let double_quote = "\""; |
| 111 | + double_quote.to_owned() + repr + double_quote |
| 112 | + }, |
| 113 | + ExprKind::Path(QPath::Resolved( |
| 114 | + _, |
| 115 | + Path { |
| 116 | + segments: path_segments, |
| 117 | + .. |
| 118 | + }, |
| 119 | + )) => { |
| 120 | + // join the path_segments values by "::" |
| 121 | + let path_segment_ident_names: Vec<&str> = path_segments |
| 122 | + .iter() |
| 123 | + .map(|path_seg| path_seg.ident.name.as_str()) |
| 124 | + .collect(); |
| 125 | + |
| 126 | + path_segment_ident_names.join("::") |
| 127 | + }, |
| 128 | + _ => unreachable!(), |
| 129 | + }; |
| 130 | + |
| 131 | + let app = Applicability::MachineApplicable; |
| 132 | + span_lint_and_sugg( |
| 133 | + cx, |
| 134 | + COLLAPSIBLE_STR_REPLACE, |
| 135 | + from_arg.span, |
| 136 | + "used slice of chars in `str::replace` call", |
| 137 | + "replace with", |
| 138 | + format!( |
| 139 | + "replace(|c| matches!(c, {}), {})", |
| 140 | + format_slice_of_chars_for_sugg(&char_list), |
| 141 | + to_arg_repr, |
| 142 | + ), |
| 143 | + app, |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +fn format_slice_of_chars_for_sugg(chars: &Vec<char>) -> String { |
| 149 | + let single_quoted_chars: Vec<String> = chars |
| 150 | + .iter() |
| 151 | + .map(|c| "'".to_owned() + &c.to_string() + &"'".to_owned()) |
| 152 | + .collect(); |
| 153 | + single_quoted_chars.join(" | ") |
| 154 | +} |
0 commit comments