Remove unnecessary Box::leak

This commit is contained in:
Cameron Cordes
2020-08-27 16:09:09 -04:00
parent fd96c9ff8f
commit eaf9994cd4
2 changed files with 11 additions and 13 deletions

View File

@@ -46,7 +46,7 @@ impl Config {
.arg( .arg(
Arg::with_name("only match") Arg::with_name("only match")
.short("o") .short("o")
.help("Only print matched text") .help("Only print matched text"),
) )
.get_matches(); .get_matches();

View File

@@ -3,7 +3,7 @@ use rack::filematch::*;
use std::fs::{self, DirEntry, File}; use std::fs::{self, DirEntry, File};
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::Path; use std::path::{Path, PathBuf};
extern crate term; extern crate term;
@@ -12,13 +12,14 @@ fn main() {
let search_path = &config.path; let search_path = &config.path;
let path = if search_path.starts_with('/') { let path = if search_path.starts_with('/') {
Path::new(search_path) PathBuf::from(search_path)
} else { } else {
let p: &'static str = Box::leak(Box::new(String::from("./") + search_path)); let mut path = String::from("./");
Path::new(p) path.push_str(search_path);
PathBuf::from(path)
}; };
let results = traverse_dir(&config, path, Option::None, |config, path| { let results = traverse_dir(&config, &path, Option::None, |config, path| {
check_file(config, path) check_file(config, path)
}); });
match results { match results {
@@ -30,7 +31,7 @@ fn main() {
fn traverse_dir( fn traverse_dir(
config: &Config, config: &Config,
dir: &Path, dir: &PathBuf,
gitignore_entries: Option<Vec<String>>, gitignore_entries: Option<Vec<String>>,
operation: fn(&Config, &Path) -> io::Result<FileMatch>, operation: fn(&Config, &Path) -> io::Result<FileMatch>,
) -> io::Result<Vec<FileMatch>> { ) -> io::Result<Vec<FileMatch>> {
@@ -71,18 +72,15 @@ fn traverse_dir(
// TODO: Should borrow gitignore instead of cloning.. // TODO: Should borrow gitignore instead of cloning..
let mut results = let mut results =
traverse_dir(&config, &entry.path(), Some(gitignore.clone()), operation)?; traverse_dir(&config, &entry.path(), Some(gitignore.clone()), operation)?;
let has_results = results let has_results = results.iter().filter(|mat| !mat.matches.is_empty()).count() > 0;
.iter()
.filter(|mat| !mat.matches.is_empty())
.count()
> 0;
if has_results { if has_results {
matches.append(&mut results); matches.append(&mut results);
} }
} else { } else {
if config.use_gitignore { if config.use_gitignore {
let should_ignore: Option<&String> = gitignore.iter().find(|&line| path.contains(&line.clone())); let should_ignore: Option<&String> =
gitignore.iter().find(|&line| path.contains(&line.clone()));
if !should_ignore.unwrap_or(&String::from("")).is_empty() if !should_ignore.unwrap_or(&String::from("")).is_empty()
|| entry.path().to_str().unwrap().contains("/.git") || entry.path().to_str().unwrap().contains("/.git")