Skip to content

Add warning if nothing was searched #1762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions crates/core/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ fn search(args: &Args) -> Result<bool> {
let mut stats = args.stats()?;
let mut searcher = args.search_worker(args.stdout())?;
let mut matched = false;
let mut searched = false;

for result in args.walker()? {
let subject = match subject_builder.build_from_result(result) {
Some(subject) => subject,
None => continue,
};
searched = true;
let search_result = match searcher.search(&subject) {
Ok(search_result) => search_result,
Err(err) => {
Expand All @@ -108,6 +110,9 @@ fn search(args: &Args) -> Result<bool> {
break;
}
}
if !searched {
eprint_nothing_searched();
}
if let Some(ref stats) = stats {
let elapsed = Instant::now().duration_since(started_at);
// We don't care if we couldn't print this successfully.
Expand All @@ -129,11 +134,13 @@ fn search_parallel(args: &Args) -> Result<bool> {
let bufwtr = args.buffer_writer()?;
let stats = args.stats()?.map(Mutex::new);
let matched = AtomicBool::new(false);
let searched = AtomicBool::new(false);
let mut searcher_err = None;
args.walker_parallel()?.run(|| {
let bufwtr = &bufwtr;
let stats = &stats;
let matched = &matched;
let searched = &searched;
let subject_builder = &subject_builder;
let mut searcher = match args.search_worker(bufwtr.buffer()) {
Ok(searcher) => searcher,
Expand All @@ -148,6 +155,7 @@ fn search_parallel(args: &Args) -> Result<bool> {
Some(subject) => subject,
None => return WalkState::Continue,
};
searched.store(true, SeqCst);
searcher.printer().get_mut().clear();
let search_result = match searcher.search(&subject) {
Ok(search_result) => search_result,
Expand Down Expand Up @@ -178,6 +186,9 @@ fn search_parallel(args: &Args) -> Result<bool> {
}
})
});
if !searched.load(SeqCst) {
eprint_nothing_searched();
}
if let Some(err) = searcher_err.take() {
return Err(err);
}
Expand All @@ -191,6 +202,14 @@ fn search_parallel(args: &Args) -> Result<bool> {
Ok(matched.load(SeqCst))
}

fn eprint_nothing_searched() {
eprintln!(
"No files were searched, which means ripgrep probably \
applied a filter you didn't expect. \
Try running again with --debug."
);
}

/// The top-level entry point for listing files without searching them. This
/// recursively steps through the file list (current directory by default) and
/// prints each path sequentially using a single thread.
Expand Down