use crate::lang_helpers::case_arms::pattern_is_bare_underscore;
use crate::role_table;
use crate::roles::control_flow::else_if_parent_clause_gate;
use crate::{Ancestors, Node, Rust};
else_if_parent_clause_gate!(Rust, ElseClause);
fn is_decision_arm<'a>(node: &Node<'a>, _code: &[u8], _ancestors: Ancestors<'a, '_>) -> bool {
!node
.child_by_field_name("pattern")
.is_some_and(|pat| pattern_is_bare_underscore(&pat, Rust::UNDERSCORE as u16))
}
fn has_alternative(node: &Node<'_>, _code: &[u8]) -> bool {
node.child_by_field_name("alternative").is_some()
}
fn not_in_type_syntax<'a>(node: &Node<'a>, _code: &[u8], ancestors: Ancestors<'a, '_>) -> bool {
!ancestors.parent(node).is_some_and(|parent| {
matches!(
parent.kind_id().into(),
Rust::ImplItem | Rust::HigherRankedTraitBound | Rust::ForLifetimes | Rust::AbstractType
)
})
}
fn has_guard(node: &Node<'_>, _code: &[u8]) -> bool {
node.child_by_field_name("condition").is_some()
}
fn has_doc_marker_child(node: &Node<'_>, _: &[u8]) -> bool {
node.child_count() > 2 && node.is_child(Rust::DocComment as u16)
}
role_table! {
lang: Rust;
Call { owns [CallExpression] }
StringLit { owns [StringLiteral, RawStringLiteral] }
Comparison {
owns [BinaryExpression(holds_payer)]
pays [
EQEQ, BANGEQ, LTEQ, GTEQ,
LT under BinaryExpression, GT under BinaryExpression,
]
project "operator"
}
ShortCircuit {
owns [
BinaryExpression(holds_payer),
LetChain, LetChain2,
]
pays [AMPAMP, PIPEPIPE]
project "operator"
}
EagerLogical {
owns []
excludes [
AMP "a boolean `&` is the integral `&`'s kind, which the grammar does not tell apart",
PIPE "a boolean `|` is the integral `|`'s kind, which the grammar does not tell apart",
CARET "a boolean `^` is the integral `^`'s kind, which the grammar does not tell apart",
]
}
Ternary { absent "Rust has no ternary operator; `if` is an expression" }
NullSafe { absent "Rust has no optional navigation or null coalescing; `?` is an exit" }
CaseArm { owns [MatchArm(gated_by is_decision_arm), MatchArm2(gated_by is_decision_arm)] }
Guard {
owns [MatchPattern(gated_by has_guard no_chain)]
pays [If under MatchPattern]
}
Branch {
owns [IfExpression, LetDeclaration(pays_itself gated_by has_alternative no_chain)]
pays [If]
}
Switch { owns [MatchExpression] }
Loop {
owns [ForExpression, WhileExpression, LoopExpression]
pays [For gated_by not_in_type_syntax, While, Loop]
}
Catch { absent "Rust has no exception handling; `?` is an exit and `match` on a `Result` a switch" }
ElseIf { owns [IfExpression(gated_by continues_else_clause)] pays [] }
Exit { owns [ReturnExpression, TryExpression] }
Modifier {
owns [Pub, MutableSpecifier, Const, Static, Unsafe, Async, Extern, Move, Dyn]
}
DeclKeyword { owns [Fn, Let, Enum, Struct, Union, Trait, Impl, Use, Mod, Type, Where] }
Primitive {
owns [
PrimitiveType, PrimitiveType2, PrimitiveType3, PrimitiveType4, PrimitiveType5,
PrimitiveType6, PrimitiveType7, PrimitiveType8, PrimitiveType9, PrimitiveType10,
PrimitiveType11, PrimitiveType12, PrimitiveType13, PrimitiveType14, PrimitiveType15,
PrimitiveType16, PrimitiveType17,
]
}
FuncOpener { owns [FunctionItem(callable opens Function), ClosureExpression(opens Function)] }
Closure { owns [ClosureExpression] }
Container { owns [SourceFile(opens Unit), ImplItem(opens Impl), TraitItem(opens Trait)] }
Member { owns [FieldDeclaration, ConstItem, StaticItem, AssociatedType] }
Comment { owns [LineComment, BlockComment] }
DocComment {
owns [
LineComment(gated_by has_doc_marker_child no_chain),
BlockComment(gated_by has_doc_marker_child no_chain),
]
}
Docstring { absent "Rust documents with `///` comments, not string literals" }
DocTarget {
owns [
ModItem,
StructItem,
UnionItem,
EnumItem,
EnumVariant,
FieldDeclaration,
ConstItem,
StaticItem,
TypeItem,
FunctionItem,
FunctionSignatureItem,
ImplItem,
TraitItem,
AssociatedType,
UseDeclaration,
ExternCrateDeclaration,
MacroDefinition,
]
}
DocRunOpener { absent "rustdoc attaches every doc comment to an item; none documents by its own content" }
DocRunDirective { absent "rustdoc reads no directive comment" }
DocBodyResidual { absent "only the callables and closures hold a doc comment in a body; the trait default" }
}