New lint no_effect_replace

Closes #1595

changelog: Add no_effect_replace lint.
This commit is contained in:
Federico Guerinoni 2022-04-25 16:12:48 +02:00
parent d422baa30c
commit ea62347a5a
8 changed files with 177 additions and 0 deletions

View file

@ -190,6 +190,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
LintId::of(methods::NEEDLESS_OPTION_TAKE),
LintId::of(methods::NEEDLESS_SPLITN),
LintId::of(methods::NEW_RET_NO_SELF),
LintId::of(methods::NO_EFFECT_REPLACE),
LintId::of(methods::OK_EXPECT),
LintId::of(methods::OPTION_AS_REF_DEREF),
LintId::of(methods::OPTION_FILTER_MAP),

View file

@ -331,6 +331,7 @@ store.register_lints(&[
methods::NEEDLESS_OPTION_TAKE,
methods::NEEDLESS_SPLITN,
methods::NEW_RET_NO_SELF,
methods::NO_EFFECT_REPLACE,
methods::OK_EXPECT,
methods::OPTION_AS_REF_DEREF,
methods::OPTION_FILTER_MAP,

View file

@ -23,6 +23,7 @@ store.register_group(true, "clippy::suspicious", Some("clippy_suspicious"), vec!
LintId::of(loops::EMPTY_LOOP),
LintId::of(loops::FOR_LOOPS_OVER_FALLIBLES),
LintId::of(loops::MUT_RANGE_BOUND),
LintId::of(methods::NO_EFFECT_REPLACE),
LintId::of(methods::SUSPICIOUS_MAP),
LintId::of(mut_key::MUTABLE_KEY_TYPE),
LintId::of(octal_escapes::OCTAL_ESCAPES),

View file

@ -44,6 +44,7 @@ mod map_identity;
mod map_unwrap_or;
mod needless_option_as_deref;
mod needless_option_take;
mod no_effect_replace;
mod ok_expect;
mod option_as_ref_deref;
mod option_map_or_none;
@ -2197,6 +2198,24 @@ declare_clippy_lint! {
"using `.as_ref().take()` on a temporary value"
}
declare_clippy_lint! {
/// ### What it does
/// Checks for `replace` statements which have no effect.
///
/// ### Why is this bad?
/// It's either a mistake or confusing.
///
/// ### Example
/// ```rust
/// "1234".replace("12", "12");
/// "1234".replacen("12", "12", 1);
/// ```
#[clippy::version = "1.62.0"]
pub NO_EFFECT_REPLACE,
suspicious,
"replace with no effect"
}
pub struct Methods {
avoid_breaking_exported_api: bool,
msrv: Option<RustcVersion>,
@ -2296,6 +2315,7 @@ impl_lint_pass!(Methods => [
NEEDLESS_OPTION_AS_DEREF,
IS_DIGIT_ASCII_RADIX,
NEEDLESS_OPTION_TAKE,
NO_EFFECT_REPLACE,
]);
/// Extracts a method call name, args, and `Span` of the method name.
@ -2707,6 +2727,9 @@ impl Methods {
unnecessary_lazy_eval::check(cx, expr, recv, u_arg, "unwrap_or");
},
},
("replace" | "replacen", [arg1, arg2] | [arg1, arg2, _]) => {
no_effect_replace::check(cx, expr, arg1, arg2);
},
_ => {},
}
}

View file

@ -0,0 +1,47 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::SpanlessEq;
use if_chain::if_chain;
use rustc_ast::LitKind;
use rustc_hir::ExprKind;
use rustc_lint::LateContext;
use rustc_span::sym;
use super::NO_EFFECT_REPLACE;
pub(super) fn check<'tcx>(
cx: &LateContext<'tcx>,
expr: &'tcx rustc_hir::Expr<'_>,
arg1: &'tcx rustc_hir::Expr<'_>,
arg2: &'tcx rustc_hir::Expr<'_>,
) {
let ty = cx.typeck_results().expr_ty(expr).peel_refs();
if !(ty.is_str() || is_type_diagnostic_item(cx, ty, sym::String)) {
return;
}
if_chain! {
if let ExprKind::Lit(spanned) = &arg1.kind;
if let Some(param1) = lit_string_value(&spanned.node);
if let ExprKind::Lit(spanned) = &arg2.kind;
if let LitKind::Str(param2, _) = &spanned.node;
if param1 == param2.as_str();
then {
span_lint(cx, NO_EFFECT_REPLACE, expr.span, "replacing text with itself");
}
}
if SpanlessEq::new(cx).eq_expr(arg1, arg2) {
span_lint(cx, NO_EFFECT_REPLACE, expr.span, "replacing text with itself");
}
}
fn lit_string_value(node: &LitKind) -> Option<String> {
match node {
LitKind::Char(value) => Some(value.to_string()),
LitKind::Str(value, _) => Some(value.as_str().to_owned()),
_ => None,
}
}