Skip to content

Suggestion does not compile: use self with when implementing a trait for a generic type #4143

Closed
@lopopolo

Description

@lopopolo
$ cargo clippy -V
clippy 0.0.212 (fc96aa03 2019-05-04)

Clippy incorrectly suggests to use Self when referring to a generic type with a different T in a trait implementation.

impl TryFromMrb<Value> for Option<String> {
    type From = Ruby;
    type To = Rust;

    unsafe fn try_from_mrb(
        interp: &Mrb,
        value: Value,
    ) -> Result<Self, Error<Self::From, Self::To>> {
        // Clippy suggests replacing "Option" with "Self", which does not compile
        let value = <Option<Value>>::try_from_mrb(interp, value)?;
        if let Some(item) = value {
            Ok(Some(String::try_from_mrb(interp, item)?))
        } else {
            Ok(None)
        }
    }
}

Results in the following error

error: unnecessary structure name repetition
  --> mruby/src/convert/nilable/string.rs:36:22
   |
36 |         let value = <Option<Value>>::try_from_mrb(interp, value)?;
   |                      ^^^^^^ help: use the applicable keyword: `Self`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions