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

match_same_arms does not take lifetimes into account #8919

Closed
hellow554 opened this issue May 31, 2022 · 5 comments
Closed

match_same_arms does not take lifetimes into account #8919

hellow554 opened this issue May 31, 2022 · 5 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@hellow554
Copy link
Contributor

hellow554 commented May 31, 2022

Summary

Taken from this real-world example: https://github.com/rust-lang/log/blob/7fb28c36c7a418912612ab37ab49bd4ca1a3a7f5/src/lib.rs#L812-L826

Lint Name

match_same_arms

Reproducer

I tried this code:

#![warn(clippy::match_same_arms)]

pub enum MaybeStaticStr<'a> {
    Static(&'static str),
    Borrowed(&'a str),
}

impl<'a> MaybeStaticStr<'a> {
    pub fn get(&self) -> &'a str {
        match *self {
            MaybeStaticStr::Static(s) => s,
            MaybeStaticStr::Borrowed(s) => s,
        }
    }
}

I saw this happen:

warning: this match arm has an identical body to another arm
  --> src/lib.rs:12:13
   |
12 |             MaybeStaticStr::Borrowed(s) => s,
   |             ---------------------------^^^^^
   |             |
   |             help: try merging the arm patterns: `MaybeStaticStr::Borrowed(s) | MaybeStaticStr::Static(s)`
   |
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
1  | #![warn(clippy::match_same_arms)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^
   = help: or try changing either arm body
note: other arm here
  --> src/lib.rs:11:13
   |
11 |             MaybeStaticStr::Static(s) => s,
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_same_arms

Problem is, that if I do that:

error[[E0495]](https://doc.rust-lang.org/stable/error-index.html#E0495): cannot infer an appropriate lifetime due to conflicting requirements
  --> src/lib.rs:10:15
   |
10 |         match *self {
   |               ^^^^^
   |
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
  --> src/lib.rs:8:6
   |
8  | impl<'a> MaybeStaticStr<'a> {
   |      ^^
note: ...so that the types are compatible
  --> src/lib.rs:10:15
   |
10 |         match *self {
   |               ^^^^^
   = note: expected `MaybeStaticStr<'_>`
              found `MaybeStaticStr<'a>`
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
  --> src/lib.rs:12:38
   |
12 |             MaybeStaticStr::Borrowed(s) => s,
   |                                      ^
   = note: expected `&'static str`
              found `&str`

Version

rustc 1.63.0-nightly (28b891916 2022-05-29)
binary: rustc
commit-hash: 28b891916d4c85cd10fb2e9cfa8bc836a2c459f3
commit-date: 2022-05-29
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.4

Additional Labels

@rustbot label +I-suggestion-causes-error

@hellow554 hellow554 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels May 31, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label May 31, 2022
@J-ZhengLi
Copy link
Member

@rustbot claim

@J-ZhengLi J-ZhengLi removed their assignment Dec 7, 2023
@J-ZhengLi
Copy link
Member

the suggestion does not cause error anymore shortly after this was posted...

I'm assuming it was fixed in this PR, as the error exists in nightly-2022-06-07 but not in nightly-2022-06-08.

So, I'm closing this for now, unless there is something that I missed when testing the suggested code here, you can always pointing it out and re-open this~

(Also sorry for the long wait, I forgot to state the reason of unassigning myself half a year ago 🤦‍♂️ , luckily this pops up as I'm now trying to re-evaluate all lifetime related issues~)

@hellow554
Copy link
Contributor Author

Should we add a test to clippy?

@J-ZhengLi
Copy link
Member

Should we add a test to clippy?

good idea 👍, do you want to submit a pr? it's totally fine if you don't~

@hellow554
Copy link
Contributor Author

Since I don't have the clippy repo on my pc right now, it would be easier if you could do it, please :)

bors added a commit that referenced this issue Jun 7, 2024
[`match_same_arms`]: add a test case with lifetimes

as reminded by: #8919

---

changelog: none
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 I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

3 participants