diff --git a/src/methods.rs b/src/methods.rs index a6200534e30e..2aa3b040e55a 100644 --- a/src/methods.rs +++ b/src/methods.rs @@ -173,8 +173,8 @@ declare_lint!(pub SEARCH_IS_SOME, Warn, impl LintPass for MethodsPass { fn get_lints(&self) -> LintArray { lint_array!(OPTION_UNWRAP_USED, RESULT_UNWRAP_USED, STR_TO_STRING, STRING_TO_STRING, - SHOULD_IMPLEMENT_TRAIT, WRONG_SELF_CONVENTION, OK_EXPECT, OPTION_MAP_UNWRAP_OR, - OPTION_MAP_UNWRAP_OR_ELSE) + SHOULD_IMPLEMENT_TRAIT, WRONG_SELF_CONVENTION, WRONG_PUB_SELF_CONVENTION, + OK_EXPECT, OPTION_MAP_UNWRAP_OR, OPTION_MAP_UNWRAP_OR_ELSE) } } @@ -258,14 +258,21 @@ impl LateLintPass for MethodsPass { fn lint_unwrap(cx: &LateContext, expr: &Expr, unwrap_args: &MethodArgs) { let (obj_ty, _) = walk_ptrs_ty_depth(cx.tcx.expr_ty(&unwrap_args[0])); - if match_type(cx, obj_ty, &OPTION_PATH) { - span_lint(cx, OPTION_UNWRAP_USED, expr.span, - "used unwrap() on an Option value. If you don't want to handle the None case \ - gracefully, consider using expect() to provide a better panic message"); + let mess = if match_type(cx, obj_ty, &OPTION_PATH) { + Some((OPTION_UNWRAP_USED, "an Option", "None")) } else if match_type(cx, obj_ty, &RESULT_PATH) { - span_lint(cx, RESULT_UNWRAP_USED, expr.span, - "used unwrap() on a Result value. Graceful handling of Err values is preferred"); + Some((RESULT_UNWRAP_USED, "a Result", "Err")) + } + else { + None + }; + + if let Some((lint, kind, none_value)) = mess { + span_lint(cx, lint, expr.span, + &format!("used unwrap() on {} value. If you don't want to handle the {} \ + case gracefully, consider using expect() to provide a better panic + message", kind, none_value)); } } diff --git a/tests/compile-fail/array_indexing.rs b/tests/compile-fail/array_indexing.rs old mode 100755 new mode 100644 diff --git a/tests/compile-fail/wrong_self_convention.rs b/tests/compile-fail/wrong_self_convention.rs new file mode 100644 index 000000000000..ca896a6b94b8 --- /dev/null +++ b/tests/compile-fail/wrong_self_convention.rs @@ -0,0 +1,45 @@ +#![feature(plugin)] +#![plugin(clippy)] + +#![deny(wrong_self_convention)] +#![deny(wrong_pub_self_convention)] +#![allow(dead_code)] + +fn main() {} + +#[derive(Clone, Copy)] +struct Foo; + +impl Foo { + + fn as_i32(self) {} + fn into_i32(self) {} + fn is_i32(self) {} + fn to_i32(self) {} + fn from_i32(self) {} //~ERROR: methods called `from_*` usually take no self + + pub fn as_i64(self) {} + pub fn into_i64(self) {} + pub fn is_i64(self) {} + pub fn to_i64(self) {} + pub fn from_i64(self) {} //~ERROR: methods called `from_*` usually take no self + +} + +struct Bar; + +impl Bar { + + fn as_i32(self) {} //~ERROR: methods called `as_*` usually take self by reference + fn into_i32(&self) {} //~ERROR: methods called `into_*` usually take self by value + fn is_i32(self) {} //~ERROR: methods called `is_*` usually take self by reference + fn to_i32(self) {} //~ERROR: methods called `to_*` usually take self by reference + fn from_i32(self) {} //~ERROR: methods called `from_*` usually take no self + + pub fn as_i64(self) {} //~ERROR: methods called `as_*` usually take self by reference + pub fn into_i64(&self) {} //~ERROR: methods called `into_*` usually take self by value + pub fn is_i64(self) {} //~ERROR: methods called `is_*` usually take self by reference + pub fn to_i64(self) {} //~ERROR: methods called `to_*` usually take self by reference + pub fn from_i64(self) {} //~ERROR: methods called `from_*` usually take no self + +}