Skip to content

match_same_arms does not take lifetimes into account #8919

Closed
@hellow554

Description

@hellow554

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions