From 2facd9517f0c58430514b9e4aa41f5996c994747 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 8 Dec 2020 13:47:58 +0100 Subject: Escape string literals in Attr::from_src --- crates/hir_def/src/attr.rs | 16 +++++----------- crates/hir_def/src/nameres/tests/mod_resolution.rs | 2 +- 2 files changed, 6 insertions(+), 12 deletions(-) (limited to 'crates/hir_def/src') diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index af3edc9df..12f4b02e2 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -166,16 +166,16 @@ impl Attrs { } pub fn docs(&self) -> Option { - let mut docs = String::new(); - self.by_key("doc") + let docs = self + .by_key("doc") .attrs() .flat_map(|attr| match attr.input.as_ref()? { AttrInput::Literal(s) => Some(s), AttrInput::TokenTree(_) => None, }) .intersperse(&SmolStr::new_inline("\n")) - // No FromIterator for String - .for_each(|s| docs.push_str(s.as_str())); + .map(|it| it.as_str()) + .collect::(); if docs.is_empty() { None } else { @@ -202,14 +202,8 @@ impl Attr { fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option { let path = ModPath::from_src(ast.path()?, hygiene)?; let input = if let Some(lit) = ast.literal() { - // FIXME: escape? let value = match lit.kind() { - ast::LiteralKind::String(string) if string.is_raw() => { - let text = string.text().as_str(); - let text = &text[string.text_range_between_quotes()? - - string.syntax().text_range().start()]; - text.into() - } + ast::LiteralKind::String(string) => string.value()?.into(), _ => lit.syntax().first_token()?.text().trim_matches('"').into(), }; Some(AttrInput::Literal(value)) diff --git a/crates/hir_def/src/nameres/tests/mod_resolution.rs b/crates/hir_def/src/nameres/tests/mod_resolution.rs index ef6f85e15..e80b593aa 100644 --- a/crates/hir_def/src/nameres/tests/mod_resolution.rs +++ b/crates/hir_def/src/nameres/tests/mod_resolution.rs @@ -372,7 +372,7 @@ fn module_resolution_explicit_path_mod_rs_with_win_separator() { check( r#" //- /main.rs -#[path = "module\bar\mod.rs"] +#[path = r"module\bar\mod.rs"] mod foo; //- /module/bar/mod.rs -- cgit v1.2.3