mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 15:11:30 +00:00
methods: lint against String.to_string (fixes #100)
This commit is contained in:
parent
6ff1e9a766
commit
4074c1f968
3 changed files with 14 additions and 1 deletions
|
@ -83,5 +83,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||
methods::OPTION_UNWRAP_USED,
|
||||
methods::RESULT_UNWRAP_USED,
|
||||
methods::STR_TO_STRING,
|
||||
methods::STRING_TO_STRING,
|
||||
]);
|
||||
}
|
||||
|
|
|
@ -13,10 +13,12 @@ declare_lint!(pub RESULT_UNWRAP_USED, Allow,
|
|||
"Warn on using unwrap() on a Result value");
|
||||
declare_lint!(pub STR_TO_STRING, Warn,
|
||||
"Warn when a String could use to_owned() instead of to_string()");
|
||||
declare_lint!(pub STRING_TO_STRING, Warn,
|
||||
"Warn when calling String.to_string()");
|
||||
|
||||
impl LintPass for MethodsPass {
|
||||
fn get_lints(&self) -> LintArray {
|
||||
lint_array!(OPTION_UNWRAP_USED, RESULT_UNWRAP_USED, STR_TO_STRING)
|
||||
lint_array!(OPTION_UNWRAP_USED, RESULT_UNWRAP_USED, STR_TO_STRING, STRING_TO_STRING)
|
||||
}
|
||||
|
||||
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
|
||||
|
@ -41,6 +43,12 @@ impl LintPass for MethodsPass {
|
|||
if let ty::TyStr = *obj_ty {
|
||||
span_lint(cx, STR_TO_STRING, expr.span, "`str.to_owned()` is faster");
|
||||
}
|
||||
else if let ty::TyStruct(did, _) = *obj_ty {
|
||||
if match_def_path(cx, did.did, &["collections", "string", "String"]) {
|
||||
span_lint(cx, STRING_TO_STRING, expr.span,
|
||||
"`String.to_string()` is a no-op")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,10 +2,14 @@
|
|||
#![plugin(clippy)]
|
||||
|
||||
#[deny(option_unwrap_used, result_unwrap_used)]
|
||||
#[deny(str_to_string, string_to_string)]
|
||||
fn main() {
|
||||
let opt = Some(0);
|
||||
let _ = opt.unwrap(); //~ERROR
|
||||
|
||||
let res: Result<i32, ()> = Ok(0);
|
||||
let _ = res.unwrap(); //~ERROR
|
||||
|
||||
let string = "str".to_string(); //~ERROR
|
||||
let _again = string.to_string(); //~ERROR
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue