From 0af727da91e7ff3c8ed5518cb7e005e8d4f939b0 Mon Sep 17 00:00:00 2001 From: John Renner Date: Mon, 27 Apr 2020 10:02:47 -0700 Subject: Validate the location of `crate` in paths --- crates/ra_syntax/src/validation.rs | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'crates/ra_syntax/src/validation.rs') diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 5e93895ec..a30bc97bb 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs @@ -96,6 +96,7 @@ pub(crate) fn validate(root: &SyntaxNode) -> Vec { ast::RecordField(it) => validate_numeric_name(it.name_ref(), &mut errors), ast::Visibility(it) => validate_visibility(it, &mut errors), ast::RangeExpr(it) => validate_range_expr(it, &mut errors), + ast::PathSegment(it) => validate_crate_keyword_in_path_segment(it, &mut errors), _ => (), } } @@ -222,3 +223,41 @@ fn validate_range_expr(expr: ast::RangeExpr, errors: &mut Vec) { )); } } + +fn validate_crate_keyword_in_path_segment( + segment: ast::PathSegment, + errors: &mut Vec, +) { + const ERR_MSG: &str = "The `crate` keyword is only allowed as the first segment of a path"; + + let crate_token = match segment.crate_token() { + None => return, + Some(it) => it, + }; + + // Disallow both ::crate and foo::crate + let path = segment.parent_path(); + if segment.coloncolon_token().is_some() || path.qualifier().is_some() { + errors.push(SyntaxError::new(ERR_MSG, crate_token.text_range())); + return; + } + + // We now know that the path variable describes a complete path. + // For expressions and types, validation is complete, but we still have + // to handle UseItems like this: + // use foo:{crate}; + // so we crawl upwards looking for any preceding paths on `UseTree`s + for node in path.syntax().ancestors().skip(1) { + match_ast! { + match node { + ast::UseTree(it) => if let Some(tree_path) = it.path() { + if tree_path != path { + errors.push(SyntaxError::new(ERR_MSG, crate_token.text_range())); + } + }, + ast::UseTreeList(_it) => continue, + _ => return, + } + }; + } +} -- cgit v1.2.3