about summary refs log tree commit diff
path: root/src/librustc_resolve
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2019-04-18 11:35:11 -0700
committerEsteban Küber <esteban@kuber.com.ar>2019-04-19 10:13:45 -0700
commit4c4ca60edd9775873e555dbb5928b000bd734403 (patch)
tree36d0896b30261cfc9be79a761fc6c5a6fab55475 /src/librustc_resolve
parentf1be8d16c55990fff8c265352328fd90555feabd (diff)
downloadrust-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.rs93
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);