[ty] remove any_over_type

This commit is contained in:
Carl Meyer 2025-07-02 11:41:58 -07:00
parent f76d3f87cf
commit ca0e578afa
No known key found for this signature in database
GPG key ID: 2D1FB7916A52E121
4 changed files with 3 additions and 142 deletions

View file

@ -412,14 +412,6 @@ impl<'db> PropertyInstanceType<'db> {
self.setter(db).map(|ty| ty.materialize(db, variance)), self.setter(db).map(|ty| ty.materialize(db, variance)),
) )
} }
fn any_over_type(self, db: &'db dyn Db, type_fn: &dyn Fn(Type<'db>) -> bool) -> bool {
self.getter(db)
.is_some_and(|ty| ty.any_over_type(db, type_fn))
|| self
.setter(db)
.is_some_and(|ty| ty.any_over_type(db, type_fn))
}
} }
bitflags! { bitflags! {
@ -753,110 +745,6 @@ impl<'db> Type<'db> {
} }
} }
/// Return `true` if `self`, or any of the types contained in `self`, match the closure passed in.
pub fn any_over_type(self, db: &'db dyn Db, type_fn: &dyn Fn(Type<'db>) -> bool) -> bool {
if type_fn(self) {
return true;
}
match self {
Self::AlwaysFalsy
| Self::AlwaysTruthy
| Self::Never
| Self::BooleanLiteral(_)
| Self::BytesLiteral(_)
| Self::ModuleLiteral(_)
| Self::FunctionLiteral(_)
| Self::ClassLiteral(_)
| Self::SpecialForm(_)
| Self::KnownInstance(_)
| Self::StringLiteral(_)
| Self::IntLiteral(_)
| Self::LiteralString
| Self::Dynamic(_)
| Self::BoundMethod(_)
| Self::WrapperDescriptor(_)
| Self::MethodWrapper(_)
| Self::DataclassDecorator(_)
| Self::DataclassTransformer(_) => false,
Self::GenericAlias(generic) => generic
.specialization(db)
.types(db)
.iter()
.copied()
.any(|ty| ty.any_over_type(db, type_fn)),
Self::Callable(callable) => {
let signatures = callable.signatures(db);
signatures.iter().any(|signature| {
signature.parameters().iter().any(|param| {
param
.annotated_type()
.is_some_and(|ty| ty.any_over_type(db, type_fn))
}) || signature
.return_ty
.is_some_and(|ty| ty.any_over_type(db, type_fn))
})
}
Self::SubclassOf(subclass_of) => {
Type::from(subclass_of.subclass_of()).any_over_type(db, type_fn)
}
Self::TypeVar(typevar) => match typevar.bound_or_constraints(db) {
None => false,
Some(TypeVarBoundOrConstraints::UpperBound(bound)) => {
bound.any_over_type(db, type_fn)
}
Some(TypeVarBoundOrConstraints::Constraints(constraints)) => constraints
.elements(db)
.iter()
.any(|constraint| constraint.any_over_type(db, type_fn)),
},
Self::BoundSuper(bound_super) => {
Type::from(bound_super.pivot_class(db)).any_over_type(db, type_fn)
|| Type::from(bound_super.owner(db)).any_over_type(db, type_fn)
}
Self::Tuple(tuple) => tuple
.tuple(db)
.all_elements()
.any(|ty| ty.any_over_type(db, type_fn)),
Self::Union(union) => union
.elements(db)
.iter()
.any(|ty| ty.any_over_type(db, type_fn)),
Self::Intersection(intersection) => {
intersection
.positive(db)
.iter()
.any(|ty| ty.any_over_type(db, type_fn))
|| intersection
.negative(db)
.iter()
.any(|ty| ty.any_over_type(db, type_fn))
}
Self::ProtocolInstance(protocol) => protocol.any_over_type(db, type_fn),
Self::PropertyInstance(property) => property.any_over_type(db, type_fn),
Self::NominalInstance(instance) => match instance.class {
ClassType::NonGeneric(_) => false,
ClassType::Generic(generic) => generic
.specialization(db)
.types(db)
.iter()
.any(|ty| ty.any_over_type(db, type_fn)),
},
Self::TypeIs(type_is) => type_is.return_type(db).any_over_type(db, type_fn),
}
}
pub const fn into_class_literal(self) -> Option<ClassLiteral<'db>> { pub const fn into_class_literal(self) -> Option<ClassLiteral<'db>> {
match self { match self {
Type::ClassLiteral(class_type) => Some(class_type), Type::ClassLiteral(class_type) => Some(class_type),

View file

@ -1145,11 +1145,11 @@ impl KnownFunction {
let [Some(casted_type), Some(source_type)] = parameter_types else { let [Some(casted_type), Some(source_type)] = parameter_types else {
return None; return None;
}; };
let contains_unknown_or_todo = let is_unknown_or_todo =
|ty| matches!(ty, Type::Dynamic(dynamic) if dynamic != DynamicType::Any); |ty| matches!(ty, Type::Dynamic(dynamic) if dynamic != DynamicType::Any);
if source_type.is_equivalent_to(db, *casted_type) if source_type.is_equivalent_to(db, *casted_type)
&& !casted_type.any_over_type(db, &|ty| contains_unknown_or_todo(ty)) && !is_unknown_or_todo(*casted_type)
&& !source_type.any_over_type(db, &|ty| contains_unknown_or_todo(ty)) && !is_unknown_or_todo(*source_type)
{ {
let builder = context.report_lint(&REDUNDANT_CAST, call_expression)?; let builder = context.report_lint(&REDUNDANT_CAST, call_expression)?;
builder.into_diagnostic(format_args!( builder.into_diagnostic(format_args!(

View file

@ -229,15 +229,6 @@ impl<'db> ProtocolInstanceType<'db> {
} }
} }
/// Return `true` if the types of any of the members match the closure passed in.
pub(super) fn any_over_type(
self,
db: &'db dyn Db,
type_fn: &dyn Fn(Type<'db>) -> bool,
) -> bool {
self.inner.interface(db).any_over_type(db, type_fn)
}
/// Return `true` if this protocol type has the given type relation to the protocol `other`. /// Return `true` if this protocol type has the given type relation to the protocol `other`.
/// ///
/// TODO: consider the types of the members as well as their existence /// TODO: consider the types of the members as well as their existence

View file

@ -152,16 +152,6 @@ impl<'db> ProtocolInterface<'db> {
.all(|member_name| other.inner(db).contains_key(member_name)) .all(|member_name| other.inner(db).contains_key(member_name))
} }
/// Return `true` if the types of any of the members match the closure passed in.
pub(super) fn any_over_type(
self,
db: &'db dyn Db,
type_fn: &dyn Fn(Type<'db>) -> bool,
) -> bool {
self.members(db)
.any(|member| member.any_over_type(db, type_fn))
}
pub(super) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self { pub(super) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
Self::new( Self::new(
db, db,
@ -371,14 +361,6 @@ impl<'a, 'db> ProtocolMember<'a, 'db> {
} }
} }
} }
fn any_over_type(&self, db: &'db dyn Db, type_fn: &dyn Fn(Type<'db>) -> bool) -> bool {
match &self.kind {
ProtocolMemberKind::Method(callable) => callable.any_over_type(db, type_fn),
ProtocolMemberKind::Property(property) => property.any_over_type(db, type_fn),
ProtocolMemberKind::Other(ty) => ty.any_over_type(db, type_fn),
}
}
} }
/// Returns `true` if a declaration or binding to a given name in a protocol class body /// Returns `true` if a declaration or binding to a given name in a protocol class body