Skip to content

Commit 9f68e47

Browse files
rchen152facebook-github-bot
authored andcommitted
Remove remaining mut variables from class_metadata_of
Summary: Tweaks calculations of bases, is_final, and total_ordering_metadata so that the variables no longer need to be `mut`. Note that this is a small behavioral change for `total_ordering_metadata` - we previously grabbed the range of the last `total_ordering` decorator; now we grab the range of the first. I don't think this will matter in practice. Reviewed By: grievejia Differential Revision: D80372921 fbshipit-source-id: ce25ccf7bd85de763170f7bb64febca736b9d8b6
1 parent 453c7ef commit 9f68e47

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

pyrefly/lib/alt/class/class_metadata.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,15 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
105105
let decorators = decorators.map(|(decorator_key, decorator_range)| {
106106
(self.get_idx(*decorator_key), *decorator_range)
107107
});
108-
let mut bases: Vec<BaseClass> = bases.to_vec();
109-
if let Some(special_base) = special_base {
110-
bases.push((**special_base).clone());
111-
}
108+
let bases = if let Some(special_base) = special_base {
109+
bases
110+
.iter()
111+
.chain([(**special_base).clone()].iter())
112+
.cloned()
113+
.collect()
114+
} else {
115+
bases.to_vec()
116+
};
112117
let initial_protocol_metadata = Self::initial_protocol_metadata(cls, bases.as_slice());
113118
let has_generic_base_class = bases.iter().any(|x| x.is_generic());
114119
let has_typed_dict_base_class = bases.iter().any(|x| x.is_typed_dict());
@@ -200,22 +205,20 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
200205
);
201206
}
202207
}
203-
let mut is_final = false;
204-
let mut total_ordering_metadata = None;
205-
for (decorator, decorator_range) in decorators.iter() {
206-
let decorator_ty = decorator.ty();
207-
match decorator_ty.callee_kind() {
208-
Some(CalleeKind::Function(FunctionKind::Final)) => {
209-
is_final = true;
210-
}
211-
Some(CalleeKind::Function(FunctionKind::TotalOrdering)) => {
212-
total_ordering_metadata = Some(TotalOrderingMetadata {
208+
let is_final = decorators.iter().any(|(decorator, _)| {
209+
decorator.ty().callee_kind() == Some(CalleeKind::Function(FunctionKind::Final))
210+
});
211+
let total_ordering_metadata = decorators.iter().find_map(|(decorator, decorator_range)| {
212+
decorator.ty().callee_kind().and_then(|kind| {
213+
if kind == CalleeKind::Function(FunctionKind::TotalOrdering) {
214+
Some(TotalOrderingMetadata {
213215
location: *decorator_range,
214-
});
216+
})
217+
} else {
218+
None
215219
}
216-
_ => {}
217-
}
218-
}
220+
})
221+
});
219222
// If this class inherits from a dataclass_transform-ed class, record the defaults that we
220223
// should use for dataclass parameters.
221224
let dataclass_defaults_from_base_class = bases_with_metadata

0 commit comments

Comments
 (0)