Skip to content

Commit

Permalink
format #![feature(unsafe_attributes)]
Browse files Browse the repository at this point in the history
Our diff-check job was failing in part due to removing `unsafe` from any
`#[unsafe(attributes)]`. To prevent that I added a quick implementation
for this.
  • Loading branch information
ytmimi committed Jun 13, 2024
1 parent 8e80f8a commit 8abbcad
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,18 @@ impl Rewrite for ast::Attribute {

// 1 = `[`
let shape = shape.offset_left(prefix.len() + 1)?;
Some(
meta.rewrite(context, shape)
.map_or_else(|| snippet.to_owned(), |rw| format!("{}[{}]", prefix, rw)),
)
Some(meta.rewrite(context, shape).map_or_else(
|| snippet.to_owned(),
|rw| match &self.kind {
ast::AttrKind::Normal(normal_attr) => match normal_attr.item.unsafety {
// For #![feature(unsafe_attributes)]
// See https://github.com/rust-lang/rust/issues/123757
ast::Safety::Unsafe(_) => format!("{}[unsafe({})]", prefix, rw),
_ => format!("{}[{}]", prefix, rw),
},
_ => format!("{}[{}]", prefix, rw),
},
))
} else {
Some(snippet.to_owned())
}
Expand Down
34 changes: 34 additions & 0 deletions tests/target/unsafe_attributes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#![feature(unsafe_attributes)]
// https://github.com/rust-lang/rust/issues/123757
//
#![simple_ident]
#![simple::path]
#![simple_ident_expr = ""]
#![simple::path::Expr = ""]
#![simple_ident_tt(a b c)]
#![simple_ident_tt[a b c]]
#![simple_ident_tt{a b c}]
#![simple::path::tt(a b c)]
#![simple::path::tt[a b c]]
#![simple::path::tt{a b c}]
#![unsafe(simple_ident)]
#![unsafe(simple::path)]
#![unsafe(simple_ident_expr = "")]
#![unsafe(simple::path::Expr = "")]
#![unsafe(simple_ident_tt(a b c))]
#![unsafe(simple_ident_tt[a b c])]
#![unsafe(simple_ident_tt{a b c})]
#![unsafe(simple::path::tt(a b c))]
#![unsafe(simple::path::tt[a b c])]
#![unsafe(simple::path::tt{a b c})]
// I don't think `safe` attributes are a thing, but adding these formatting cases here just in case
#![safe(simple_ident)]
#![safe(simple::path)]
#![safe(simple_ident_expr = "")]
#![safe(simple::path::Expr = "")]
#![safe(simple_ident_tt(a b c))]
#![safe(simple_ident_tt[a b c])]
#![safe(simple_ident_tt{a b c})]
#![safe(simple::path::tt(a b c))]
#![safe(simple::path::tt[a b c])]
#![safe(simple::path::tt{a b c})]

0 comments on commit 8abbcad

Please sign in to comment.