Fix false negative with OK_EXPECT

This commit is contained in:
mcarton 2016-02-13 01:38:55 +01:00
parent 17b7b413f7
commit 7f567ce1d1
2 changed files with 6 additions and 23 deletions

View file

@ -274,7 +274,6 @@ declare_lint! {
/// println!("{:p} {:p}",*y, z); // prints out the same pointer
/// }
/// ```
///
declare_lint! {
pub CLONE_DOUBLE_REF, Warn, "using `clone` on `&&T`"
}
@ -789,26 +788,12 @@ fn get_error_type<'a>(cx: &LateContext, ty: ty::Ty<'a>) -> Option<ty::Ty<'a>> {
None
}
/// This checks whether a given type is known to implement Debug. It's
/// conservative, i.e. it should not return false positives, but will return
/// false negatives.
/// This checks whether a given type is known to implement Debug.
fn has_debug_impl<'a, 'b>(ty: ty::Ty<'a>, cx: &LateContext<'b, 'a>) -> bool {
let no_ref_ty = walk_ptrs_ty(ty);
let debug = match cx.tcx.lang_items.debug_trait() {
Some(debug) => debug,
None => return false,
};
let debug_def = cx.tcx.lookup_trait_def(debug);
let mut debug_impl_exists = false;
debug_def.for_each_relevant_impl(cx.tcx, no_ref_ty, |d| {
let self_ty = &cx.tcx.impl_trait_ref(d).and_then(|im| im.substs.self_ty());
if let Some(self_ty) = *self_ty {
if !self_ty.flags.get().contains(ty::TypeFlags::HAS_PARAMS) {
debug_impl_exists = true;
}
}
});
debug_impl_exists
match cx.tcx.lang_items.debug_trait() {
Some(debug) => implements_trait(cx, ty, debug, Some(vec![])),
None => false,
}
}
#[cfg_attr(rustfmt, rustfmt_skip)]

View file

@ -274,10 +274,8 @@ fn main() {
// the error type implements `Debug`
let res2: Result<i32, MyError> = Ok(0);
res2.ok().expect("oh noes!");
// we currently don't warn if the error type has a type parameter
// (but it would be nice if we did)
let res3: Result<u32, MyErrorWithParam<u8>>= Ok(0);
res3.ok().expect("whoof");
res3.ok().expect("whoof"); //~ERROR called `ok().expect()`
let res4: Result<u32, io::Error> = Ok(0);
res4.ok().expect("argh"); //~ERROR called `ok().expect()`
let res5: io::Result<u32> = Ok(0);