Repository: sxyazi/yazi
File: yazi-fs/src/path.rs (Rust)

Code before:

#[inline]
pub fn expand_url<'a>(url: impl Into<Cow<'a, Url>>) -> Cow<'a, Url> {
	let cow: Cow<'a, Url> = url.into();
	match _expand_url(&cow) {
		Cow::Borrowed(_) => cow,
		Cow::Owned(url) => url.into(),
	}
}

fn _expand_url(url: &Url) -> Cow<'_, Url> {
	// ${HOME} or $HOME
	#[cfg(unix)]
	let re = regex::bytes::Regex::new(r"\$(?:\{([^}]+)\}|([a-zA-Z\d_]+))").unwrap();

	// %USERPROFILE%
	#[cfg(windows)]
	let re = regex::bytes::Regex::new(r"%([^%]+)%").unwrap();

	let b = url.loc.as_os_str().as_encoded_bytes();
	let local = !url.scheme.is_virtual();

	// Windows paths that only have a drive letter but no root, e.g. "D:"
	#[cfg(windows)]
	if local && b.len() == 2 && b[1] == b':' && b[0].is_ascii_alphabetic() {
		url.with(format!(r"{}:\", b[0].to_ascii_uppercase() as char)).into();
	}

	let b = re.replace_all(b, |caps: &regex::bytes::Captures| {
		let name = caps.get(2).or_else(|| caps.get(1)).unwrap();
		str::from_utf8(name.as_bytes())
			.ok()
			.and_then(env::var_os)
			.map_or_else(|| caps.get(0).unwrap().as_bytes().to_owned(), |s| s.into_encoded_bytes())
	});
	if matches!(b, Cow::Borrowed(_)) {
		return url.into();
	}

	let p = unsafe { PathBuf::from(OsString::from_encoded_bytes_unchecked(b.into_owned())) };
	if let Some(rest) = p.strip_prefix("~").ok().filter(|_| local) {
		url.with(clean_path(&dirs::home_dir().unwrap_or_default().join(rest))).into()
	} else if p.is_absolute() {
		url.with(clean_path(&p)).into()
	} else {
		clean_url(CWD.load().join(p))
	}
}

pub fn skip_url(url: &Url, n: usize) -> Cow<'_, OsStr> {
	let mut it = url.components();

Review comment (suggestion):
This line creates a new URL but doesn't return it. The semicolon should be removed and this should be a return statement.
```suggestion
		return url.with(format!(r"{}:\", b[0].to_ascii_uppercase() as char)).into();
```

Code after:

#[inline]
pub fn expand_url<'a>(url: impl Into<Cow<'a, Url>>) -> Cow<'a, Url> {
	let cow: Cow<'a, Url> = url.into();
	match _expand_url(&cow) {
		Cow::Borrowed(_) => cow,
		Cow::Owned(url) => url.into(),
	}
}

fn _expand_url(url: &Url) -> Cow<'_, Url> {
	// ${HOME} or $HOME
	#[cfg(unix)]
	let re = regex::bytes::Regex::new(r"\$(?:\{([^}]+)\}|([a-zA-Z\d_]+))").unwrap();

	// %USERPROFILE%
	#[cfg(windows)]
	let re = regex::bytes::Regex::new(r"%([^%]+)%").unwrap();

	let b = url.loc.as_os_str().as_encoded_bytes();
	let local = !url.scheme.is_virtual();

	// Windows paths that only have a drive letter but no root, e.g. "D:"
	#[cfg(windows)]
	if local && b.len() == 2 && b[1] == b':' && b[0].is_ascii_alphabetic() {
		return url.with(format!(r"{}:\", b[0].to_ascii_uppercase() as char)).into();
	}

	let b = re.replace_all(b, |caps: &regex::bytes::Captures| {
		let name = caps.get(2).or_else(|| caps.get(1)).unwrap();
		str::from_utf8(name.as_bytes())
			.ok()
			.and_then(env::var_os)
			.map_or_else(|| caps.get(0).unwrap().as_bytes().to_owned(), |s| s.into_encoded_bytes())
	});

	let path: Cow<_> = unsafe {
		match b {
			Cow::Borrowed(b) => Path::new(OsStr::from_encoded_bytes_unchecked(b)).into(),
			Cow::Owned(b) => PathBuf::from(OsString::from_encoded_bytes_unchecked(b)).into(),
		}
	};

	if let Some(rest) = path.strip_prefix("~").ok().filter(|_| local) {
		url.with(clean_path(&dirs::home_dir().unwrap_or_default().join(rest))).into()
	} else if path.is_absolute() {
		url.with(clean_path(&path)).into()
	} else {
		clean_url(CWD.load().join(path))
	}
}