Skip to content

New lint no_effect_replace #8754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3594,6 +3594,7 @@ Released 2018-09-13
[`new_ret_no_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#new_ret_no_self
[`new_without_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default
[`no_effect`]: https://rust-lang.github.io/rust-clippy/master/index.html#no_effect
[`no_effect_replace`]: https://rust-lang.github.io/rust-clippy/master/index.html#no_effect_replace
[`no_effect_underscore_binding`]: https://rust-lang.github.io/rust-clippy/master/index.html#no_effect_underscore_binding
[`non_ascii_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_ascii_literal
[`non_octal_unix_permissions`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_octal_unix_permissions
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_suspicious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
23 changes: 23 additions & 0 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
},
_ => {},
}
}
Expand Down
47 changes: 47 additions & 0 deletions clippy_lints/src/methods/no_effect_replace.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
51 changes: 51 additions & 0 deletions tests/ui/no_effect_replace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![warn(clippy::no_effect_replace)]

fn main() {
let _ = "12345".replace('1', "1");
let _ = "12345".replace("12", "12");
let _ = String::new().replace("12", "12");

let _ = "12345".replacen('1', "1", 1);
let _ = "12345".replacen("12", "12", 1);
let _ = String::new().replacen("12", "12", 1);

let _ = "12345".replace("12", "22");
let _ = "12345".replacen("12", "22", 1);

let mut x = X::default();
let _ = "hello".replace(&x.f(), &x.f());
let _ = "hello".replace(&x.f(), &x.ff());

let _ = "hello".replace(&y(), &y());
let _ = "hello".replace(&y(), &z());

let _ = Replaceme.replace("a", "a");
}

#[derive(Default)]
struct X {}

impl X {
fn f(&mut self) -> String {
"he".to_string()
}

fn ff(&mut self) -> String {
"hh".to_string()
}
}

fn y() -> String {
"he".to_string()
}

fn z() -> String {
"hh".to_string()
}

struct Replaceme;
impl Replaceme {
pub fn replace(&mut self, a: &str, b: &str) -> Self {
Self
}
}
52 changes: 52 additions & 0 deletions tests/ui/no_effect_replace.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
error: replacing text with itself
--> $DIR/no_effect_replace.rs:4:13
|
LL | let _ = "12345".replace('1', "1");
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::no-effect-replace` implied by `-D warnings`

error: replacing text with itself
--> $DIR/no_effect_replace.rs:5:13
|
LL | let _ = "12345".replace("12", "12");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: replacing text with itself
--> $DIR/no_effect_replace.rs:6:13
|
LL | let _ = String::new().replace("12", "12");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: replacing text with itself
--> $DIR/no_effect_replace.rs:8:13
|
LL | let _ = "12345".replacen('1', "1", 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: replacing text with itself
--> $DIR/no_effect_replace.rs:9:13
|
LL | let _ = "12345".replacen("12", "12", 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: replacing text with itself
--> $DIR/no_effect_replace.rs:10:13
|
LL | let _ = String::new().replacen("12", "12", 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: replacing text with itself
--> $DIR/no_effect_replace.rs:16:13
|
LL | let _ = "hello".replace(&x.f(), &x.f());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: replacing text with itself
--> $DIR/no_effect_replace.rs:19:13
|
LL | let _ = "hello".replace(&y(), &y());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 8 previous errors