diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2019-04-18 11:35:11 -0700 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2019-04-19 10:13:45 -0700 |
| commit | 4c4ca60edd9775873e555dbb5928b000bd734403 (patch) | |
| tree | 36d0896b30261cfc9be79a761fc6c5a6fab55475 /src/librustc_resolve | |
| parent | f1be8d16c55990fff8c265352328fd90555feabd (diff) | |
| download | rust-4c4ca60edd9775873e555dbb5928b000bd734403.tar.gz rust-4c4ca60edd9775873e555dbb5928b000bd734403.zip | |
remove duplicated code and simplify logic
Diffstat (limited to 'src/librustc_resolve')
| -rw-r--r-- | src/librustc_resolve/diagnostics.rs | 93 |
1 files changed, 34 insertions, 59 deletions
diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index b7deb546882..c89c222ad57 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -328,7 +328,38 @@ impl<'a> Resolver<'a> { _ => false, }; - let (followed_by_brace, closing_brace) = self.followed_by_brace(span); + let mut bad_struct_syntax_suggestion = || { + let (followed_by_brace, closing_brace) = self.followed_by_brace(span); + let mut suggested = false; + match source { + PathSource::Expr(Some(parent)) => { + suggested = path_sep(err, &parent); + } + PathSource::Expr(None) if followed_by_brace == true => { + if let Some((sp, snippet)) = closing_brace { + err.span_suggestion( + sp, + "surround the struct literal with parenthesis", + format!("({})", snippet), + Applicability::MaybeIncorrect, + ); + } else { + err.span_label( + span, // Note the parenthesis surrounding the suggestion below + format!("did you mean `({} {{ /* fields */ }})`?", path_str), + ); + } + suggested = true; + }, + _ => {} + } + if !suggested { + err.span_label( + span, + format!("did you mean `{} {{ /* fields */ }}`?", path_str), + ); + } + }; match (def, source) { (Def::Macro(..), _) => { @@ -383,69 +414,13 @@ impl<'a> Resolver<'a> { ); } } else { - match source { - PathSource::Expr(Some(parent)) => if !path_sep(err, &parent) { - err.span_label( - span, - format!("did you mean `{} {{ /* fields */ }}`?", path_str), - ); - } - PathSource::Expr(None) if followed_by_brace == true => { - if let Some((sp, snippet)) = closing_brace { - err.span_suggestion( - sp, - "surround the struct literal with parenthesis", - format!("({})", snippet), - Applicability::MaybeIncorrect, - ); - } else { - err.span_label( - span, - format!("did you mean `({} {{ /* fields */ }})`?", path_str), - ); - } - }, - _ => { - err.span_label( - span, - format!("did you mean `{} {{ /* fields */ }}`?", path_str), - ); - }, - } + bad_struct_syntax_suggestion(); } } (Def::Union(..), _) | (Def::Variant(..), _) | (Def::Ctor(_, _, CtorKind::Fictive), _) if ns == ValueNS => { - match source { - PathSource::Expr(Some(parent)) => if !path_sep(err, &parent) { - err.span_label( - span, - format!("did you mean `{} {{ /* fields */ }}`?", path_str), - ); - } - PathSource::Expr(None) if followed_by_brace == true => { - if let Some((sp, snippet)) = closing_brace { - err.span_suggestion( - sp, - "surround the struct literal with parenthesis", - format!("({})", snippet), - Applicability::MaybeIncorrect, - ); - } else { - err.span_label( - span, - format!("did you mean `({} {{ /* fields */ }})`?", path_str), - ); - } - }, - _ => { - err.span_label( - span, - format!("did you mean `{} {{ /* fields */ }}`?", path_str), - ); - }, - } + bad_struct_syntax_suggestion(); } (Def::SelfTy(..), _) if ns == ValueNS => { err.span_label(span, fallback_label); |
