Repository: sxyazi/yazi
File: yazi-core/src/mgr/commands/rename.rs (Rust)

Code before:
			if opt.force || !maybe_exists(&new).await || paths_to_same_file(&old, &new).await {
				Self::rename_do(tab, old, new).await.ok();
			} else if ConfirmProxy::show(ConfirmCfg::overwrite(&new)).await {
				Self::rename_do(tab, old, new).await.ok();
			}
		});
	}

	async fn rename_do(tab: Id, old: Url, new: Url) -> Result<()> {
		let Some((p_old, n_old)) = old.pair() else { return Ok(()) };
		let Some((p_new, n_new)) = new.pair() else { return Ok(()) };
		let _permit = WATCHER.acquire().await.unwrap();

		let overwritten = realname(&new).await;
		fs::rename(&old, &new).await?;

		if let Some(o) = overwritten {
			ok_or_not_found(fs::rename(p_new.join(&o), &new).await)?;
			FilesOp::Deleting(p_new.clone(), HashSet::from_iter([UrnBuf::from(o)])).emit();
		}

		let file = File::from(new.clone()).await?;
		if p_new == p_old {
			FilesOp::Upserting(p_old, HashMap::from_iter([(n_old, file)])).emit();
		} else {
			FilesOp::Deleting(p_old, HashSet::from_iter([n_old])).emit();
			FilesOp::Upserting(p_new, HashMap::from_iter([(n_new, file)])).emit();
		}

		TabProxy::reveal(&new);
		Pubsub::pub_from_rename(tab, &old, &new);
		Ok(())
	}

	fn empty_url_part(url: &Url, by: &str) -> String {
		if by == "all" {
			return String::new();
		}

		let ext = url.extension();
		match by {
			"stem" => ext.map_or_else(String::new, |s| format!(".{}", s.to_string_lossy())),
			"ext" if ext.is_some() => format!("{}.", url.file_stem().unwrap().to_string_lossy()),
			"dot_ext" if ext.is_some() => url.file_stem().unwrap().to_string_lossy().into_owned(),
			_ => url.name().to_string_lossy().into_owned(),
		}
	}
}


Review comment (suggestion):
[nitpick] Ensure consistent set initialization across rename operations to avoid potential mismatches in file identifiers.
```suggestion
			FilesOp::Deleting(p_old, [UrnBuf::from(n_old)].into()).emit();
```

Code after:
			if opt.force || !maybe_exists(&new).await || paths_to_same_file(&old, &new).await {
				Self::rename_do(tab, old, new).await.ok();
			} else if ConfirmProxy::show(ConfirmCfg::overwrite(&new)).await {
				Self::rename_do(tab, old, new).await.ok();
			}
		});
	}

	async fn rename_do(tab: Id, old: Url, new: Url) -> Result<()> {
		let Some((p_old, n_old)) = old.pair() else { return Ok(()) };
		let Some((p_new, n_new)) = new.pair() else { return Ok(()) };
		let _permit = WATCHER.acquire().await.unwrap();

		let overwritten = realname(&new).await;
		fs::rename(&old, &new).await?;

		if let Some(o) = overwritten {
			ok_or_not_found(fs::rename(p_new.join(&o), &new).await)?;
			FilesOp::Deleting(p_new.clone(), [UrnBuf::from(o)].into()).emit();
		}

		let file = File::new(new.clone()).await?;
		if p_new == p_old {
			FilesOp::Upserting(p_old, [(n_old, file)].into()).emit();
		} else {
			FilesOp::Deleting(p_old, [n_old].into()).emit();
			FilesOp::Upserting(p_new, [(n_new, file)].into()).emit();
		}

		TabProxy::reveal(&new);
		Pubsub::pub_from_rename(tab, &old, &new);
		Ok(())
	}

	fn empty_url_part(url: &Url, by: &str) -> String {
		if by == "all" {
			return String::new();
		}

		let ext = url.extension();
		match by {
			"stem" => ext.map_or_else(String::new, |s| format!(".{}", s.to_string_lossy())),
			"ext" if ext.is_some() => format!("{}.", url.file_stem().unwrap().to_string_lossy()),
			"dot_ext" if ext.is_some() => url.file_stem().unwrap().to_string_lossy().into_owned(),
			_ => url.name().to_string_lossy().into_owned(),
		}
	}
}