|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 3 | +use clippy_utils::SpanlessEq; |
| 4 | +use if_chain::if_chain; |
| 5 | +use rustc_ast::LitKind; |
| 6 | +use rustc_hir::{Expr, ExprKind}; |
| 7 | +use rustc_lint::{LateContext, LateLintPass}; |
| 8 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 9 | +use rustc_span::sym; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// ### What it does |
| 13 | + /// Checks for `replace` statements which have no effect. |
| 14 | + /// |
| 15 | + /// ### Why is this bad? |
| 16 | + /// It's either a mistake or confusing. |
| 17 | + /// |
| 18 | + /// ### Example |
| 19 | + /// ```rust |
| 20 | + /// "1234".replace("12", "12"); |
| 21 | + /// "1234".replacen("12", "12", 1); |
| 22 | + /// ``` |
| 23 | + #[clippy::version = "1.62.0"] |
| 24 | + pub NO_EFFECT_REPLACE, |
| 25 | + suspicious, |
| 26 | + "replace with no effect" |
| 27 | +} |
| 28 | +declare_lint_pass!(NoEffectReplace => [NO_EFFECT_REPLACE]); |
| 29 | + |
| 30 | +impl LateLintPass<'_> for NoEffectReplace { |
| 31 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) { |
| 32 | + let (path, args) = match expr.kind { |
| 33 | + ExprKind::MethodCall(path, args, _) => (path, args), |
| 34 | + _ => return, |
| 35 | + }; |
| 36 | + |
| 37 | + if !(args.len() == 3 || args.len() == 4) |
| 38 | + || !(path.ident.name.as_str() == "replace" || path.ident.name.as_str() == "replacen") |
| 39 | + { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + let ty = cx.typeck_results().expr_ty(expr).peel_refs(); |
| 44 | + if !(ty.is_str() || is_type_diagnostic_item(cx, ty, sym::String)) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if_chain! { |
| 49 | + if let ExprKind::Lit(spanned) = &args[1].kind; |
| 50 | + if let Some(param1) = lit_string_value(&spanned.node); |
| 51 | + |
| 52 | + if let ExprKind::Lit(spanned) = &args[2].kind; |
| 53 | + if let LitKind::Str(param2, _) = &spanned.node; |
| 54 | + if param1 == param2.as_str(); |
| 55 | + |
| 56 | + then { |
| 57 | + span_lint( |
| 58 | + cx, |
| 59 | + NO_EFFECT_REPLACE, |
| 60 | + expr.span, |
| 61 | + "you are using replace with same element", |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if_chain! { |
| 67 | + if let ExprKind::MethodCall(path, args, _) = expr.kind; |
| 68 | + if (args.len() == 3 && path.ident.name.as_str() == "replace") || (args.len() == 4 && path.ident.name.as_str() == "replacen"); |
| 69 | + if SpanlessEq::new(cx).eq_expr(&args[1], &args[2]); |
| 70 | + |
| 71 | + then { |
| 72 | + span_lint( |
| 73 | + cx, |
| 74 | + NO_EFFECT_REPLACE, |
| 75 | + expr.span, |
| 76 | + "you are using replace with same element", |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +fn lit_string_value(node: &LitKind) -> Option<String> { |
| 84 | + match node { |
| 85 | + LitKind::Char(value) => Some(value.to_string()), |
| 86 | + LitKind::Str(value, _) => Some(value.as_str().to_owned()), |
| 87 | + _ => None, |
| 88 | + } |
| 89 | +} |
0 commit comments