1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::ty::is_type_lang_item;
3 use clippy_utils::SpanlessEq;
4 use if_chain::if_chain;
5 use rustc_ast::LitKind;
6 use rustc_hir::{ExprKind, LangItem};
7 use rustc_lint::LateContext;
9 use super::NO_EFFECT_REPLACE;
11 pub(super) fn check<'tcx>(
12 cx: &LateContext<'tcx>,
13 expr: &'tcx rustc_hir::Expr<'_>,
14 arg1: &'tcx rustc_hir::Expr<'_>,
15 arg2: &'tcx rustc_hir::Expr<'_>,
17 let ty = cx.typeck_results().expr_ty(expr).peel_refs();
18 if !(ty.is_str() || is_type_lang_item(cx, ty, LangItem::String)) {
23 if let ExprKind::Lit(spanned) = &arg1.kind;
24 if let Some(param1) = lit_string_value(&spanned.node);
26 if let ExprKind::Lit(spanned) = &arg2.kind;
27 if let LitKind::Str(param2, _) = &spanned.node;
28 if param1 == param2.as_str();
31 span_lint(cx, NO_EFFECT_REPLACE, expr.span, "replacing text with itself");
35 if SpanlessEq::new(cx).eq_expr(arg1, arg2) {
36 span_lint(cx, NO_EFFECT_REPLACE, expr.span, "replacing text with itself");
40 fn lit_string_value(node: &LitKind) -> Option<String> {
42 LitKind::Char(value) => Some(value.to_string()),
43 LitKind::Str(value, _) => Some(value.as_str().to_owned()),