Repository navigation
2024 edition migration of ref in patterns suboptimal: fixed up by clippy --fix #136047
Description
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.7Backtrace
Not relevant? Didn't crash after all...
Activity
VorpalBlade commentedon Jan 25, 2025
@rustbot modify labels: A-edition-2024
Nadrieril commentedon Jan 25, 2025
That's surprising indeed, the lint shouldn't trigger on that. What the type of the payload of the
Functionvariant?VorpalBlade commentedon Jan 25, 2025
The type here is:
However, this also happens separately with the function variant of this other enum as well:
As well as this change in
save.rs(that clippy also undoes):Where
commentis a&CompactStringaccording to the Rust Analyzer inlay hint. So the matched type is(Option<PackageRef>, &Option<CompactString>). WherePackageRefis a newtype aroundlasso::SpurandCompactStringis from thecompact_strcrate.And in the third case (
state.rs) I have this type, and the second member of the Apply variant is affected (the Arc is not).I would suggest you take a look at the whole diff of VorpalBlade/paketkoll@7ec3d44 to see what clippy changed back as there are several instances of this, with different types. I don't know which ones should be considered separate bugs and which ones should be the same. They all look related to me.
Nadrieril commentedon Jan 26, 2025
I see two issues at play here:
Some(ref comment)=>Some(comment)instead of =>&Some(ref comment).Selector::Function(ref f)? If it wasref fthen the lint is working as intended (if stupid).VorpalBlade commentedon Jan 26, 2025
Ah, yes it seems you are right looking at VorpalBlade/paketkoll@a715c59. There was an extra ref there. So your point 2 is indeed not an issue.
Point 1 remains. Clippy shouldn't have to clean up from the migration being sloppy.
When I'm not on mobile I will edit the title and the original post to clarify that clippy didn't actually revert, just clean up.
[-]2024 edition migration immediately undone by clippy --fix[/-][+]2024 edition migration of `ref` in patterns suboptimal: fixed up by clippy --fix[/+]traviscross commentedon Jan 28, 2025
See also:
needless_borrowed_referencerust-clippy#13616cc @dianne @Manishearth @Centri3 @flip1995
ehuss commentedon Jan 28, 2025
Does anyone know how difficult it would be to make rust_2024_incompatible_pat smarter so that it recommends the more concise syntax?
Manishearth commentedon Jan 28, 2025
FWIW, the original design for editions had two-step "edition lint" + "idiom lint" phases. The first phase took code that compiled on the old edition and turned it into code that compiled on both editions, but was potentially not fully idiomatic. The second phase took code that compiled on both editions and cleaned it up into code that may only compile in the newest edition.
This was quite necessary since edition migrations aren't always perfect, and it's not great for the codebase to be in a situation where it's partially migrated but no longer compiles on either edition so you can't easily migrate further
So while it's possible that in this specific case the migration could have been smarter, in general Clippy "fixing up after the edition" is fine. Ideally these fixups would be idiom lints, but it's not too bad for Clippy to do it.
dianne commentedon Jan 28, 2025
I don't think it'd be too difficult. I've written something very similar for another diagnostic. I'll see what I can do.
@rustbot claim
Rollup merge of rust-lang#136577 - dianne:simple-pat-migration-simpli…
Rollup merge of rust-lang#136577 - dianne:simple-pat-migration-simpli…
Unrolled build for rust-lang#136577