Skip to content

Commit

Permalink
edit
Browse files Browse the repository at this point in the history
  • Loading branch information
surechen committed Nov 16, 2021
1 parent c2e56e0 commit def7b3e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
10 changes: 7 additions & 3 deletions clippy_lints/src/methods/str_splitn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,25 @@ pub(super) fn check_needless_splitn(
}
let ctxt = expr.span.ctxt();
let mut app = Applicability::MachineApplicable;
let (reverse, message) = if method_name == "splitn" {
(false, "unnecessary use of `splitn`")
} else {
(true, "unnecessary use of `rsplitn`")
};
if_chain! {
if method_name == "splitn";
if count >= 2;
if check_iter(cx, ctxt, cx.tcx.hir().parent_iter(expr.hir_id), count);
then {
span_lint_and_sugg(
cx,
NEEDLESS_SPLITN,
expr.span,
"unnecessary use of `splitn`",
message,
"try this",
format!(
"{}.{}({})",
snippet_with_context(cx, self_arg.span, ctxt, "..", &mut app).0,
"split",
if reverse {"rsplit"} else {"split"},
snippet_with_context(cx, pat_arg.span, ctxt, "..", &mut app).0
),
app,
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/needless_splitn.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ fn main() {
let (_, _) = str.splitn(2, '=').next_tuple().unwrap();
let (_, _) = str.split('=').next_tuple().unwrap();
let _: Vec<&str> = str.splitn(3, '=').collect();

let _ = str.rsplit('=').next();
let _ = str.rsplit('=').nth(0);
let _ = str.rsplitn(2, '=').nth(1);
let (_, _) = str.rsplitn(2, '=').next_tuple().unwrap();
let (_, _) = str.rsplit('=').next_tuple().unwrap();
}
6 changes: 6 additions & 0 deletions tests/ui/needless_splitn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ fn main() {
let (_, _) = str.splitn(2, '=').next_tuple().unwrap();
let (_, _) = str.splitn(3, '=').next_tuple().unwrap();
let _: Vec<&str> = str.splitn(3, '=').collect();

let _ = str.rsplitn(2, '=').next();
let _ = str.rsplitn(2, '=').nth(0);
let _ = str.rsplitn(2, '=').nth(1);
let (_, _) = str.rsplitn(2, '=').next_tuple().unwrap();
let (_, _) = str.rsplitn(3, '=').next_tuple().unwrap();
}
20 changes: 19 additions & 1 deletion tests/ui/needless_splitn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,23 @@ error: unnecessary use of `splitn`
LL | let (_, _) = str.splitn(3, '=').next_tuple().unwrap();
| ^^^^^^^^^^^^^^^^^^ help: try this: `str.split('=')`

error: aborting due to 3 previous errors
error: unnecessary use of `rsplitn`
--> $DIR/needless_splitn.rs:22:13
|
LL | let _ = str.rsplitn(2, '=').next();
| ^^^^^^^^^^^^^^^^^^^ help: try this: `str.rsplit('=')`

error: unnecessary use of `rsplitn`
--> $DIR/needless_splitn.rs:23:13
|
LL | let _ = str.rsplitn(2, '=').nth(0);
| ^^^^^^^^^^^^^^^^^^^ help: try this: `str.rsplit('=')`

error: unnecessary use of `rsplitn`
--> $DIR/needless_splitn.rs:26:18
|
LL | let (_, _) = str.rsplitn(3, '=').next_tuple().unwrap();
| ^^^^^^^^^^^^^^^^^^^ help: try this: `str.rsplit('=')`

error: aborting due to 6 previous errors

0 comments on commit def7b3e

Please sign in to comment.