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

Another false positive: match_same_arms vs. &Any #1218

Open
Tracked by #12046
llogiq opened this issue Sep 7, 2016 · 6 comments
Open
Tracked by #12046

Another false positive: match_same_arms vs. &Any #1218

llogiq opened this issue Sep 7, 2016 · 6 comments
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. E-needs-mcve Call for participation: This issue needs a Minimal Complete and Verifiable Example I-false-positive Issue: The lint was triggered on code it shouldn't have L-correctness Lint: Belongs in the correctness lint group T-middle Type: Probably requires verifiying types

Comments

@llogiq
Copy link
Contributor

llogiq commented Sep 7, 2016

If the two arms do not have the same actual type, pulling them together with | will fail. The lint should not trigger if the actual types of the match arms differ.

E.g.

enum Foo<'a> {
    Bar(&'a FooBar),
    Baz(&'a Buzz),
}

let x: &Any = match foo {
    Bar(ref bar) => bar,
    Baz(ref bar) => bar,
}

Changing it to

let x: &Any = match foo {
    Bar(ref bar) |
    Baz(ref bar) => bar,
}

will fail with a type error.

@llogiq llogiq added C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-middle Type: Probably requires verifiying types L-correctness Lint: Belongs in the correctness lint group labels Sep 7, 2016
@mcarton
Copy link
Member

mcarton commented Sep 7, 2016

Weird, I'm pretty sure types are checked exactly for that kind of things.

@mcarton
Copy link
Member

mcarton commented Sep 7, 2016

Found it: https://github.com/Manishearth/rust-clippy/blob/master/tests/compile-fail/copies.rs#L273-L277

(took me much longer than I like to admit to find something I wrote 😅)

@llogiq
Copy link
Contributor Author

llogiq commented Sep 7, 2016

There's a difference in that different types are usually not returned from the match – in fact the type system usually doesn't permit it. However there's the &Any escape hatch, which your test does not appear to cover; your bar(T) function is generic, not dynamic (I'm not sure if the problem lies with the dynamic dispatch or with returning a ref to a potentially unsized value).

@mcarton
Copy link
Member

mcarton commented Sep 7, 2016

Yup, I was just pointing out the insufficient test.

@mcarton
Copy link
Member

mcarton commented Oct 24, 2016

@llogiq can you provide a MCVE? I can't reproduce.

@phansch phansch added E-needs-mcve Call for participation: This issue needs a Minimal Complete and Verifiable Example I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 21, 2020
@m-rph
Copy link
Contributor

m-rph commented Dec 29, 2023

This is the closest I could get to reproducing it maybe I am missing something?

use std::any::*;

#[derive(Debug)]
struct FooBar;
#[derive(Debug)]
struct Buzz;

enum Foo<'a> {
    Bar(&'a FooBar),
    Baz(&'a Buzz),
}


fn main() {
    let fb = FooBar{};
    let _ = Foo::Baz(&Buzz{});
    let x: &dyn Any = match Foo::Bar(&fb) {
        Foo::Bar(bar) => bar as &dyn Any,
        Foo::Baz(bar) => bar as &dyn Any,
    };
    println!("{:?}", x);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b9738d5a54c5980a3e97727b6a6e8310

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 E-medium Call for participation: Medium difficulty level problem and requires some initial experience. E-needs-mcve Call for participation: This issue needs a Minimal Complete and Verifiable Example I-false-positive Issue: The lint was triggered on code it shouldn't have L-correctness Lint: Belongs in the correctness lint group T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

4 participants