Skip to content

Commit ae57785

Browse files
committed
Auto merge of #6333 - PunitLodha:master, r=flip1995
Added lint str_to_string *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: un-deprecate [`str_to_string`] and [`string_to_string`] and introduce them as `restriction` lints again. Fixes #5610 Added new lint:- str_to_string r? `@flip1995`
2 parents 32c51d2 + 2345ef5 commit ae57785

12 files changed

+189
-91
lines changed

clippy_lints/src/deprecated_lints.rs

-20
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,6 @@ declare_deprecated_lint! {
5151
"`Vec::as_mut_slice` has been stabilized in 1.7"
5252
}
5353

54-
declare_deprecated_lint! {
55-
/// **What it does:** Nothing. This lint has been deprecated.
56-
///
57-
/// **Deprecation reason:** This used to check for `.to_string()` method calls on values
58-
/// of type `&str`. This is not unidiomatic and with specialization coming, `to_string` could be
59-
/// specialized to be as efficient as `to_owned`.
60-
pub STR_TO_STRING,
61-
"using `str::to_string` is common even today and specialization will likely happen soon"
62-
}
63-
64-
declare_deprecated_lint! {
65-
/// **What it does:** Nothing. This lint has been deprecated.
66-
///
67-
/// **Deprecation reason:** This used to check for `.to_string()` method calls on values
68-
/// of type `String`. This is not unidiomatic and with specialization coming, `to_string` could be
69-
/// specialized to be as efficient as `clone`.
70-
pub STRING_TO_STRING,
71-
"using `string::to_string` is common even today and specialization will likely happen soon"
72-
}
73-
7454
declare_deprecated_lint! {
7555
/// **What it does:** Nothing. This lint has been deprecated.
7656
///

clippy_lints/src/lib.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
441441
"clippy::unstable_as_mut_slice",
442442
"`Vec::as_mut_slice` has been stabilized in 1.7",
443443
);
444-
store.register_removed(
445-
"clippy::str_to_string",
446-
"using `str::to_string` is common even today and specialization will likely happen soon",
447-
);
448-
store.register_removed(
449-
"clippy::string_to_string",
450-
"using `string::to_string` is common even today and specialization will likely happen soon",
451-
);
452444
store.register_removed(
453445
"clippy::misaligned_transmute",
454446
"this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr",
@@ -840,6 +832,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
840832
&strings::STRING_ADD_ASSIGN,
841833
&strings::STRING_FROM_UTF8_AS_BYTES,
842834
&strings::STRING_LIT_AS_BYTES,
835+
&strings::STRING_TO_STRING,
836+
&strings::STR_TO_STRING,
843837
&suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,
844838
&suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL,
845839
&swap::ALMOST_SWAPPED,
@@ -1186,6 +1180,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11861180
store.register_early_pass(|| box asm_syntax::InlineAsmX86AttSyntax);
11871181
store.register_early_pass(|| box asm_syntax::InlineAsmX86IntelSyntax);
11881182
store.register_late_pass(|| box undropped_manually_drops::UndroppedManuallyDrops);
1183+
store.register_late_pass(|| box strings::StrToString);
1184+
store.register_late_pass(|| box strings::StringToString);
11891185

11901186

11911187
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
@@ -1228,6 +1224,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12281224
LintId::of(&shadow::SHADOW_REUSE),
12291225
LintId::of(&shadow::SHADOW_SAME),
12301226
LintId::of(&strings::STRING_ADD),
1227+
LintId::of(&strings::STRING_TO_STRING),
1228+
LintId::of(&strings::STR_TO_STRING),
12311229
LintId::of(&types::RC_BUFFER),
12321230
LintId::of(&unwrap_in_result::UNWRAP_IN_RESULT),
12331231
LintId::of(&verbose_file_reads::VERBOSE_FILE_READS),
@@ -1943,14 +1941,6 @@ fn register_removed_non_tool_lints(store: &mut rustc_lint::LintStore) {
19431941
"unstable_as_mut_slice",
19441942
"`Vec::as_mut_slice` has been stabilized in 1.7",
19451943
);
1946-
store.register_removed(
1947-
"str_to_string",
1948-
"using `str::to_string` is common even today and specialization will likely happen soon",
1949-
);
1950-
store.register_removed(
1951-
"string_to_string",
1952-
"using `string::to_string` is common even today and specialization will likely happen soon",
1953-
);
19541944
store.register_removed(
19551945
"misaligned_transmute",
19561946
"this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr",

clippy_lints/src/strings.rs

+99-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_errors::Applicability;
22
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, LangItem, QPath};
33
use rustc_lint::{LateContext, LateLintPass, LintContext};
44
use rustc_middle::lint::in_external_macro;
5+
use rustc_middle::ty;
56
use rustc_session::{declare_lint_pass, declare_tool_lint};
67
use rustc_span::source_map::Spanned;
78
use rustc_span::sym;
@@ -11,7 +12,7 @@ use if_chain::if_chain;
1112
use crate::utils::SpanlessEq;
1213
use crate::utils::{
1314
get_parent_expr, is_allowed, is_type_diagnostic_item, match_function_call, method_calls, paths, span_lint,
14-
span_lint_and_sugg,
15+
span_lint_and_help, span_lint_and_sugg,
1516
};
1617

1718
declare_clippy_lint! {
@@ -289,3 +290,100 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
289290
}
290291
}
291292
}
293+
294+
declare_clippy_lint! {
295+
/// **What it does:** This lint checks for `.to_string()` method calls on values of type `&str`.
296+
///
297+
/// **Why is this bad?** The `to_string` method is also used on other types to convert them to a string.
298+
/// When called on a `&str` it turns the `&str` into the owned variant `String`, which can be better
299+
/// expressed with `.to_owned()`.
300+
///
301+
/// **Known problems:** None.
302+
///
303+
/// **Example:**
304+
///
305+
/// ```rust
306+
/// // example code where clippy issues a warning
307+
/// let _ = "str".to_string();
308+
/// ```
309+
/// Use instead:
310+
/// ```rust
311+
/// // example code which does not raise clippy warning
312+
/// let _ = "str".to_owned();
313+
/// ```
314+
pub STR_TO_STRING,
315+
restriction,
316+
"using `to_string()` on a `&str`, which should be `to_owned()`"
317+
}
318+
319+
declare_lint_pass!(StrToString => [STR_TO_STRING]);
320+
321+
impl LateLintPass<'_> for StrToString {
322+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
323+
if_chain! {
324+
if let ExprKind::MethodCall(path, _, args, _) = &expr.kind;
325+
if path.ident.name == sym!(to_string);
326+
let ty = cx.typeck_results().expr_ty(&args[0]);
327+
if let ty::Ref(_, ty, ..) = ty.kind();
328+
if *ty.kind() == ty::Str;
329+
then {
330+
span_lint_and_help(
331+
cx,
332+
STR_TO_STRING,
333+
expr.span,
334+
"`to_string()` called on a `&str`",
335+
None,
336+
"consider using `.to_owned()`",
337+
);
338+
}
339+
}
340+
}
341+
}
342+
343+
declare_clippy_lint! {
344+
/// **What it does:** This lint checks for `.to_string()` method calls on values of type `String`.
345+
///
346+
/// **Why is this bad?** The `to_string` method is also used on other types to convert them to a string.
347+
/// When called on a `String` it only clones the `String`, which can be better expressed with `.clone()`.
348+
/// **Known problems:** None.
349+
///
350+
/// **Example:**
351+
///
352+
/// ```rust
353+
/// // example code where clippy issues a warning
354+
/// let msg = String::from("Hello World");
355+
/// let _ = msg.to_string();
356+
/// ```
357+
/// Use instead:
358+
/// ```rust
359+
/// // example code which does not raise clippy warning
360+
/// let msg = String::from("Hello World");
361+
/// let _ = msg.clone();
362+
/// ```
363+
pub STRING_TO_STRING,
364+
restriction,
365+
"using `to_string()` on a `String`, which should be `clone()`"
366+
}
367+
368+
declare_lint_pass!(StringToString => [STRING_TO_STRING]);
369+
370+
impl LateLintPass<'_> for StringToString {
371+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
372+
if_chain! {
373+
if let ExprKind::MethodCall(path, _, args, _) = &expr.kind;
374+
if path.ident.name == sym!(to_string);
375+
let ty = cx.typeck_results().expr_ty(&args[0]);
376+
if is_type_diagnostic_item(cx, ty, sym!(string_type));
377+
then {
378+
span_lint_and_help(
379+
cx,
380+
STRING_TO_STRING,
381+
expr.span,
382+
"`to_string()` called on a `String`",
383+
None,
384+
"consider using `.clone()`",
385+
);
386+
}
387+
}
388+
}
389+
}

