Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion of match_as_ref does not compile #4437

Closed
ordovicia opened this issue Aug 22, 2019 · 4 comments · Fixed by #4446
Closed

Suggestion of match_as_ref does not compile #4437

ordovicia opened this issue Aug 22, 2019 · 4 comments · Fixed by #4446
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@ordovicia
Copy link
Contributor

Version: clippy 0.0.212 (e3cb40e 2019-06-25)

#[derive(Debug)]
struct E {
    source: Option<ParseIntError>,
}

impl Error for E {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        // Clippy emits `match_as_ref` warning on this `match`
        // and suggests using `Option::as_ref()`.
        match self.source {
            Some(ref s) => Some(s),
            None => None,
        }
    //  ^ help: try this: `self.source.as_ref()`
    
    // ... but using `as_ref()` causes E0308: mismatched types.
        
    //  self.source.as_ref()
//      ^^^^^^^^^^^^^^^^^^^^ expected trait std::error::Error, found struct `std::num::ParseIntError`
// 
// = note: expected type `std::option::Option<&(dyn std::error::Error + 'static)>`
//            found type `std::option::Option<&std::num::ParseIntError>`
    }
}

Playground

@tesuji
Copy link
Contributor

tesuji commented Aug 23, 2019

If you apply as_ref() suggestion and remove ref binding like this:

-            Some(ref s) => Some(s),
+            Some(s) => Some(s),

It will work.

@mati865
Copy link
Contributor

mati865 commented Aug 23, 2019

Clippy suggests to remove whole match block which does not compile. Nonetheless seems like a Rust bug in accepting:

match self.source.as_ref() {
            Some(s) => Some(s),
            None => None,
        }

but rejecting self.source.as_ref() without match.

@tesuji
Copy link
Contributor

tesuji commented Aug 23, 2019

Ah, I see it now.

@ordovicia
Copy link
Contributor Author

Thanks for the comments, all.

I realized that this is a Rust's bug or at least weird behavior (rust-lang/rust#53716).
As a workaround, manual coersion self.source.as_ref().map(|s| s as &dyn Error) works well (it causes allow-by-default trivial_casts lint though).

Then it would be better to suggest using map like above when the returned type of as_ref is to be coerced.

@ordovicia ordovicia changed the title False positive in match_as_ref Suggestion of match_as_ref does not compile Aug 23, 2019
@phansch phansch added the C-bug Category: Clippy is not doing the correct thing label Aug 25, 2019
bors added a commit that referenced this issue Aug 30, 2019
Fix `match_as_ref` bad suggestion

Fixes #4437

changelog: Fix `match_as_ref` bad suggestion
@bors bors closed this as completed in 23336ad Aug 30, 2019
bors added a commit that referenced this issue Aug 26, 2020
Fix `let_and_return` bad suggestion

Add a cast to the suggestion when the return expression has adjustments.
These adjustments are lost when the suggestion is applied.

This is similar to the problem in issue #4437.

Closes #5729

changelog: Fix `let_and_return` bad suggestion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants