-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Lint collapsible_str_replace
#9269
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a4413f7
Register new lint collapsible_str_replace to methods
nahuakang 89698b9
Extend and improve initial test cases for collapsible_str_replace
nahuakang 6e86687
Handle replace calls with char slices
nahuakang a9bd0bd
Handle repeated str::replace calls with single char kind to str
nahuakang c989746
Remove checks on char slice; improve lint suggestion
nahuakang fb30b64
Adjust test cases; run cargo dev bless
nahuakang b070b40
Simplify lint logic and address code review comments
nahuakang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::visitors::for_each_expr; | ||
use clippy_utils::{eq_expr_value, get_parent_expr}; | ||
use core::ops::ControlFlow; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use std::collections::VecDeque; | ||
|
||
use super::method_call; | ||
use super::COLLAPSIBLE_STR_REPLACE; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx hir::Expr<'tcx>, | ||
from: &'tcx hir::Expr<'tcx>, | ||
to: &'tcx hir::Expr<'tcx>, | ||
) { | ||
let replace_methods = collect_replace_calls(cx, expr, to); | ||
if replace_methods.methods.len() > 1 { | ||
let from_kind = cx.typeck_results().expr_ty(from).peel_refs().kind(); | ||
// If the parent node's `to` argument is the same as the `to` argument | ||
// of the last replace call in the current chain, don't lint as it was already linted | ||
if let Some(parent) = get_parent_expr(cx, expr) | ||
&& let Some(("replace", [_, current_from, current_to], _)) = method_call(parent) | ||
&& eq_expr_value(cx, to, current_to) | ||
&& from_kind == cx.typeck_results().expr_ty(current_from).peel_refs().kind() | ||
{ | ||
return; | ||
} | ||
|
||
check_consecutive_replace_calls(cx, expr, &replace_methods, to); | ||
} | ||
} | ||
|
||
struct ReplaceMethods<'tcx> { | ||
methods: VecDeque<&'tcx hir::Expr<'tcx>>, | ||
from_args: VecDeque<&'tcx hir::Expr<'tcx>>, | ||
} | ||
|
||
fn collect_replace_calls<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx hir::Expr<'tcx>, | ||
to_arg: &'tcx hir::Expr<'tcx>, | ||
) -> ReplaceMethods<'tcx> { | ||
let mut methods = VecDeque::new(); | ||
let mut from_args = VecDeque::new(); | ||
|
||
let _: Option<()> = for_each_expr(expr, |e| { | ||
if let Some(("replace", [_, from, to], _)) = method_call(e) { | ||
if eq_expr_value(cx, to_arg, to) && cx.typeck_results().expr_ty(from).peel_refs().is_char() { | ||
methods.push_front(e); | ||
from_args.push_front(from); | ||
ControlFlow::Continue(()) | ||
} else { | ||
ControlFlow::BREAK | ||
} | ||
} else { | ||
ControlFlow::Continue(()) | ||
} | ||
}); | ||
|
||
ReplaceMethods { methods, from_args } | ||
} | ||
|
||
/// Check a chain of `str::replace` calls for `collapsible_str_replace` lint. | ||
fn check_consecutive_replace_calls<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx hir::Expr<'tcx>, | ||
replace_methods: &ReplaceMethods<'tcx>, | ||
to_arg: &'tcx hir::Expr<'tcx>, | ||
) { | ||
let from_args = &replace_methods.from_args; | ||
let from_arg_reprs: Vec<String> = from_args | ||
.iter() | ||
.map(|from_arg| snippet(cx, from_arg.span, "..").to_string()) | ||
.collect(); | ||
let app = Applicability::MachineApplicable; | ||
let earliest_replace_call = replace_methods.methods.front().unwrap(); | ||
if let Some((_, [..], span_lo)) = method_call(earliest_replace_call) { | ||
span_lint_and_sugg( | ||
cx, | ||
COLLAPSIBLE_STR_REPLACE, | ||
expr.span.with_lo(span_lo.lo()), | ||
"used consecutive `str::replace` call", | ||
"replace with", | ||
format!( | ||
"replace([{}], {})", | ||
from_arg_reprs.join(", "), | ||
snippet(cx, to_arg.span, ".."), | ||
), | ||
app, | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::collapsible_str_replace)] | ||
|
||
fn get_filter() -> char { | ||
'u' | ||
} | ||
|
||
fn main() { | ||
let d = 'd'; | ||
let p = 'p'; | ||
let s = 's'; | ||
let u = 'u'; | ||
let l = "l"; | ||
|
||
let mut iter = ["l", "z"].iter(); | ||
|
||
// LINT CASES | ||
let _ = "hesuo worpd".replace(['s', 'u'], "l"); | ||
|
||
let _ = "hesuo worpd".replace(['s', 'u'], l); | ||
|
||
let _ = "hesuo worpd".replace(['s', 'u', 'p'], "l"); | ||
|
||
let _ = "hesuo worpd" | ||
.replace(['s', 'u', 'p', 'd'], "l"); | ||
|
||
let _ = "hesuo world".replace([s, 'u'], "l"); | ||
|
||
let _ = "hesuo worpd".replace([s, 'u', 'p'], "l"); | ||
|
||
let _ = "hesuo worpd".replace([s, u, 'p'], "l"); | ||
|
||
let _ = "hesuo worpd".replace([s, u, p], "l"); | ||
|
||
let _ = "hesuo worlp".replace(['s', 'u'], "l").replace('p', "d"); | ||
|
||
let _ = "hesuo worpd".replace('s', "x").replace(['u', 'p'], "l"); | ||
|
||
// Note: Future iterations could lint `replace(|c| matches!(c, "su" | 'd' | 'p'), "l")` | ||
let _ = "hesudo worpd".replace("su", "l").replace(['d', 'p'], "l"); | ||
|
||
let _ = "hesudo worpd".replace([d, 'p'], "l").replace("su", "l"); | ||
|
||
let _ = "hesuo world".replace([get_filter(), 's'], "l"); | ||
|
||
// NO LINT CASES | ||
let _ = "hesuo world".replace('s', "l").replace('u', "p"); | ||
|
||
let _ = "hesuo worpd".replace('s', "l").replace('p', l); | ||
|
||
let _ = "hesudo worpd".replace('d', "l").replace("su", "l").replace('p', "l"); | ||
|
||
// Note: Future iterations of `collapsible_str_replace` might lint this and combine to `[s, u, p]` | ||
let _ = "hesuo worpd".replace([s, u], "l").replace([u, p], "l"); | ||
|
||
let _ = "hesuo worpd".replace(['s', 'u'], "l").replace(['u', 'p'], "l"); | ||
|
||
let _ = "hesuo worpd".replace('s', "l").replace(['u', 'p'], "l"); | ||
|
||
let _ = "hesuo worpd".replace(['s', 'u', 'p'], "l").replace('r', "l"); | ||
|
||
let _ = "hesuo worpd".replace(['s', 'u', 'p'], l).replace('r', l); | ||
|
||
let _ = "hesuo worpd".replace(['s', u, 'p'], "l").replace('r', "l"); | ||
|
||
let _ = "hesuo worpd".replace([s, u], "l").replace(p, "l"); | ||
|
||
// Regression test | ||
let _ = "hesuo worpd" | ||
.replace('u', iter.next().unwrap()) | ||
.replace('s', iter.next().unwrap()); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::collapsible_str_replace)] | ||
|
||
fn get_filter() -> char { | ||
'u' | ||
} | ||
|
||
fn main() { | ||
let d = 'd'; | ||
let p = 'p'; | ||
let s = 's'; | ||
let u = 'u'; | ||
let l = "l"; | ||
|
||
let mut iter = ["l", "z"].iter(); | ||
|
||
// LINT CASES | ||
let _ = "hesuo worpd".replace('s', "l").replace('u', "l"); | ||
|
||
let _ = "hesuo worpd".replace('s', l).replace('u', l); | ||
|
||
let _ = "hesuo worpd".replace('s', "l").replace('u', "l").replace('p', "l"); | ||
|
||
let _ = "hesuo worpd" | ||
.replace('s', "l") | ||
.replace('u', "l") | ||
.replace('p', "l") | ||
.replace('d', "l"); | ||
|
||
let _ = "hesuo world".replace(s, "l").replace('u', "l"); | ||
|
||
let _ = "hesuo worpd".replace(s, "l").replace('u', "l").replace('p', "l"); | ||
|
||
let _ = "hesuo worpd".replace(s, "l").replace(u, "l").replace('p', "l"); | ||
|
||
let _ = "hesuo worpd".replace(s, "l").replace(u, "l").replace(p, "l"); | ||
|
||
let _ = "hesuo worlp".replace('s', "l").replace('u', "l").replace('p', "d"); | ||
|
||
let _ = "hesuo worpd".replace('s', "x").replace('u', "l").replace('p', "l"); | ||
|
||
// Note: Future iterations could lint `replace(|c| matches!(c, "su" | 'd' | 'p'), "l")` | ||
let _ = "hesudo worpd".replace("su", "l").replace('d', "l").replace('p', "l"); | ||
nahuakang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let _ = "hesudo worpd".replace(d, "l").replace('p', "l").replace("su", "l"); | ||
|
||
let _ = "hesuo world".replace(get_filter(), "l").replace('s', "l"); | ||
|
||
// NO LINT CASES | ||
let _ = "hesuo world".replace('s', "l").replace('u', "p"); | ||
|
||
let _ = "hesuo worpd".replace('s', "l").replace('p', l); | ||
|
||
let _ = "hesudo worpd".replace('d', "l").replace("su", "l").replace('p', "l"); | ||
|
||
// Note: Future iterations of `collapsible_str_replace` might lint this and combine to `[s, u, p]` | ||
let _ = "hesuo worpd".replace([s, u], "l").replace([u, p], "l"); | ||
|
||
let _ = "hesuo worpd".replace(['s', 'u'], "l").replace(['u', 'p'], "l"); | ||
|
||
let _ = "hesuo worpd".replace('s', "l").replace(['u', 'p'], "l"); | ||
|
||
let _ = "hesuo worpd".replace(['s', 'u', 'p'], "l").replace('r', "l"); | ||
|
||
let _ = "hesuo worpd".replace(['s', 'u', 'p'], l).replace('r', l); | ||
|
||
let _ = "hesuo worpd".replace(['s', u, 'p'], "l").replace('r', "l"); | ||
|
||
let _ = "hesuo worpd".replace([s, u], "l").replace(p, "l"); | ||
|
||
// Regression test | ||
let _ = "hesuo worpd" | ||
.replace('u', iter.next().unwrap()) | ||
.replace('s', iter.next().unwrap()); | ||
} | ||
nahuakang marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.