Skip to content

Commit 5b6be5c

Browse files
committed
Change type to &str
1 parent 811c141 commit 5b6be5c

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/cli/self_update/windows.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,11 @@ pub(crate) fn wait_for_parent() -> Result<()> {
457457

458458
pub(crate) fn do_add_to_path(process: &Process) -> Result<()> {
459459
let new_path = _with_path_cargo_home_bin(_add_to_path, process)?;
460-
_apply_new_path(new_path)?;
460+
_apply_new_path(new_path.as_deref())?;
461461
do_add_to_programs(process)
462462
}
463463

464-
fn _apply_new_path(new_path: Option<String>) -> Result<()> {
464+
fn _apply_new_path(new_path: Option<&str>) -> Result<()> {
465465
use std::ptr;
466466
use windows_sys::Win32::Foundation::*;
467467
use windows_sys::Win32::UI::WindowsAndMessaging::{
@@ -478,7 +478,7 @@ fn _apply_new_path(new_path: Option<String>) -> Result<()> {
478478
if new_path.is_empty() {
479479
environment.remove_value("PATH")?;
480480
} else {
481-
environment.set_string("PATH", &new_path)?;
481+
environment.set_string("PATH", new_path)?;
482482
}
483483

484484
// Tell other processes to update their environment
@@ -515,22 +515,22 @@ fn get_windows_path_var() -> Result<Option<String>> {
515515

516516
// Returns None if the existing old_path does not need changing, otherwise
517517
// prepends the path_str to old_path, handling empty old_path appropriately.
518-
fn _add_to_path(old_path: String, path_str: String) -> Option<String> {
518+
fn _add_to_path(old_path: &str, path_str: &str) -> Option<String> {
519519
if old_path.is_empty() {
520-
Some(path_str)
521-
} else if old_path.contains(&path_str) {
520+
Some(path_str.to_owned())
521+
} else if old_path.contains(path_str) {
522522
None
523523
} else {
524-
let mut new_path = path_str;
524+
let mut new_path = path_str.to_owned();
525525
new_path.push(';');
526-
new_path += &old_path;
526+
new_path += old_path;
527527
Some(new_path)
528528
}
529529
}
530530

531531
// Returns None if the existing old_path does not need changing
532-
fn _remove_from_path(old_path: String, path_str: String) -> Option<String> {
533-
let idx = old_path.find(&path_str)?;
532+
fn _remove_from_path(old_path: &str, path_str: &str) -> Option<String> {
533+
let idx = old_path.find(path_str)?;
534534
// If there's a trailing semicolon (likely, since we probably added one
535535
// during install), include that in the substring to remove. We don't search
536536
// for that to find the string, because if it's the last string in the path,
@@ -552,17 +552,18 @@ fn _remove_from_path(old_path: String, path_str: String) -> Option<String> {
552552

553553
fn _with_path_cargo_home_bin<F>(f: F, process: &Process) -> Result<Option<String>>
554554
where
555-
F: FnOnce(String, String) -> Option<String>,
555+
F: FnOnce(&str, &str) -> Option<String>,
556556
{
557-
let windows_path = get_windows_path_var()?;
557+
let path = get_windows_path_var()?;
558+
let windows_path = path.as_deref();
558559
let mut path_str = process.cargo_home()?;
559560
path_str.push("bin");
560-
Ok(windows_path.and_then(|old_path| f(old_path, path_str.to_string_lossy().to_string())))
561+
Ok(windows_path.and_then(|old_path| f(old_path, &path_str.to_string_lossy())))
561562
}
562563

563564
pub(crate) fn do_remove_from_path(process: &Process) -> Result<()> {
564565
let new_path = _with_path_cargo_home_bin(_remove_from_path, process)?;
565-
_apply_new_path(new_path)?;
566+
_apply_new_path(new_path.as_deref())?;
566567
do_remove_from_programs()
567568
}
568569

@@ -760,8 +761,8 @@ mod tests {
760761
assert_eq!(
761762
None,
762763
super::_add_to_path(
763-
r"c:\users\example\.cargo\bin;foo".to_string(),
764-
r"c:\users\example\.cargo\bin".to_string()
764+
r"c:\users\example\.cargo\bin;foo",
765+
r"c:\users\example\.cargo\bin"
765766
)
766767
);
767768
}
@@ -796,7 +797,7 @@ mod tests {
796797
{
797798
// Can't compare the Results as Eq isn't derived; thanks error-chain.
798799
#![allow(clippy::unit_cmp)]
799-
assert_eq!((), super::_apply_new_path(Some("foo".to_string())).unwrap());
800+
assert_eq!((), super::_apply_new_path(Some("foo")).unwrap());
800801
}
801802
let environment = CURRENT_USER.create("Environment").unwrap();
802803
let path = environment.get_string("PATH").unwrap();
@@ -815,7 +816,7 @@ mod tests {
815816
{
816817
// Can't compare the Results as Eq isn't derived; thanks error-chain.
817818
#![allow(clippy::unit_cmp)]
818-
assert_eq!((), super::_apply_new_path(Some(String::new())).unwrap());
819+
assert_eq!((), super::_apply_new_path(Some("")).unwrap());
819820
}
820821
let reg_value = environment.get_string("PATH");
821822
match reg_value {
@@ -868,8 +869,8 @@ mod tests {
868869
assert_eq!(
869870
"foo",
870871
super::_remove_from_path(
871-
r"c:\users\example\.cargo\bin;foo".to_string(),
872-
r"c:\users\example\.cargo\bin".to_string(),
872+
r"c:\users\example\.cargo\bin;foo",
873+
r"c:\users\example\.cargo\bin",
873874
)
874875
.unwrap()
875876
)
@@ -880,8 +881,8 @@ mod tests {
880881
assert_eq!(
881882
"foo",
882883
super::_remove_from_path(
883-
r"foo;c:\users\example\.cargo\bin".to_string(),
884-
r"c:\users\example\.cargo\bin".to_string(),
884+
r"foo;c:\users\example\.cargo\bin",
885+
r"c:\users\example\.cargo\bin",
885886
)
886887
.unwrap()
887888
)

0 commit comments

Comments
 (0)