1
Fork 0

Use newtype enums instead of bool

This commit is contained in:
Esteban Kuber 2022-04-30 02:40:36 +00:00
parent 09f3ea1692
commit bce5ab2c78

View file

@ -123,7 +123,7 @@ impl<'a> Resolver<'a> {
let (span, found_use) = if let Some(def_id) = def_id.as_local() { let (span, found_use) = if let Some(def_id) = def_id.as_local() {
UsePlacementFinder::check(krate, self.def_id_to_node_id[def_id]) UsePlacementFinder::check(krate, self.def_id_to_node_id[def_id])
} else { } else {
(None, false) (None, FoundUse::No)
}; };
if !candidates.is_empty() { if !candidates.is_empty() {
show_candidates( show_candidates(
@ -132,9 +132,9 @@ impl<'a> Resolver<'a> {
&mut err, &mut err,
span, span,
&candidates, &candidates,
instead, if instead { Instead::Yes } else { Instead::No },
found_use, found_use,
false, IsPattern::No,
); );
} else if let Some((span, msg, sugg, appl)) = suggestion { } else if let Some((span, msg, sugg, appl)) = suggestion {
err.span_suggestion(span, msg, sugg, appl); err.span_suggestion(span, msg, sugg, appl);
@ -702,9 +702,9 @@ impl<'a> Resolver<'a> {
&mut err, &mut err,
Some(span), Some(span),
&import_suggestions, &import_suggestions,
false, Instead::No,
true, FoundUse::Yes,
true, IsPattern::Yes,
); );
} }
err err
@ -1482,9 +1482,9 @@ impl<'a> Resolver<'a> {
err, err,
None, None,
&import_suggestions, &import_suggestions,
false, Instead::No,
true, FoundUse::Yes,
false, IsPattern::No,
); );
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) { if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
@ -2420,6 +2420,27 @@ fn find_span_immediately_after_crate_name(
(next_left_bracket == after_second_colon, from_second_colon) (next_left_bracket == after_second_colon, from_second_colon)
} }
/// A suggestion has already been emitted, change the wording slightly to clarify that both are
/// independent options.
enum Instead {
Yes,
No,
}
/// Whether an existing place with an `use` item was found.
enum FoundUse {
Yes,
No,
}
/// Whether a binding is part of a pattern or an expression. Used for diagnostics.
enum IsPattern {
/// The binding is part of a pattern
Yes,
/// The binding is part of an expression
No,
}
/// When an entity with a given name is not available in scope, we search for /// When an entity with a given name is not available in scope, we search for
/// entities with that name in all crates. This method allows outputting the /// entities with that name in all crates. This method allows outputting the
/// results of this search in a programmer-friendly way /// results of this search in a programmer-friendly way
@ -2430,9 +2451,9 @@ fn show_candidates(
// This is `None` if all placement locations are inside expansions // This is `None` if all placement locations are inside expansions
use_placement_span: Option<Span>, use_placement_span: Option<Span>,
candidates: &[ImportSuggestion], candidates: &[ImportSuggestion],
instead: bool, instead: Instead,
found_use: bool, found_use: FoundUse,
is_pattern: bool, is_pattern: IsPattern,
) { ) {
if candidates.is_empty() { if candidates.is_empty() {
return; return;
@ -2465,8 +2486,8 @@ fn show_candidates(
("one of these", "items", String::new()) ("one of these", "items", String::new())
}; };
let instead = if instead { " instead" } else { "" }; let instead = if let Instead::Yes = instead { " instead" } else { "" };
let mut msg = if is_pattern { let mut msg = if let IsPattern::Yes = is_pattern {
format!( format!(
"if you meant to match on {}{}{}, use the full path in the pattern", "if you meant to match on {}{}{}, use the full path in the pattern",
kind, instead, name kind, instead, name
@ -2479,7 +2500,7 @@ fn show_candidates(
err.note(note); err.note(note);
} }
if let (true, Some(span)) = (is_pattern, use_placement_span) { if let (IsPattern::Yes, Some(span)) = (is_pattern, use_placement_span) {
err.span_suggestions( err.span_suggestions(
span, span,
&msg, &msg,
@ -2490,7 +2511,7 @@ fn show_candidates(
for candidate in &mut accessible_path_strings { for candidate in &mut accessible_path_strings {
// produce an additional newline to separate the new use statement // produce an additional newline to separate the new use statement
// from the directly following item. // from the directly following item.
let additional_newline = if found_use { "" } else { "\n" }; let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
candidate.0 = format!("use {};\n{}", &candidate.0, additional_newline); candidate.0 = format!("use {};\n{}", &candidate.0, additional_newline);
} }
@ -2513,7 +2534,8 @@ fn show_candidates(
} else { } else {
assert!(!inaccessible_path_strings.is_empty()); assert!(!inaccessible_path_strings.is_empty());
let prefix = if is_pattern { "you might have meant to match on " } else { "" }; let prefix =
if let IsPattern::Yes = is_pattern { "you might have meant to match on " } else { "" };
if inaccessible_path_strings.len() == 1 { if inaccessible_path_strings.len() == 1 {
let (name, descr, def_id, note) = &inaccessible_path_strings[0]; let (name, descr, def_id, note) = &inaccessible_path_strings[0];
let msg = format!( let msg = format!(
@ -2521,7 +2543,7 @@ fn show_candidates(
prefix, prefix,
descr, descr,
name, name,
if is_pattern { ", which" } else { "" } if let IsPattern::Yes = is_pattern { ", which" } else { "" }
); );
if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) { if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) {
@ -2589,14 +2611,14 @@ struct UsePlacementFinder {
} }
impl UsePlacementFinder { impl UsePlacementFinder {
fn check(krate: &Crate, target_module: NodeId) -> (Option<Span>, bool) { fn check(krate: &Crate, target_module: NodeId) -> (Option<Span>, FoundUse) {
let mut finder = let mut finder =
UsePlacementFinder { target_module, first_legal_span: None, first_use_span: None }; UsePlacementFinder { target_module, first_legal_span: None, first_use_span: None };
finder.visit_crate(krate); finder.visit_crate(krate);
if let Some(use_span) = finder.first_use_span { if let Some(use_span) = finder.first_use_span {
(Some(use_span), true) (Some(use_span), FoundUse::Yes)
} else { } else {
(finder.first_legal_span, false) (finder.first_legal_span, FoundUse::No)
} }
} }
} }