mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
added lints str_to_string and string_to_string
This commit is contained in:
parent
32c51d2c58
commit
2345ef5b1f
12 changed files with 189 additions and 91 deletions
|
@ -51,26 +51,6 @@ declare_deprecated_lint! {
|
|||
"`Vec::as_mut_slice` has been stabilized in 1.7"
|
||||
}
|
||||
|
||||
declare_deprecated_lint! {
|
||||
/// **What it does:** Nothing. This lint has been deprecated.
|
||||
///
|
||||
/// **Deprecation reason:** This used to check for `.to_string()` method calls on values
|
||||
/// of type `&str`. This is not unidiomatic and with specialization coming, `to_string` could be
|
||||
/// specialized to be as efficient as `to_owned`.
|
||||
pub STR_TO_STRING,
|
||||
"using `str::to_string` is common even today and specialization will likely happen soon"
|
||||
}
|
||||
|
||||
declare_deprecated_lint! {
|
||||
/// **What it does:** Nothing. This lint has been deprecated.
|
||||
///
|
||||
/// **Deprecation reason:** This used to check for `.to_string()` method calls on values
|
||||
/// of type `String`. This is not unidiomatic and with specialization coming, `to_string` could be
|
||||
/// specialized to be as efficient as `clone`.
|
||||
pub STRING_TO_STRING,
|
||||
"using `string::to_string` is common even today and specialization will likely happen soon"
|
||||
}
|
||||
|
||||
declare_deprecated_lint! {
|
||||
/// **What it does:** Nothing. This lint has been deprecated.
|
||||
///
|
||||
|
|
|
@ -441,14 +441,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
"clippy::unstable_as_mut_slice",
|
||||
"`Vec::as_mut_slice` has been stabilized in 1.7",
|
||||
);
|
||||
store.register_removed(
|
||||
"clippy::str_to_string",
|
||||
"using `str::to_string` is common even today and specialization will likely happen soon",
|
||||
);
|
||||
store.register_removed(
|
||||
"clippy::string_to_string",
|
||||
"using `string::to_string` is common even today and specialization will likely happen soon",
|
||||
);
|
||||
store.register_removed(
|
||||
"clippy::misaligned_transmute",
|
||||
"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:
|
|||
&strings::STRING_ADD_ASSIGN,
|
||||
&strings::STRING_FROM_UTF8_AS_BYTES,
|
||||
&strings::STRING_LIT_AS_BYTES,
|
||||
&strings::STRING_TO_STRING,
|
||||
&strings::STR_TO_STRING,
|
||||
&suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,
|
||||
&suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL,
|
||||
&swap::ALMOST_SWAPPED,
|
||||
|
@ -1186,6 +1180,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
store.register_early_pass(|| box asm_syntax::InlineAsmX86AttSyntax);
|
||||
store.register_early_pass(|| box asm_syntax::InlineAsmX86IntelSyntax);
|
||||
store.register_late_pass(|| box undropped_manually_drops::UndroppedManuallyDrops);
|
||||
store.register_late_pass(|| box strings::StrToString);
|
||||
store.register_late_pass(|| box strings::StringToString);
|
||||
|
||||
|
||||
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:
|
|||
LintId::of(&shadow::SHADOW_REUSE),
|
||||
LintId::of(&shadow::SHADOW_SAME),
|
||||
LintId::of(&strings::STRING_ADD),
|
||||
LintId::of(&strings::STRING_TO_STRING),
|
||||
LintId::of(&strings::STR_TO_STRING),
|
||||
LintId::of(&types::RC_BUFFER),
|
||||
LintId::of(&unwrap_in_result::UNWRAP_IN_RESULT),
|
||||
LintId::of(&verbose_file_reads::VERBOSE_FILE_READS),
|
||||
|
@ -1943,14 +1941,6 @@ fn register_removed_non_tool_lints(store: &mut rustc_lint::LintStore) {
|
|||
"unstable_as_mut_slice",
|
||||
"`Vec::as_mut_slice` has been stabilized in 1.7",
|
||||
);
|
||||
store.register_removed(
|
||||
"str_to_string",
|
||||
"using `str::to_string` is common even today and specialization will likely happen soon",
|
||||
);
|
||||
store.register_removed(
|
||||
"string_to_string",
|
||||
"using `string::to_string` is common even today and specialization will likely happen soon",
|
||||
);
|
||||
store.register_removed(
|
||||
"misaligned_transmute",
|
||||
"this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr",
|
||||
|
|
|
@ -2,6 +2,7 @@ use rustc_errors::Applicability;
|
|||
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, LangItem, QPath};
|
||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||
use rustc_middle::lint::in_external_macro;
|
||||
use rustc_middle::ty;
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::source_map::Spanned;
|
||||
use rustc_span::sym;
|
||||
|
@ -11,7 +12,7 @@ use if_chain::if_chain;
|
|||
use crate::utils::SpanlessEq;
|
||||
use crate::utils::{
|
||||
get_parent_expr, is_allowed, is_type_diagnostic_item, match_function_call, method_calls, paths, span_lint,
|
||||
span_lint_and_sugg,
|
||||
span_lint_and_help, span_lint_and_sugg,
|
||||
};
|
||||
|
||||
declare_clippy_lint! {
|
||||
|
@ -289,3 +290,100 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// **What it does:** This lint checks for `.to_string()` method calls on values of type `&str`.
|
||||
///
|
||||
/// **Why is this bad?** The `to_string` method is also used on other types to convert them to a string.
|
||||
/// When called on a `&str` it turns the `&str` into the owned variant `String`, which can be better
|
||||
/// expressed with `.to_owned()`.
|
||||
///
|
||||
/// **Known problems:** None.
|
||||
///
|
||||
/// **Example:**
|
||||
///
|
||||
/// ```rust
|
||||
/// // example code where clippy issues a warning
|
||||
/// let _ = "str".to_string();
|
||||
/// ```
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// // example code which does not raise clippy warning
|
||||
/// let _ = "str".to_owned();
|
||||
/// ```
|
||||
pub STR_TO_STRING,
|
||||
restriction,
|
||||
"using `to_string()` on a `&str`, which should be `to_owned()`"
|
||||
}
|
||||
|
||||
declare_lint_pass!(StrToString => [STR_TO_STRING]);
|
||||
|
||||
impl LateLintPass<'_> for StrToString {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
|
||||
if_chain! {
|
||||
if let ExprKind::MethodCall(path, _, args, _) = &expr.kind;
|
||||
if path.ident.name == sym!(to_string);
|
||||
let ty = cx.typeck_results().expr_ty(&args[0]);
|
||||
if let ty::Ref(_, ty, ..) = ty.kind();
|
||||
if *ty.kind() == ty::Str;
|
||||
then {
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
STR_TO_STRING,
|
||||
expr.span,
|
||||
"`to_string()` called on a `&str`",
|
||||
None,
|
||||
"consider using `.to_owned()`",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// **What it does:** This lint checks for `.to_string()` method calls on values of type `String`.
|
||||
///
|
||||
/// **Why is this bad?** The `to_string` method is also used on other types to convert them to a string.
|
||||
/// When called on a `String` it only clones the `String`, which can be better expressed with `.clone()`.
|
||||
/// **Known problems:** None.
|
||||
///
|
||||
/// **Example:**
|
||||
///
|
||||
/// ```rust
|
||||
/// // example code where clippy issues a warning
|
||||
/// let msg = String::from("Hello World");
|
||||
/// let _ = msg.to_string();
|
||||
/// ```
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// // example code which does not raise clippy warning
|
||||
/// let msg = String::from("Hello World");
|
||||
/// let _ = msg.clone();
|
||||
/// ```
|
||||
pub STRING_TO_STRING,
|
||||
restriction,
|
||||
"using `to_string()` on a `String`, which should be `clone()`"
|
||||
}
|
||||
|
||||
declare_lint_pass!(StringToString => [STRING_TO_STRING]);
|
||||
|
||||
impl LateLintPass<'_> for StringToString {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
|
||||
if_chain! {
|
||||
if let ExprKind::MethodCall(path, _, args, _) = &expr.kind;
|
||||
if path.ident.name == sym!(to_string);
|
||||
let ty = cx.typeck_results().expr_ty(&args[0]);
|
||||
if is_type_diagnostic_item(cx, ty, sym!(string_type));
|
||||
then {
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
STRING_TO_STRING,
|
||||
expr.span,
|
||||
"`to_string()` called on a `String`",
|
||||
None,
|
||||
"consider using `.clone()`",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2237,6 +2237,13 @@ vec![
|
|||
deprecation: None,
|
||||
module: "stable_sort_primitive",
|
||||
},
|
||||
Lint {
|
||||
name: "str_to_string",
|
||||
group: "restriction",
|
||||
desc: "using `to_string()` on a `&str`, which should be `to_owned()`",
|
||||
deprecation: None,
|
||||
module: "strings",
|
||||
},
|
||||
Lint {
|
||||
name: "string_add",
|
||||
group: "restriction",
|
||||
|
@ -2272,6 +2279,13 @@ vec![
|
|||
deprecation: None,
|
||||
module: "strings",
|
||||
},
|
||||
Lint {
|
||||
name: "string_to_string",
|
||||
group: "restriction",
|
||||
desc: "using `to_string()` on a `String`, which should be `clone()`",
|
||||
deprecation: None,
|
||||
module: "strings",
|
||||
},
|
||||
Lint {
|
||||
name: "struct_excessive_bools",
|
||||
group: "pedantic",
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
#[warn(clippy::str_to_string)]
|
||||
#[warn(clippy::string_to_string)]
|
||||
#[warn(clippy::unstable_as_slice)]
|
||||
#[warn(clippy::unstable_as_mut_slice)]
|
||||
#[warn(clippy::misaligned_transmute)]
|
||||
|
|
|
@ -1,88 +1,76 @@
|
|||
error: lint `clippy::str_to_string` has been removed: `using `str::to_string` is common even today and specialization will likely happen soon`
|
||||
--> $DIR/deprecated.rs:1:8
|
||||
|
|
||||
LL | #[warn(clippy::str_to_string)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D renamed-and-removed-lints` implied by `-D warnings`
|
||||
|
||||
error: lint `clippy::string_to_string` has been removed: `using `string::to_string` is common even today and specialization will likely happen soon`
|
||||
--> $DIR/deprecated.rs:2:8
|
||||
|
|
||||
LL | #[warn(clippy::string_to_string)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: lint `clippy::unstable_as_slice` has been removed: ``Vec::as_slice` has been stabilized in 1.7`
|
||||
--> $DIR/deprecated.rs:3:8
|
||||
--> $DIR/deprecated.rs:1:8
|
||||
|
|
||||
LL | #[warn(clippy::unstable_as_slice)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D renamed-and-removed-lints` implied by `-D warnings`
|
||||
|
||||
error: lint `clippy::unstable_as_mut_slice` has been removed: ``Vec::as_mut_slice` has been stabilized in 1.7`
|
||||
--> $DIR/deprecated.rs:4:8
|
||||
--> $DIR/deprecated.rs:2:8
|
||||
|
|
||||
LL | #[warn(clippy::unstable_as_mut_slice)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: lint `clippy::misaligned_transmute` has been removed: `this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr`
|
||||
--> $DIR/deprecated.rs:5:8
|
||||
--> $DIR/deprecated.rs:3:8
|
||||
|
|
||||
LL | #[warn(clippy::misaligned_transmute)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
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`
|
||||
--> $DIR/deprecated.rs:6:8
|
||||
--> $DIR/deprecated.rs:4:8
|
||||
|
|
||||
LL | #[warn(clippy::unused_collect)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: lint `clippy::invalid_ref` has been removed: `superseded by rustc lint `invalid_value``
|
||||
--> $DIR/deprecated.rs:7:8
|
||||
--> $DIR/deprecated.rs:5:8
|
||||
|
|
||||
LL | #[warn(clippy::invalid_ref)]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: lint `clippy::into_iter_on_array` has been removed: `this lint has been uplifted to rustc and is now called `array_into_iter``
|
||||
--> $DIR/deprecated.rs:8:8
|
||||
--> $DIR/deprecated.rs:6:8
|
||||
|
|
||||
LL | #[warn(clippy::into_iter_on_array)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: lint `clippy::unused_label` has been removed: `this lint has been uplifted to rustc and is now called `unused_labels``
|
||||
--> $DIR/deprecated.rs:9:8
|
||||
--> $DIR/deprecated.rs:7:8
|
||||
|
|
||||
LL | #[warn(clippy::unused_label)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: lint `clippy::regex_macro` has been removed: `the regex! macro has been removed from the regex crate in 2018`
|
||||
--> $DIR/deprecated.rs:10:8
|
||||
--> $DIR/deprecated.rs:8:8
|
||||
|
|
||||
LL | #[warn(clippy::regex_macro)]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: lint `clippy::drop_bounds` has been removed: `this lint has been uplifted to rustc and is now called `drop_bounds``
|
||||
--> $DIR/deprecated.rs:11:8
|
||||
--> $DIR/deprecated.rs:9:8
|
||||
|
|
||||
LL | #[warn(clippy::drop_bounds)]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
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``
|
||||
--> $DIR/deprecated.rs:12:8
|
||||
--> $DIR/deprecated.rs:10:8
|
||||
|
|
||||
LL | #[warn(clippy::temporary_cstring_as_ptr)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: lint `clippy::panic_params` has been removed: `this lint has been uplifted to rustc and is now called `panic_fmt``
|
||||
--> $DIR/deprecated.rs:13:8
|
||||
--> $DIR/deprecated.rs:11:8
|
||||
|
|
||||
LL | #[warn(clippy::panic_params)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: lint `clippy::str_to_string` has been removed: `using `str::to_string` is common even today and specialization will likely happen soon`
|
||||
error: lint `clippy::unstable_as_slice` has been removed: ``Vec::as_slice` has been stabilized in 1.7`
|
||||
--> $DIR/deprecated.rs:1:8
|
||||
|
|
||||
LL | #[warn(clippy::str_to_string)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
LL | #[warn(clippy::unstable_as_slice)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 14 previous errors
|
||||
error: aborting due to 12 previous errors
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
#[warn(str_to_string)]
|
||||
#[warn(string_to_string)]
|
||||
#[warn(unstable_as_slice)]
|
||||
#[warn(unstable_as_mut_slice)]
|
||||
#[warn(misaligned_transmute)]
|
||||
|
|
|
@ -1,40 +1,28 @@
|
|||
error: lint `str_to_string` has been removed: `using `str::to_string` is common even today and specialization will likely happen soon`
|
||||
--> $DIR/deprecated_old.rs:1:8
|
||||
|
|
||||
LL | #[warn(str_to_string)]
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D renamed-and-removed-lints` implied by `-D warnings`
|
||||
|
||||
error: lint `string_to_string` has been removed: `using `string::to_string` is common even today and specialization will likely happen soon`
|
||||
--> $DIR/deprecated_old.rs:2:8
|
||||
|
|
||||
LL | #[warn(string_to_string)]
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
||||
error: lint `unstable_as_slice` has been removed: ``Vec::as_slice` has been stabilized in 1.7`
|
||||
--> $DIR/deprecated_old.rs:3:8
|
||||
--> $DIR/deprecated_old.rs:1:8
|
||||
|
|
||||
LL | #[warn(unstable_as_slice)]
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D renamed-and-removed-lints` implied by `-D warnings`
|
||||
|
||||
error: lint `unstable_as_mut_slice` has been removed: ``Vec::as_mut_slice` has been stabilized in 1.7`
|
||||
--> $DIR/deprecated_old.rs:4:8
|
||||
--> $DIR/deprecated_old.rs:2:8
|
||||
|
|
||||
LL | #[warn(unstable_as_mut_slice)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: lint `misaligned_transmute` has been removed: `this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr`
|
||||
--> $DIR/deprecated_old.rs:5:8
|
||||
--> $DIR/deprecated_old.rs:3:8
|
||||
|
|
||||
LL | #[warn(misaligned_transmute)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: lint `str_to_string` has been removed: `using `str::to_string` is common even today and specialization will likely happen soon`
|
||||
error: lint `unstable_as_slice` has been removed: ``Vec::as_slice` has been stabilized in 1.7`
|
||||
--> $DIR/deprecated_old.rs:1:8
|
||||
|
|
||||
LL | #[warn(str_to_string)]
|
||||
| ^^^^^^^^^^^^^
|
||||
LL | #[warn(unstable_as_slice)]
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
error: aborting due to 4 previous errors
|
||||
|
||||
|
|
7
tests/ui/str_to_string.rs
Normal file
7
tests/ui/str_to_string.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
#![warn(clippy::str_to_string)]
|
||||
|
||||
fn main() {
|
||||
let hello = "hello world".to_string();
|
||||
let msg = &hello[..];
|
||||
msg.to_string();
|
||||
}
|
19
tests/ui/str_to_string.stderr
Normal file
19
tests/ui/str_to_string.stderr
Normal file
|
@ -0,0 +1,19 @@
|
|||
error: `to_string()` called on a `&str`
|
||||
--> $DIR/str_to_string.rs:4:17
|
||||
|
|
||||
LL | let hello = "hello world".to_string();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::str-to-string` implied by `-D warnings`
|
||||
= help: consider using `.to_owned()`
|
||||
|
||||
error: `to_string()` called on a `&str`
|
||||
--> $DIR/str_to_string.rs:6:5
|
||||
|
|
||||
LL | msg.to_string();
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider using `.to_owned()`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
7
tests/ui/string_to_string.rs
Normal file
7
tests/ui/string_to_string.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
#![warn(clippy::string_to_string)]
|
||||
#![allow(clippy::redundant_clone)]
|
||||
|
||||
fn main() {
|
||||
let mut message = String::from("Hello");
|
||||
let mut v = message.to_string();
|
||||
}
|
11
tests/ui/string_to_string.stderr
Normal file
11
tests/ui/string_to_string.stderr
Normal file
|
@ -0,0 +1,11 @@
|
|||
error: `to_string()` called on a `String`
|
||||
--> $DIR/string_to_string.rs:6:17
|
||||
|
|
||||
LL | let mut v = message.to_string();
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::string-to-string` implied by `-D warnings`
|
||||
= help: consider using `.clone()`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
Loading…
Reference in a new issue