Skip to content

Commit 858d96d

Browse files
committed
Auto merge of rust-lang#11871 - GuillaumeGomez:UNNECESSARY_TO_OWNED-split, r=llogiq
Extend `UNNECESSARY_TO_OWNED` to handle `split` Fixes rust-lang/rust-clippy#9965. When you have `to_string().split('a')` or equivalent, it'll suggest to remove the `to_owned`/`to_string` part. r? `@flip1995` changelog: Extend `UNNECESSARY_TO_OWNED` to handle `split`
2 parents 2ddcfb8 + 238c5f9 commit 858d96d

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

clippy_lints/src/methods/unnecessary_to_owned.rs

+56
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use clippy_utils::ty::{get_iterator_item_ty, implements_trait, is_copy, peel_mid
77
use clippy_utils::visitors::find_all_ret_expressions;
88
use clippy_utils::{fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, return_ty};
99
use rustc_errors::Applicability;
10+
use rustc_hir::def::{DefKind, Res};
1011
use rustc_hir::def_id::DefId;
1112
use rustc_hir::{BorrowKind, Expr, ExprKind, ItemKind, Node};
1213
use rustc_hir_typeck::{FnCtxt, Inherited};
@@ -37,6 +38,9 @@ pub fn check<'tcx>(
3738
if is_cloned_or_copied(cx, method_name, method_def_id) {
3839
unnecessary_iter_cloned::check(cx, expr, method_name, receiver);
3940
} else if is_to_owned_like(cx, expr, method_name, method_def_id) {
41+
if check_split_call_arg(cx, expr, method_name, receiver) {
42+
return;
43+
}
4044
// At this point, we know the call is of a `to_owned`-like function. The functions
4145
// `check_addr_of_expr` and `check_call_arg` determine whether the call is unnecessary
4246
// based on its context, that is, whether it is a referent in an `AddrOf` expression, an
@@ -233,6 +237,58 @@ fn check_into_iter_call_arg(
233237
false
234238
}
235239

240+
/// Checks whether `expr` is an argument in an `into_iter` call and, if so, determines whether its
241+
/// call of a `to_owned`-like function is unnecessary.
242+
fn check_split_call_arg(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: Symbol, receiver: &Expr<'_>) -> bool {
243+
if let Some(parent) = get_parent_expr(cx, expr)
244+
&& let Some((fn_name, argument_expr)) = get_fn_name_and_arg(cx, parent)
245+
&& fn_name.as_str() == "split"
246+
&& let Some(receiver_snippet) = snippet_opt(cx, receiver.span)
247+
&& let Some(arg_snippet) = snippet_opt(cx, argument_expr.span)
248+
{
249+
// The next suggestion may be incorrect because the removal of the `to_owned`-like
250+
// function could cause the iterator to hold a reference to a resource that is used
251+
// mutably. See https://github.com/rust-lang/rust-clippy/issues/8148.
252+
span_lint_and_sugg(
253+
cx,
254+
UNNECESSARY_TO_OWNED,
255+
parent.span,
256+
&format!("unnecessary use of `{method_name}`"),
257+
"use",
258+
format!("{receiver_snippet}.split({arg_snippet})"),
259+
Applicability::MaybeIncorrect,
260+
);
261+
return true;
262+
}
263+
false
264+
}
265+
266+
fn get_fn_name_and_arg<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option<(Symbol, Expr<'tcx>)> {
267+
match &expr.kind {
268+
ExprKind::MethodCall(path, _, [arg_expr], ..) => Some((path.ident.name, *arg_expr)),
269+
ExprKind::Call(
270+
Expr {
271+
kind: ExprKind::Path(qpath),
272+
hir_id: path_hir_id,
273+
..
274+
},
275+
[arg_expr],
276+
) => {
277+
// Only return Fn-like DefIds, not the DefIds of statics/consts/etc that contain or
278+
// deref to fn pointers, dyn Fn, impl Fn - #8850
279+
if let Res::Def(DefKind::Fn | DefKind::Ctor(..) | DefKind::AssocFn, def_id) =
280+
cx.typeck_results().qpath_res(qpath, *path_hir_id)
281+
&& let Some(fn_name) = cx.tcx.opt_item_name(def_id)
282+
{
283+
Some((fn_name, *arg_expr))
284+
} else {
285+
None
286+
}
287+
},
288+
_ => None,
289+
}
290+
}
291+
236292
/// Checks whether `expr` is an argument in a function call and, if so, determines whether its call
237293
/// of a `to_owned`-like function is unnecessary.
238294
fn check_other_call_arg<'tcx>(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#[allow(clippy::single_char_pattern)]
2+
3+
fn main() {
4+
let _ = "a".split('a').next().unwrap();
5+
//~^ ERROR: unnecessary use of `to_string`
6+
let _ = "a".split("a").next().unwrap();
7+
//~^ ERROR: unnecessary use of `to_string`
8+
let _ = "a".split('a').next().unwrap();
9+
//~^ ERROR: unnecessary use of `to_owned`
10+
let _ = "a".split("a").next().unwrap();
11+
//~^ ERROR: unnecessary use of `to_owned`
12+
13+
let _ = [1].split(|x| *x == 2).next().unwrap();
14+
//~^ ERROR: unnecessary use of `to_vec`
15+
let _ = [1].split(|x| *x == 2).next().unwrap();
16+
//~^ ERROR: unnecessary use of `to_vec`
17+
let _ = [1].split(|x| *x == 2).next().unwrap();
18+
//~^ ERROR: unnecessary use of `to_owned`
19+
let _ = [1].split(|x| *x == 2).next().unwrap();
20+
//~^ ERROR: unnecessary use of `to_owned`
21+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#[allow(clippy::single_char_pattern)]
2+
3+
fn main() {
4+
let _ = "a".to_string().split('a').next().unwrap();
5+
//~^ ERROR: unnecessary use of `to_string`
6+
let _ = "a".to_string().split("a").next().unwrap();
7+
//~^ ERROR: unnecessary use of `to_string`
8+
let _ = "a".to_owned().split('a').next().unwrap();
9+
//~^ ERROR: unnecessary use of `to_owned`
10+
let _ = "a".to_owned().split("a").next().unwrap();
11+
//~^ ERROR: unnecessary use of `to_owned`
12+
13+
let _ = [1].to_vec().split(|x| *x == 2).next().unwrap();
14+
//~^ ERROR: unnecessary use of `to_vec`
15+
let _ = [1].to_vec().split(|x| *x == 2).next().unwrap();
16+
//~^ ERROR: unnecessary use of `to_vec`
17+
let _ = [1].to_owned().split(|x| *x == 2).next().unwrap();
18+
//~^ ERROR: unnecessary use of `to_owned`
19+
let _ = [1].to_owned().split(|x| *x == 2).next().unwrap();
20+
//~^ ERROR: unnecessary use of `to_owned`
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
error: unnecessary use of `to_string`
2+
--> $DIR/unnecessary_to_owned_on_split.rs:4:13
3+
|
4+
LL | let _ = "a".to_string().split('a').next().unwrap();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `"a".split('a')`
6+
|
7+
= note: `-D clippy::unnecessary-to-owned` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_to_owned)]`
9+
10+
error: unnecessary use of `to_string`
11+
--> $DIR/unnecessary_to_owned_on_split.rs:6:13
12+
|
13+
LL | let _ = "a".to_string().split("a").next().unwrap();
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `"a".split("a")`
15+
16+
error: unnecessary use of `to_owned`
17+
--> $DIR/unnecessary_to_owned_on_split.rs:8:13
18+
|
19+
LL | let _ = "a".to_owned().split('a').next().unwrap();
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `"a".split('a')`
21+
22+
error: unnecessary use of `to_owned`
23+
--> $DIR/unnecessary_to_owned_on_split.rs:10:13
24+
|
25+
LL | let _ = "a".to_owned().split("a").next().unwrap();
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `"a".split("a")`
27+
28+
error: unnecessary use of `to_vec`
29+
--> $DIR/unnecessary_to_owned_on_split.rs:13:13
30+
|
31+
LL | let _ = [1].to_vec().split(|x| *x == 2).next().unwrap();
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[1].split(|x| *x == 2)`
33+
34+
error: unnecessary use of `to_vec`
35+
--> $DIR/unnecessary_to_owned_on_split.rs:15:13
36+
|
37+
LL | let _ = [1].to_vec().split(|x| *x == 2).next().unwrap();
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[1].split(|x| *x == 2)`
39+
40+
error: unnecessary use of `to_owned`
41+
--> $DIR/unnecessary_to_owned_on_split.rs:17:13
42+
|
43+
LL | let _ = [1].to_owned().split(|x| *x == 2).next().unwrap();
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[1].split(|x| *x == 2)`
45+
46+
error: unnecessary use of `to_owned`
47+
--> $DIR/unnecessary_to_owned_on_split.rs:19:13
48+
|
49+
LL | let _ = [1].to_owned().split(|x| *x == 2).next().unwrap();
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[1].split(|x| *x == 2)`
51+
52+
error: aborting due to 8 previous errors
53+

0 commit comments

Comments
 (0)