Escape string literals in Attr::from_src

This commit is contained in:
Lukas Wirth 2020-12-08 13:47:58 +01:00
parent 7a338e5207
commit 2facd9517f
2 changed files with 6 additions and 12 deletions

View file

@ -166,16 +166,16 @@ impl Attrs {
} }
pub fn docs(&self) -> Option<Documentation> { pub fn docs(&self) -> Option<Documentation> {
let mut docs = String::new(); let docs = self
self.by_key("doc") .by_key("doc")
.attrs() .attrs()
.flat_map(|attr| match attr.input.as_ref()? { .flat_map(|attr| match attr.input.as_ref()? {
AttrInput::Literal(s) => Some(s), AttrInput::Literal(s) => Some(s),
AttrInput::TokenTree(_) => None, AttrInput::TokenTree(_) => None,
}) })
.intersperse(&SmolStr::new_inline("\n")) .intersperse(&SmolStr::new_inline("\n"))
// No FromIterator<SmolStr> for String .map(|it| it.as_str())
.for_each(|s| docs.push_str(s.as_str())); .collect::<String>();
if docs.is_empty() { if docs.is_empty() {
None None
} else { } else {
@ -202,14 +202,8 @@ impl Attr {
fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option<Attr> { fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option<Attr> {
let path = ModPath::from_src(ast.path()?, hygiene)?; let path = ModPath::from_src(ast.path()?, hygiene)?;
let input = if let Some(lit) = ast.literal() { let input = if let Some(lit) = ast.literal() {
// FIXME: escape?
let value = match lit.kind() { let value = match lit.kind() {
ast::LiteralKind::String(string) if string.is_raw() => { ast::LiteralKind::String(string) => string.value()?.into(),
let text = string.text().as_str();
let text = &text[string.text_range_between_quotes()?
- string.syntax().text_range().start()];
text.into()
}
_ => lit.syntax().first_token()?.text().trim_matches('"').into(), _ => lit.syntax().first_token()?.text().trim_matches('"').into(),
}; };
Some(AttrInput::Literal(value)) Some(AttrInput::Literal(value))

View file

@ -372,7 +372,7 @@ fn module_resolution_explicit_path_mod_rs_with_win_separator() {
check( check(
r#" r#"
//- /main.rs //- /main.rs
#[path = "module\bar\mod.rs"] #[path = r"module\bar\mod.rs"]
mod foo; mod foo;
//- /module/bar/mod.rs //- /module/bar/mod.rs