| 112 | } |
| 113 | |
| 114 | fn justified(mut node: Node<'_>, context: &Context<'_>, safety: bool) -> bool { |
| 115 | let explains = |node| { |
| 116 | let text = tokens::text(node, context.source); |
| 117 | let marker = if safety { "SAFETY:" } else { "Reason:" }; |
| 118 | tokens::comment(node) |
| 119 | && text |
| 120 | .split_once(marker) |
| 121 | .is_some_and(|(_, reason)| reason.trim().len() >= 8) |
| 122 | }; |
| 123 | // Support a comment inside the unsafe block or immediately before its |
| 124 | // enclosing statement. Strings containing SAFETY are never comments. |
| 125 | if code_comment_inside(node, &explains) { |
| 126 | return true; |
| 127 | } |
| 128 | for _ in 0..4 { |
| 129 | if node.prev_named_sibling().is_some_and(explains) { |
| 130 | return true; |
| 131 | } |
| 132 | let Some(parent) = node.parent() else { |
| 133 | break; |
| 134 | }; |
| 135 | if matches!(parent.kind(), "function_item" | "source_file" | "block") { |
| 136 | break; |
| 137 | } |
| 138 | node = parent; |
| 139 | } |
| 140 | false |
| 141 | } |
| 142 | |
| 143 | fn code_comment_inside<'tree>(node: Node<'tree>, explains: &impl Fn(Node<'tree>) -> bool) -> bool { |
| 144 | // Only direct comments in the unsafe body count, not a nested operation's |