"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) {
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")
+ }
+ }
}
}
}
#![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
}