Skip to content

Commit

Permalink
Resolve branches_sharing_code clippy lint
Browse files Browse the repository at this point in the history
    warning: all if blocks contain the same code at the start
       --> src/error.rs:744:5
        |
    744 | /     if TypeId::of::<C>() == target {
    745 | |         let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();
        | |_____________________________________________________________________________^
        |
        = note: `#[warn(clippy::branches_sharing_code)]` on by default
        = warning: Some moved values might need to be renamed to avoid wrong references
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
    help: consider moving the start statements out like this
        |
    744 |     let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();
    745 |     if TypeId::of::<C>() == target {
        |

    warning: all if blocks contain the same code at the start
       --> src/error.rs:761:5
        |
    761 | /     if TypeId::of::<C>() == target {
    762 | |         let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut();
        | |_________________________________________________________________________________^
        |
        = warning: Some moved values might need to be renamed to avoid wrong references
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
    help: consider moving the start statements out like this
        |
    761 |     let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut();
    762 |     if TypeId::of::<C>() == target {
        |
  • Loading branch information
dtolnay committed Apr 9, 2021
1 parent 704622f commit aa6c83d
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,12 +741,11 @@ unsafe fn context_chain_downcast<C>(e: Ref<ErrorImpl>, target: TypeId) -> Option
where
C: 'static,
{
let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();
if TypeId::of::<C>() == target {
let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();
Some(Ref::new(&unerased._object.context).cast::<()>())
} else {
// Recurse down the context chain per the inner error's vtable.
let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();
let source = &unerased._object.error;
(vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target)
}
Expand All @@ -758,12 +757,11 @@ unsafe fn context_chain_downcast_mut<C>(e: Mut<ErrorImpl>, target: TypeId) -> Op
where
C: 'static,
{
let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut();
if TypeId::of::<C>() == target {
let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut();
Some(Mut::new(&mut unerased._object.context).cast::<()>())
} else {
// Recurse down the context chain per the inner error's vtable.
let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut();
let source = &mut unerased._object.error;
(vtable(source.inner.ptr).object_downcast_mut)(source.inner.by_mut(), target)
}
Expand Down

0 comments on commit aa6c83d

Please sign in to comment.