Skip to content

Commit

Permalink
add two lints to string module
Browse files Browse the repository at this point in the history
  • Loading branch information
PunitLodha committed Nov 25, 2020
1 parent 9e0fdb4 commit 9c9f365
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 117 deletions.
14 changes: 6 additions & 8 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,6 @@ mod shadow;
mod single_component_path_imports;
mod slow_vector_initialization;
mod stable_sort_primitive;
mod str_to_string;
mod string_to_string;
mod strings;
mod suspicious_trait_impl;
mod swap;
Expand Down Expand Up @@ -829,12 +827,12 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS,
&slow_vector_initialization::SLOW_VECTOR_INITIALIZATION,
&stable_sort_primitive::STABLE_SORT_PRIMITIVE,
&str_to_string::STR_TO_STRING,
&string_to_string::STRING_TO_STRING,
&strings::STRING_ADD,
&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,
Expand Down Expand Up @@ -1169,8 +1167,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 str_to_string::StrToString);
store.register_late_pass(|| box string_to_string::StringToString);
store.register_late_pass(|| box strings::StrToString);
store.register_late_pass(|| box strings::StringToString);


store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
Expand Down Expand Up @@ -1212,9 +1210,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&pattern_type_mismatch::PATTERN_TYPE_MISMATCH),
LintId::of(&shadow::SHADOW_REUSE),
LintId::of(&shadow::SHADOW_SAME),
LintId::of(&str_to_string::STR_TO_STRING),
LintId::of(&string_to_string::STRING_TO_STRING),
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),
Expand Down
53 changes: 0 additions & 53 deletions clippy_lints/src/str_to_string.rs

This file was deleted.

53 changes: 0 additions & 53 deletions clippy_lints/src/string_to_string.rs

This file was deleted.

98 changes: 97 additions & 1 deletion clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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! {
Expand Down Expand Up @@ -289,3 +290,98 @@ 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?** This does a multitude of things to just clone a string. Using `.to_owned()` is more specific.
///
/// **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()` does a multitude of things to just clone 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?** This does a multitude of things to just clone a string. Using `.clone()` is more specific.
///
/// **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()` does a multitude of things to just clone a `String`",
None,
"consider using `.clone()`",
);
}
}
}
}
4 changes: 2 additions & 2 deletions src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,7 @@ vec![
group: "restriction",
desc: "using `to_string()` on a `&str`, which should be `to_owned()`",
deprecation: None,
module: "str_to_string",
module: "strings",
},
Lint {
name: "string_add",
Expand Down Expand Up @@ -2284,7 +2284,7 @@ vec![
group: "restriction",
desc: "using `to_string()` on a `String`, which should be `clone()`",
deprecation: None,
module: "string_to_string",
module: "strings",
},
Lint {
name: "struct_excessive_bools",
Expand Down

0 comments on commit 9c9f365

Please sign in to comment.