[red-knot] Fixup some formatting in infer.rs (#16348)

This commit is contained in:
Alex Waygood 2025-02-24 14:44:49 +00:00 committed by GitHub
parent 7059f4249b
commit 45bae29a4b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3713,43 +3713,45 @@ impl<'db> TypeInferenceBuilder<'db> {
.unwrap_with_diagnostic(|lookup_error| match lookup_error { .unwrap_with_diagnostic(|lookup_error| match lookup_error {
LookupError::Unbound => { LookupError::Unbound => {
let bound_on_instance = match value_type { let bound_on_instance = match value_type {
Type::ClassLiteral(class) => { Type::ClassLiteral(class) => {
!class.class().instance_member(db, attr).0.is_unbound() !class.class().instance_member(db, attr).0.is_unbound()
}
Type::SubclassOf(subclass_of @ SubclassOfType { .. }) => {
match subclass_of.subclass_of() {
ClassBase::Class(class) => {
!class.instance_member(db, attr).0.is_unbound()
}
ClassBase::Dynamic(_) => unreachable!("Attribute lookup on a dynamic `SubclassOf` type should always return a bound symbol"),
} }
Type::SubclassOf(subclass_of @ SubclassOfType { .. }) => {
match subclass_of.subclass_of() {
ClassBase::Class(class) => {
!class.instance_member(db, attr).0.is_unbound()
}
ClassBase::Dynamic(_) => unreachable!(
"Attribute lookup on a dynamic `SubclassOf` type should always return a bound symbol"
),
}
}
_ => false,
};
if bound_on_instance {
self.context.report_lint(
&UNRESOLVED_ATTRIBUTE,
attribute,
format_args!(
"Attribute `{}` can only be accessed on instances, not on the class object `{}` itself.",
attr.id,
value_type.display(db)
),
);
} else {
self.context.report_lint(
&UNRESOLVED_ATTRIBUTE,
attribute,
format_args!(
"Type `{}` has no attribute `{}`",
value_type.display(db),
attr.id
),
);
} }
_ => false,
};
if bound_on_instance { Type::unknown()
self.context.report_lint(
&UNRESOLVED_ATTRIBUTE,
attribute,
format_args!(
"Attribute `{}` can only be accessed on instances, not on the class object `{}` itself.",
attr.id,
value_type.display(db)
),
);
} else {
self.context.report_lint(
&UNRESOLVED_ATTRIBUTE,
attribute,
format_args!(
"Type `{}` has no attribute `{}`",
value_type.display(db),
attr.id
),
);
}
Type::unknown()
} }
LookupError::PossiblyUnbound(type_when_bound) => { LookupError::PossiblyUnbound(type_when_bound) => {
self.context.report_lint( self.context.report_lint(
@ -3799,7 +3801,7 @@ impl<'db> TypeInferenceBuilder<'db> {
let class_member = value_ty.member(self.db(), attr); let class_member = value_ty.member(self.db(), attr);
if class_member.is_unbound() { if class_member.is_unbound() {
if let Some(class) = match value_ty { let class = match value_ty {
Type::ClassLiteral(class) => Some(class.class()), Type::ClassLiteral(class) => Some(class.class()),
Type::SubclassOf(subclass_of @ SubclassOfType { .. }) => { Type::SubclassOf(subclass_of @ SubclassOfType { .. }) => {
match subclass_of.subclass_of() { match subclass_of.subclass_of() {
@ -3808,7 +3810,8 @@ impl<'db> TypeInferenceBuilder<'db> {
} }
} }
_ => None, _ => None,
} { };
if let Some(class) = class {
let instance_member = class.instance_member(self.db(), attr); let instance_member = class.instance_member(self.db(), attr);
// Attribute is declared or bound on instance. Forbid access from the class object // Attribute is declared or bound on instance. Forbid access from the class object