Merge pull request #539 from mcarton/small-fix

Small fix
This commit is contained in:
Manish Goregaokar 2016-01-03 20:24:16 +05:30
commit 822a35ab9f
3 changed files with 60 additions and 8 deletions

View file

@ -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));
}
}

0
tests/compile-fail/array_indexing.rs Executable file → Normal file
View file

View file

@ -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
}