src/lintlist/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,13 @@ vec![
22372237
deprecation: None,
22382238
module: "stable_sort_primitive",
22392239
},
2240+
Lint {
2241+
name: "str_to_string",
2242+
group: "restriction",
2243+
desc: "using `to_string()` on a `&str`, which should be `to_owned()`",
2244+
deprecation: None,
2245+
module: "strings",
2246+
},
22402247
Lint {
22412248
name: "string_add",
22422249
group: "restriction",
@@ -2272,6 +2279,13 @@ vec![
22722279
deprecation: None,
22732280
module: "strings",
22742281
},
2282+
Lint {
2283+
name: "string_to_string",
2284+
group: "restriction",
2285+
desc: "using `to_string()` on a `String`, which should be `clone()`",
2286+
deprecation: None,
2287+
module: "strings",
2288+
},
22752289
Lint {
22762290
name: "struct_excessive_bools",
22772291
group: "pedantic",

tests/ui/deprecated.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#[warn(clippy::str_to_string)]
2-
#[warn(clippy::string_to_string)]
31
#[warn(clippy::unstable_as_slice)]
42
#[warn(clippy::unstable_as_mut_slice)]
53
#[warn(clippy::misaligned_transmute)]

tests/ui/deprecated.stderr

+17-29
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,76 @@
1-
error: lint `clippy::str_to_string` has been removed: `using `str::to_string` is common even today and specialization will likely happen soon`
2-
--> $DIR/deprecated.rs:1:8
3-
|
4-
LL | #[warn(clippy::str_to_string)]
5-
| ^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: `-D renamed-and-removed-lints` implied by `-D warnings`
8-
9-
error: lint `clippy::string_to_string` has been removed: `using `string::to_string` is common even today and specialization will likely happen soon`
10-
--> $DIR/deprecated.rs:2:8
11-
|
12-
LL | #[warn(clippy::string_to_string)]
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^
14-
151
error: lint `clippy::unstable_as_slice` has been removed: ``Vec::as_slice` has been stabilized in 1.7`
16-
--> $DIR/deprecated.rs:3:8
2+
--> $DIR/deprecated.rs:1:8
173
|
184
LL | #[warn(clippy::unstable_as_slice)]
195
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D renamed-and-removed-lints` implied by `-D warnings`
208

219
error: lint `clippy::unstable_as_mut_slice` has been removed: ``Vec::as_mut_slice` has been stabilized in 1.7`
22-
--> $DIR/deprecated.rs:4:8
10+
--> $DIR/deprecated.rs:2:8
2311
|
2412
LL | #[warn(clippy::unstable_as_mut_slice)]
2513
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2614

2715
error: lint `clippy::misaligned_transmute` has been removed: `this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr`
28-
--> $DIR/deprecated.rs:5:8
16+
--> $DIR/deprecated.rs:3:8
2917
|
3018
LL | #[warn(clippy::misaligned_transmute)]
3119
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3220

3321
error: lint `clippy::unused_collect` has been removed: ``collect` has been marked as #[must_use] in rustc and that covers all cases of this lint`
34-
--> $DIR/deprecated.rs:6:8
22+
--> $DIR/deprecated.rs:4:8
3523
|
3624
LL | #[warn(clippy::unused_collect)]
3725
| ^^^^^^^^^^^^^^^^^^^^^^
3826

3927
error: lint `clippy::invalid_ref` has been removed: `superseded by rustc lint `invalid_value``
40-
--> $DIR/deprecated.rs:7:8
28+
--> $DIR/deprecated.rs:5:8
4129
|
4230
LL | #[warn(clippy::invalid_ref)]
4331
| ^^^^^^^^^^^^^^^^^^^
4432

4533
error: lint `clippy::into_iter_on_array` has been removed: `this lint has been uplifted to rustc and is now called `array_into_iter``
46-
--> $DIR/deprecated.rs:8:8
34+
--> $DIR/deprecated.rs:6:8
4735
|
4836
LL | #[warn(clippy::into_iter_on_array)]
4937
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
5038

5139
error: lint `clippy::unused_label` has been removed: `this lint has been uplifted to rustc and is now called `unused_labels``
52-
--> $DIR/deprecated.rs:9:8
40+
--> $DIR/deprecated.rs:7:8
5341
|
5442
LL | #[warn(clippy::unused_label)]
5543
| ^^^^^^^^^^^^^^^^^^^^
5644

5745
error: lint `clippy::regex_macro` has been removed: `the regex! macro has been removed from the regex crate in 2018`
58-
--> $DIR/deprecated.rs:10:8
46+
--> $DIR/deprecated.rs:8:8
5947
|
6048
LL | #[warn(clippy::regex_macro)]
6149
| ^^^^^^^^^^^^^^^^^^^
6250

6351
error: lint `clippy::drop_bounds` has been removed: `this lint has been uplifted to rustc and is now called `drop_bounds``
64-
--> $DIR/deprecated.rs:11:8
52+
--> $DIR/deprecated.rs:9:8
6553
|
6654
LL | #[warn(clippy::drop_bounds)]
6755
| ^^^^^^^^^^^^^^^^^^^
6856

6957
error: lint `clippy::temporary_cstring_as_ptr` has been removed: `this lint has been uplifted to rustc and is now called `temporary_cstring_as_ptr``
70-
--> $DIR/deprecated.rs:12:8
58+
--> $DIR/deprecated.rs:10:8
7159
|
7260
LL | #[warn(clippy::temporary_cstring_as_ptr)]
7361
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7462

7563
error: lint `clippy::panic_params` has been removed: `this lint has been uplifted to rustc and is now called `panic_fmt``
76-
--> $DIR/deprecated.rs:13:8
64+
--> $DIR/deprecated.rs:11:8
7765
|
7866
LL | #[warn(clippy::panic_params)]
7967
| ^^^^^^^^^^^^^^^^^^^^
8068

81-
error: lint `clippy::str_to_string` has been removed: `using `str::to_string` is common even today and specialization will likely happen soon`
69+
error: lint `clippy::unstable_as_slice` has been removed: ``Vec::as_slice` has been stabilized in 1.7`
8270
--> $DIR/deprecated.rs:1:8
8371
|
84-
LL | #[warn(clippy::str_to_string)]
85-
| ^^^^^^^^^^^^^^^^^^^^^
72+
LL | #[warn(clippy::unstable_as_slice)]
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
8674

87-
error: aborting due to 14 previous errors
75+
error: aborting due to 12 previous errors
8876

tests/ui/deprecated_old.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#[warn(str_to_string)]
2-
#[warn(string_to_string)]
31
#[warn(unstable_as_slice)]
42
#[warn(unstable_as_mut_slice)]
53
#[warn(misaligned_transmute)]

tests/ui/deprecated_old.stderr

+9-21
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
1-
error: lint `str_to_string` has been removed: `using `str::to_string` is common even today and specialization will likely happen soon`
2-
--> $DIR/deprecated_old.rs:1:8
3-
|
4-
LL | #[warn(str_to_string)]
5-
| ^^^^^^^^^^^^^
6-
|
7-
= note: `-D renamed-and-removed-lints` implied by `-D warnings`
8-
9-
error: lint `string_to_string` has been removed: `using `string::to_string` is common even today and specialization will likely happen soon`
10-
--> $DIR/deprecated_old.rs:2:8
11-
|
12-
LL | #[warn(string_to_string)]
13-
| ^^^^^^^^^^^^^^^^
14-
151
error: lint `unstable_as_slice` has been removed: ``Vec::as_slice` has been stabilized in 1.7`
16-
--> $DIR/deprecated_old.rs:3:8
2+
--> $DIR/deprecated_old.rs:1:8
173
|
184
LL | #[warn(unstable_as_slice)]
195
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D renamed-and-removed-lints` implied by `-D warnings`
208

219
error: lint `unstable_as_mut_slice` has been removed: ``Vec::as_mut_slice` has been stabilized in 1.7`
22-
--> $DIR/deprecated_old.rs:4:8
10+
--> $DIR/deprecated_old.rs:2:8
2311
|
2412
LL | #[warn(unstable_as_mut_slice)]
2513
| ^^^^^^^^^^^^^^^^^^^^^
2614

2715
error: lint `misaligned_transmute` has been removed: `this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr`
28-
--> $DIR/deprecated_old.rs:5:8
16+
--> $DIR/deprecated_old.rs:3:8
2917
|
3018
LL | #[warn(misaligned_transmute)]
3119
| ^^^^^^^^^^^^^^^^^^^^
3220

33-
error: lint `str_to_string` has been removed: `using `str::to_string` is common even today and specialization will likely happen soon`
21+
error: lint `unstable_as_slice` has been removed: ``Vec::as_slice` has been stabilized in 1.7`
3422
--> $DIR/deprecated_old.rs:1:8
3523
|
36-
LL | #[warn(str_to_string)]
37-
| ^^^^^^^^^^^^^
24+
LL | #[warn(unstable_as_slice)]
25+
| ^^^^^^^^^^^^^^^^^
3826

39-
error: aborting due to 6 previous errors
27+
error: aborting due to 4 previous errors
4028

0 commit comments

Comments
 (0)