[red-knot] Inline Type::is_literal (#13230)

This commit is contained in:
Alex Waygood 2024-09-03 15:02:50 +01:00 committed by GitHub
parent 50c8ee5175
commit dfee65882b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 14 deletions

View file

@ -220,19 +220,6 @@ impl<'db> Type<'db> {
matches!(self, Type::Never) matches!(self, Type::Never)
} }
/// Returns `true` if this type should be displayed as a literal value.
pub const fn is_literal(&self) -> bool {
matches!(
self,
Type::IntLiteral(_)
| Type::BooleanLiteral(_)
| Type::StringLiteral(_)
| Type::BytesLiteral(_)
| Type::Class(_)
| Type::Function(_)
)
}
pub const fn into_class_type(self) -> Option<ClassType<'db>> { pub const fn into_class_type(self) -> Option<ClassType<'db>> {
match self { match self {
Type::Class(class_type) => Some(class_type), Type::Class(class_type) => Some(class_type),

View file

@ -27,7 +27,15 @@ pub struct DisplayType<'db> {
impl Display for DisplayType<'_> { impl Display for DisplayType<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let representation = self.ty.representation(self.db); let representation = self.ty.representation(self.db);
if self.ty.is_literal() { if matches!(
self.ty,
Type::IntLiteral(_)
| Type::BooleanLiteral(_)
| Type::StringLiteral(_)
| Type::BytesLiteral(_)
| Type::Class(_)
| Type::Function(_)
) {
write!(f, "Literal[{representation}]",) write!(f, "Literal[{representation}]",)
} else { } else {
representation.fmt(f) representation.fmt(f)