aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-12-08 12:47:58 +0000
committerLukas Wirth <[email protected]>2020-12-08 12:47:58 +0000
commit2facd9517f0c58430514b9e4aa41f5996c994747 (patch)
tree38b6790b6e7c422a8989f158a2f1b80ee1e1dc81 /crates/hir_def/src
parent7a338e520729d5198fb233c6d94d33f1ae365b24 (diff)
Escape string literals in Attr::from_src
Diffstat (limited to 'crates/hir_def/src')
-rw-r--r--crates/hir_def/src/attr.rs16
-rw-r--r--crates/hir_def/src/nameres/tests/mod_resolution.rs2
2 files changed, 6 insertions, 12 deletions
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 {
166 } 166 }
167 167
168 pub fn docs(&self) -> Option<Documentation> { 168 pub fn docs(&self) -> Option<Documentation> {
169 let mut docs = String::new(); 169 let docs = self
170 self.by_key("doc") 170 .by_key("doc")
171 .attrs() 171 .attrs()
172 .flat_map(|attr| match attr.input.as_ref()? { 172 .flat_map(|attr| match attr.input.as_ref()? {
173 AttrInput::Literal(s) => Some(s), 173 AttrInput::Literal(s) => Some(s),
174 AttrInput::TokenTree(_) => None, 174 AttrInput::TokenTree(_) => None,
175 }) 175 })
176 .intersperse(&SmolStr::new_inline("\n")) 176 .intersperse(&SmolStr::new_inline("\n"))
177 // No FromIterator<SmolStr> for String 177 .map(|it| it.as_str())
178 .for_each(|s| docs.push_str(s.as_str())); 178 .collect::<String>();
179 if docs.is_empty() { 179 if docs.is_empty() {
180 None 180 None
181 } else { 181 } else {
@@ -202,14 +202,8 @@ impl Attr {
202 fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option<Attr> { 202 fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option<Attr> {
203 let path = ModPath::from_src(ast.path()?, hygiene)?; 203 let path = ModPath::from_src(ast.path()?, hygiene)?;
204 let input = if let Some(lit) = ast.literal() { 204 let input = if let Some(lit) = ast.literal() {
205 // FIXME: escape?
206 let value = match lit.kind() { 205 let value = match lit.kind() {
207 ast::LiteralKind::String(string) if string.is_raw() => { 206 ast::LiteralKind::String(string) => string.value()?.into(),
208 let text = string.text().as_str();
209 let text = &text[string.text_range_between_quotes()?
210 - string.syntax().text_range().start()];
211 text.into()
212 }
213 _ => lit.syntax().first_token()?.text().trim_matches('"').into(), 207 _ => lit.syntax().first_token()?.text().trim_matches('"').into(),
214 }; 208 };
215 Some(AttrInput::Literal(value)) 209 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() {
372 check( 372 check(
373 r#" 373 r#"
374//- /main.rs 374//- /main.rs
375#[path = "module\bar\mod.rs"] 375#[path = r"module\bar\mod.rs"]
376mod foo; 376mod foo;
377 377
378//- /module/bar/mod.rs 378//- /module/bar/mod.rs