[ty] Refactor: Use let-chains in a few places (#20985)

This commit is contained in:
David Peter 2025-10-20 19:01:37 +02:00 committed by GitHub
parent 44e678a222
commit 54cd9d889d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 66 additions and 70 deletions

View file

@ -527,8 +527,8 @@ impl<'db> SemanticIndex<'db> {
break; break;
} }
if !ancestor_scope.is_eager() { if !ancestor_scope.is_eager() {
if let PlaceExprRef::Symbol(symbol) = expr { if let PlaceExprRef::Symbol(symbol) = expr
if let Some(place_id) = && let Some(place_id) =
self.place_tables[enclosing_scope].symbol_id(symbol.name()) self.place_tables[enclosing_scope].symbol_id(symbol.name())
{ {
let key = EnclosingSnapshotKey { let key = EnclosingSnapshotKey {
@ -542,7 +542,6 @@ impl<'db> SemanticIndex<'db> {
.enclosing_snapshot(*id, key.nested_laziness); .enclosing_snapshot(*id, key.nested_laziness);
} }
} }
}
return EnclosingSnapshotResult::NoLongerInEagerContext; return EnclosingSnapshotResult::NoLongerInEagerContext;
} }
} }

View file

@ -622,11 +622,11 @@ impl CondaEnvironmentKind {
/// the active environment name, not a constant base environment name. /// the active environment name, not a constant base environment name.
fn from_prefix_path(system: &dyn System, path: &SystemPath) -> Self { fn from_prefix_path(system: &dyn System, path: &SystemPath) -> Self {
// If `_CONDA_ROOT` is set and matches `CONDA_PREFIX`, it's the base environment. // If `_CONDA_ROOT` is set and matches `CONDA_PREFIX`, it's the base environment.
if let Ok(conda_root) = system.env_var(EnvVars::CONDA_ROOT) { if let Ok(conda_root) = system.env_var(EnvVars::CONDA_ROOT)
if path.as_str() == conda_root { && path.as_str() == conda_root
{
return Self::Base; return Self::Base;
} }
}
// Next, we'll use a heuristic based on `CONDA_DEFAULT_ENV` // Next, we'll use a heuristic based on `CONDA_DEFAULT_ENV`
let Ok(current_env) = system.env_var(EnvVars::CONDA_DEFAULT_ENV) else { let Ok(current_env) = system.env_var(EnvVars::CONDA_DEFAULT_ENV) else {

View file

@ -616,11 +616,11 @@ impl<'db> FunctionLiteral<'db> {
// We only include an implementation (i.e. a definition not decorated with `@overload`) if // We only include an implementation (i.e. a definition not decorated with `@overload`) if
// it's the only definition. // it's the only definition.
let (overloads, implementation) = self.overloads_and_implementation(db); let (overloads, implementation) = self.overloads_and_implementation(db);
if let Some(implementation) = implementation { if let Some(implementation) = implementation
if overloads.is_empty() { && overloads.is_empty()
{
return CallableSignature::single(implementation.signature(db)); return CallableSignature::single(implementation.signature(db));
} }
}
CallableSignature::from_overloads(overloads.iter().map(|overload| overload.signature(db))) CallableSignature::from_overloads(overloads.iter().map(|overload| overload.signature(db)))
} }
@ -1048,8 +1048,8 @@ fn is_instance_truthiness<'db>(
class: ClassLiteral<'db>, class: ClassLiteral<'db>,
) -> Truthiness { ) -> Truthiness {
let is_instance = |ty: &Type<'_>| { let is_instance = |ty: &Type<'_>| {
if let Type::NominalInstance(instance) = ty { if let Type::NominalInstance(instance) = ty
if instance && instance
.class(db) .class(db)
.iter_mro(db) .iter_mro(db)
.filter_map(ClassBase::into_class) .filter_map(ClassBase::into_class)
@ -1060,7 +1060,6 @@ fn is_instance_truthiness<'db>(
{ {
return true; return true;
} }
}
false false
}; };
@ -1642,8 +1641,9 @@ impl KnownFunction {
match second_argument { match second_argument {
Type::ClassLiteral(class) => { Type::ClassLiteral(class) => {
if let Some(protocol_class) = class.into_protocol_class(db) { if let Some(protocol_class) = class.into_protocol_class(db)
if !protocol_class.is_runtime_checkable(db) { && !protocol_class.is_runtime_checkable(db)
{
report_runtime_check_against_non_runtime_checkable_protocol( report_runtime_check_against_non_runtime_checkable_protocol(
context, context,
call_expression, call_expression,
@ -1651,7 +1651,6 @@ impl KnownFunction {
self, self,
); );
} }
}
if self == KnownFunction::IsInstance { if self == KnownFunction::IsInstance {
overload.set_return_type( overload.set_return_type(

View file

@ -326,8 +326,8 @@ fn validate_from_dict_literal<'db, 'ast>(
if let ast::Expr::Dict(dict_expr) = &arguments.args[0] { if let ast::Expr::Dict(dict_expr) = &arguments.args[0] {
// Validate dict entries // Validate dict entries
for dict_item in &dict_expr.items { for dict_item in &dict_expr.items {
if let Some(ref key_expr) = dict_item.key { if let Some(ref key_expr) = dict_item.key
if let ast::Expr::StringLiteral(ast::ExprStringLiteral { && let ast::Expr::StringLiteral(ast::ExprStringLiteral {
value: key_value, .. value: key_value, ..
}) = key_expr }) = key_expr
{ {
@ -349,7 +349,6 @@ fn validate_from_dict_literal<'db, 'ast>(
} }
} }
} }
}
provided_keys provided_keys
} }
@ -404,9 +403,9 @@ pub(super) fn validate_typed_dict_dict_literal<'db>(
// Validate each key-value pair in the dictionary literal // Validate each key-value pair in the dictionary literal
for item in &dict_expr.items { for item in &dict_expr.items {
if let Some(key_expr) = &item.key { if let Some(key_expr) = &item.key
let key_ty = expression_type_fn(key_expr); && let Type::StringLiteral(key_str) = expression_type_fn(key_expr)
if let Type::StringLiteral(key_str) = key_ty { {
let key_str = key_str.value(context.db()); let key_str = key_str.value(context.db());
provided_keys.insert(key_str); provided_keys.insert(key_str);
@ -424,7 +423,6 @@ pub(super) fn validate_typed_dict_dict_literal<'db>(
); );
} }
} }
}
valid &= validate_typed_dict_required_keys(context, typed_dict, &provided_keys, error_node); valid &= validate_typed_dict_required_keys(context, typed_dict, &provided_keys, error_node);