Skip to content

2024 edition migration of ref in patterns suboptimal: fixed up by clippy --fix #136047

Open
@VorpalBlade

Description

@VorpalBlade

The 2024 edition migration changed this:

impl TryFrom<&Selector> for konfigkoll_utils::line_edit::Selector {
    type Error = eyre::Error;

    fn try_from(value: &Selector) -> Result<Self, Self::Error> {
        match value {
            Selector::All => Ok(Self::All),
            Selector::Eof => Ok(Self::Eof),
            Selector::Line(n) => Ok(Self::Line(*n)),
            Selector::Range(a, b) => Ok(Self::Range(*a, *b)),
            Selector::Regex(r) => Ok(Self::Regex(Regex::new(r).wrap_err("invalid regex")?)),
            Selector::Function(ref f) => {
                let f = f.clone();
                Ok(Self::Function(Rc::new(move |lineno, s| {
                    let guard = f.borrow_mut().expect("Failed to borrow function object");
                    match guard.call::<_, bool>((lineno, s)) {
                        VmResult::Ok(v) => v,
                        VmResult::Err(e) => {
                            tracing::error!(
                                "Error in custom selector function {:?}: {:?}",
                                *guard,
                                e
                            );
                            false
                        }
                    }
                })))
            }
        }
    }
}

into this:

impl TryFrom<&Selector> for konfigkoll_utils::line_edit::Selector {
    type Error = eyre::Error;

    fn try_from(value: &Selector) -> Result<Self, Self::Error> {
        match value {
            Selector::All => Ok(Self::All),
            Selector::Eof => Ok(Self::Eof),
            Selector::Line(n) => Ok(Self::Line(*n)),
            Selector::Range(a, b) => Ok(Self::Range(*a, *b)),
            Selector::Regex(r) => Ok(Self::Regex(Regex::new(r).wrap_err("invalid regex")?)),
            &Selector::Function(ref f) => {
                let f = f.clone();
                Ok(Self::Function(Rc::new(move |lineno, s| {
                    let guard = f.borrow_mut().expect("Failed to borrow function object");
                    match guard.call::<_, bool>((lineno, s)) {
                        VmResult::Ok(v) => v,
                        VmResult::Err(e) => {
                            tracing::error!(
                                "Error in custom selector function {:?}: {:?}",
                                *guard,
                                e
                            );
                            false
                        }
                    }
                })))
            }
        }
    }
}

Then running cargo +beta clippy --fix changed it right back to Selector::Function(f).

When runing without --fix this is the clippy message:

warning: dereferencing a tuple pattern where every element takes a reference
   --> crates/konfigkoll_script/src/plugins/patch.rs:105:13
    |
105 |             &Selector::Function(ref f) => {
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrowed_reference
    = note: `#[warn(clippy::needless_borrowed_reference)]` on by default
help: try removing the `&` and `ref` parts
    |
105 -             &Selector::Function(ref f) => {
105 +             Selector::Function(f) => {
    |

warning: dereferencing a tuple pattern where every element takes a reference
   --> crates/konfigkoll_script/src/plugins/patch.rs:208:13
    |
208 |             &Action::Function(ref f) => {
    |             ^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrowed_reference
help: try removing the `&` and `ref` parts
    |
208 -             &Action::Function(ref f) => {
208 +             Action::Function(f) => {
    |

I expected to see this happen: A coherrent decision on what way is the proper way to write this. Rustc and clippy shouldn't fight each other. The edition migration shouldn't be sloppy and need to be fixed up by clippy.

Instead, this happened: Clippy has to clean up after rustc.

I have also pushed this to a branch in my repo, in case you need more context for investigating this: https://github.com/VorpalBlade/paketkoll/tree/feature/edition-2024 (the migration and the reversion are each in a separate commit). There seemed to be a few more instances of the same needless_borrowed_reference in other files too, but I think they are the same underlying cause.

Meta

rustc --version --verbose:

$ rustc +beta --version --verbose                   
rustc 1.85.0-beta.5 (441650704 2025-01-20)
binary: rustc
commit-hash: 44165070434aab38db1115448621880a35da00ff
commit-date: 2025-01-20
host: x86_64-unknown-linux-gnu
release: 1.85.0-beta.5
LLVM version: 19.1.7
Backtrace

Not relevant? Didn't crash after all...

Metadata

Metadata

Assignees

Labels

A-clippyArea: ClippyA-edition-2024Area: The 2024 editionA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.I-edition-triagedIssue: This issue has been reviewed and triaged by the Edition team.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions