Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 92fdfb5

Browse files
committed
Make is_raw_identifier() public util function
1 parent 646f973 commit 92fdfb5

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

crates/hir-expand/src/name.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::fmt;
44

5-
use syntax::{ast, SmolStr, SyntaxKind};
5+
use syntax::{ast, utils::is_raw_identifier, SmolStr};
66

77
/// `Name` is a wrapper around string, which is used in hir for both references
88
/// and declarations. In theory, names should also carry hygiene info, but we are
@@ -33,11 +33,6 @@ impl fmt::Display for Name {
3333
}
3434
}
3535

36-
fn is_raw_identifier(name: &str) -> bool {
37-
let is_keyword = SyntaxKind::from_keyword(name).is_some();
38-
is_keyword && !matches!(name, "self" | "crate" | "super" | "Self")
39-
}
40-
4136
impl<'a> fmt::Display for UnescapedName<'a> {
4237
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4338
match &self.0 .0 {

crates/syntax/src/ast/make.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use itertools::Itertools;
1313
use stdx::{format_to, never};
1414

15-
use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxToken};
15+
use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
1616

1717
/// While the parent module defines basic atomic "constructors", the `ext`
1818
/// module defines shortcuts for common things.
@@ -111,8 +111,7 @@ pub fn name_ref(name_ref: &str) -> ast::NameRef {
111111
ast_from_text(&format!("fn f() {{ {raw_escape}{name_ref}; }}"))
112112
}
113113
fn raw_ident_esc(ident: &str) -> &'static str {
114-
let is_keyword = parser::SyntaxKind::from_keyword(ident).is_some();
115-
if is_keyword && !matches!(ident, "self" | "crate" | "super" | "Self") {
114+
if is_raw_identifier(ident) {
116115
"r#"
117116
} else {
118117
""

crates/syntax/src/utils.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use itertools::Itertools;
44

5-
use crate::{ast, match_ast, AstNode};
5+
use crate::{ast, match_ast, AstNode, SyntaxKind};
66

77
pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String {
88
path.syntax()
@@ -23,6 +23,11 @@ pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String {
2323
.join("::")
2424
}
2525

26+
pub fn is_raw_identifier(name: &str) -> bool {
27+
let is_keyword = SyntaxKind::from_keyword(name).is_some();
28+
is_keyword && !matches!(name, "self" | "crate" | "super" | "Self")
29+
}
30+
2631
#[cfg(test)]
2732
mod tests {
2833
use super::path_to_string_stripping_turbo_fish;

0 commit comments

Comments
 (0)