Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,22 @@ impl<'a> ParsedAttribute<'a> {

pub fn new(raw: &'a str) -> Self {
let raw_trimmed = raw.trim();
let raw_without_closing = raw_trimmed.strip_suffix(']').unwrap_or_else(|| {
panic!(
"\
String `{raw_trimmed}` cannot be parsed as an attribute \
because it is not closed with a square bracket."
)
});

// Temporarily accept attrs without a closing `]` due to a bug in Rust 1.94:
// https://github.com/rust-lang/rust/pull/153465
//
// TODO: Restore the version below that requires a closing `]`
// once rustdoc stops emitting malformed attrs
// like `#[doc(test(attr(deny(rust_2018_idioms))))`.
let raw_without_closing = raw_trimmed.strip_suffix(']').unwrap_or(raw_trimmed);

// let raw_without_closing = raw_trimmed.strip_suffix(']').unwrap_or_else(|| {
// panic!(
// "\
// String `{raw_trimmed}` cannot be parsed as an attribute \
// because it is not closed with a square bracket."
// )
// });

if let Some(raw_content) = raw_without_closing.strip_prefix("#[") {
ParsedAttribute {
Expand Down Expand Up @@ -816,6 +825,17 @@ mod tests {
)
}

#[test]
fn attribute_missing_final_square_bracket_is_tolerated() {
let malformed = "#[doc(test(attr(deny(rust_2018_idioms))))";
let repaired = "#[doc(test(attr(deny(rust_2018_idioms))))]";

let attribute = ParsedAttribute::new(malformed);

assert_eq!(attribute, ParsedAttribute::new(repaired));
assert_eq!(attribute.raw_attribute(), repaired);
}

#[test]
fn attribute_meta_item_custom_brackets() {
for raw_attribute in ["macro{arg1,arg2}", "macro[arg1,arg2]"] {
Expand Down
Loading