mv clippy suggestions

This commit is contained in:
jacremer 2021-10-04 21:40:26 -07:00
parent 80e7a8d594
commit 0ef0588e29

View File

@ -26,7 +26,7 @@ impl Command for Mv {
) )
.required( .required(
"destination", "destination",
SyntaxShape::Filepath, SyntaxShape::FilePath,
"the location to move files/directories to", "the location to move files/directories to",
) )
} }
@ -94,7 +94,7 @@ impl Command for Mv {
} }
} }
fn move_file(call: &Call, from: &PathBuf, to: &PathBuf) -> Result<(), ShellError> { fn move_file(call: &Call, from: &Path, to: &Path) -> Result<(), ShellError> {
if to.exists() && from.is_dir() && to.is_file() { if to.exists() && from.is_dir() && to.is_file() {
return Err(ShellError::MoveNotPossible { return Err(ShellError::MoveNotPossible {
source_message: "Can't move a directory".to_string(), source_message: "Can't move a directory".to_string(),
@ -114,7 +114,7 @@ fn move_file(call: &Call, from: &PathBuf, to: &PathBuf) -> Result<(), ShellError
return Err(ShellError::DirectoryNotFound(call.positional[1].span)); return Err(ShellError::DirectoryNotFound(call.positional[1].span));
} }
let mut to = to.clone(); let mut to = to.to_path_buf();
if to.is_dir() { if to.is_dir() {
let from_file_name = match from.file_name() { let from_file_name = match from.file_name() {
Some(name) => name, Some(name) => name,
@ -124,18 +124,16 @@ fn move_file(call: &Call, from: &PathBuf, to: &PathBuf) -> Result<(), ShellError
to.push(from_file_name); to.push(from_file_name);
} }
move_item(call, &from, &to) move_item(call, from, &to)
} }
fn move_item(call: &Call, from: &Path, to: &Path) -> Result<(), ShellError> { fn move_item(call: &Call, from: &Path, to: &Path) -> Result<(), ShellError> {
// We first try a rename, which is a quick operation. If that doesn't work, we'll try a copy // We first try a rename, which is a quick operation. If that doesn't work, we'll try a copy
// and remove the old file/folder. This is necessary if we're moving across filesystems or devices. // and remove the old file/folder. This is necessary if we're moving across filesystems or devices.
std::fs::rename(&from, &to).or_else(|_| { std::fs::rename(&from, &to).map_err(|_| ShellError::MoveNotPossible {
Err(ShellError::MoveNotPossible {
source_message: "failed to move".to_string(), source_message: "failed to move".to_string(),
source_span: call.positional[0].span, source_span: call.positional[0].span,
destination_message: "into".to_string(), destination_message: "into".to_string(),
destination_span: call.positional[1].span, destination_span: call.positional[1].span,
}) })
})
} }