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

emit lint when using ref with match ergonomics #105773

Open
lcnr opened this issue Dec 16, 2022 · 7 comments
Open

emit lint when using ref with match ergonomics #105773

lcnr opened this issue Dec 16, 2022 · 7 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lcnr
Copy link
Contributor

lcnr commented Dec 16, 2022

fn foo(x: &Option<u32>) -> Option<&u32> {
    match x {
        Some(ref inner) => Some(inner),
        None => None,
    }
}

because of match ergonomics, every binding inside of Some is already matched by ref implicitly, so the additional explicit ref is completely redundant. Consider the following example where l and r have the exact same type.

#![feature(type_name_of_val)]
use std::any::type_name_of_val;
fn foo(x: &Option<(u32, u32)>) {
    match x {
        Some((l, ref r)) => assert_eq!(
            type_name_of_val(l),
            type_name_of_val(r),
        ),
        None => unreachable!(),
    }
}

fn main() {
    foo(&Some((3, 3)));
}

This lint should either suggest to disable match ergonomics by adding & and &mut patterns in the relevant places or to remove the ref.

There are a lot of places even inside of rustc which hit this pattern, so this lint feels really desirable to me.

@lcnr lcnr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 16, 2022
@matthiaskrgr
Copy link
Member

could also be a clippy lint 🙃

@lcnr
Copy link
Contributor Author

lcnr commented Dec 16, 2022

i feel like i encounter this often enough that having it in rustc itself would be nice. That lint should be part of the unused category, similar to the lint for unused braces.

@est31
Copy link
Member

est31 commented Dec 17, 2022

clippy's needless_borrow lint already does this, at least partially, e.g. rust-lang/rust-clippy#9049 .

Generally the "this code can use new feature X" lints are included in clippy. Note that there is code out there that has really old MSRV requirements. clippy has a mechanism to handle such MSRV requirements, including cargo integration, but rustc lacks the ability to turn on lints based on MSRV support.

See also the thread of #87512 and why it was rejected.

@est31
Copy link
Member

est31 commented Dec 17, 2022

Note that I'm not saying that rustc should never get such lints, ever. It's just a problem that should be addressed before such a lint is added.

For example the MSRV problem could be solved by turning on lints based on the edition. So it could enable version based lints only on editions where the first rustc version of the edition already had the feature the lint is suggesting. There are mechanisms for editions already, and editions also influence how rust code should be written. clippy defaults to MSRV == latest, which is obviously way too aggressive.

@lcnr
Copy link
Contributor Author

lcnr commented May 3, 2023

Generally the "this code can use new feature X" lints are included in clippy. Note that there is code out there that has really old MSRV requirements. clippy has a mechanism to handle such MSRV requirements, including cargo integration, but rustc lacks the ability to turn on lints based on MSRV support.

My proposed lint is not a "this code can use new feature X", it is "this code either does not compile or is redundant" afaik.

If you have match ergonomics together with ref, then the code just didn't compile before match ergonomics were stable because you were missing some explicit & somewhere: https://rust.godbolt.org/z/7q67hqbna

@lcnr lcnr added the E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. label May 3, 2023
@isabelleatkins
Copy link

@rustbot claim

@isabelleatkins
Copy link

@rustbot release-assignment

@lcnr lcnr removed the E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. label May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants