Prevent suggestions from being emitted if all possible locations are inside expansions

This commit is contained in:
Oliver Schneider 2017-09-01 11:14:04 +02:00
parent 74748b11bb
commit f0e7a5b8e5
No known key found for this signature in database
GPG Key ID: A69F8D225B3AD7D9

View File

@ -631,7 +631,6 @@ fn visit_mod(
},
}
}
assert!(self.span.is_some(), "a file can't have no items and emit suggestions");
}
}
@ -3562,8 +3561,7 @@ fn report_with_use_injections(&mut self, krate: &Crate) {
};
visit::walk_crate(&mut finder, krate);
if !candidates.is_empty() {
let span = finder.span.expect("did not find module");
show_candidates(&mut err, span, &candidates, better, finder.found_use);
show_candidates(&mut err, finder.span, &candidates, better, finder.found_use);
}
err.emit();
}
@ -3757,7 +3755,8 @@ fn import_candidate_to_paths(suggestion: &ImportSuggestion) -> (Span, String, St
/// entities with that name in all crates. This method allows outputting the
/// results of this search in a programmer-friendly way
fn show_candidates(err: &mut DiagnosticBuilder,
span: Span,
// This is `None` if all placement locations are inside expansions
span: Option<Span>,
candidates: &[ImportSuggestion],
better: bool,
found_use: bool) {
@ -3775,18 +3774,27 @@ fn show_candidates(err: &mut DiagnosticBuilder,
};
let msg = format!("possible {}candidate{} into scope", better, msg_diff);
for candidate in &mut path_strings {
// produce an additional newline to separate the new use statement
// from the directly following item.
let additional_newline = if found_use {
""
} else {
"\n"
};
*candidate = format!("use {};\n{}", candidate, additional_newline);
}
if let Some(span) = span {
for candidate in &mut path_strings {
// produce an additional newline to separate the new use statement
// from the directly following item.
let additional_newline = if found_use {
""
} else {
"\n"
};
*candidate = format!("use {};\n{}", candidate, additional_newline);
}
err.span_suggestions(span, &msg, path_strings);
err.span_suggestions(span, &msg, path_strings);
} else {
let mut msg = msg;
msg.push(':');
for candidate in path_strings {
msg.push('\n');
msg.push_str(&candidate);
}
}
}
/// A somewhat inefficient routine to obtain the name of a module.