From 54cd9d889dff7a6b3f1e8b520c6993782e1fb9f5 Mon Sep 17 00:00:00 2001 From: David Peter Date: Mon, 20 Oct 2025 19:01:37 +0200 Subject: [PATCH] [ty] Refactor: Use `let`-chains in a few places (#20985) --- .../ty_python_semantic/src/semantic_index.rs | 25 ++++--- .../ty_python_semantic/src/site_packages.rs | 8 +-- .../ty_python_semantic/src/types/function.rs | 35 +++++----- .../src/types/typed_dict.rs | 68 +++++++++---------- 4 files changed, 66 insertions(+), 70 deletions(-) diff --git a/crates/ty_python_semantic/src/semantic_index.rs b/crates/ty_python_semantic/src/semantic_index.rs index fc57d0b2f6..558243f59c 100644 --- a/crates/ty_python_semantic/src/semantic_index.rs +++ b/crates/ty_python_semantic/src/semantic_index.rs @@ -527,20 +527,19 @@ impl<'db> SemanticIndex<'db> { break; } if !ancestor_scope.is_eager() { - if let PlaceExprRef::Symbol(symbol) = expr { - if let Some(place_id) = + if let PlaceExprRef::Symbol(symbol) = expr + && let Some(place_id) = self.place_tables[enclosing_scope].symbol_id(symbol.name()) - { - let key = EnclosingSnapshotKey { - enclosing_scope, - enclosing_place: place_id.into(), - nested_scope, - nested_laziness: ScopeLaziness::Lazy, - }; - if let Some(id) = self.enclosing_snapshots.get(&key) { - return self.use_def_maps[enclosing_scope] - .enclosing_snapshot(*id, key.nested_laziness); - } + { + let key = EnclosingSnapshotKey { + enclosing_scope, + enclosing_place: place_id.into(), + nested_scope, + nested_laziness: ScopeLaziness::Lazy, + }; + if let Some(id) = self.enclosing_snapshots.get(&key) { + return self.use_def_maps[enclosing_scope] + .enclosing_snapshot(*id, key.nested_laziness); } } return EnclosingSnapshotResult::NoLongerInEagerContext; diff --git a/crates/ty_python_semantic/src/site_packages.rs b/crates/ty_python_semantic/src/site_packages.rs index af810950c6..9062228363 100644 --- a/crates/ty_python_semantic/src/site_packages.rs +++ b/crates/ty_python_semantic/src/site_packages.rs @@ -622,10 +622,10 @@ impl CondaEnvironmentKind { /// the active environment name, not a constant base environment name. 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 let Ok(conda_root) = system.env_var(EnvVars::CONDA_ROOT) { - if path.as_str() == conda_root { - return Self::Base; - } + if let Ok(conda_root) = system.env_var(EnvVars::CONDA_ROOT) + && path.as_str() == conda_root + { + return Self::Base; } // Next, we'll use a heuristic based on `CONDA_DEFAULT_ENV` diff --git a/crates/ty_python_semantic/src/types/function.rs b/crates/ty_python_semantic/src/types/function.rs index 1020056b39..d368a33099 100644 --- a/crates/ty_python_semantic/src/types/function.rs +++ b/crates/ty_python_semantic/src/types/function.rs @@ -616,10 +616,10 @@ impl<'db> FunctionLiteral<'db> { // We only include an implementation (i.e. a definition not decorated with `@overload`) if // it's the only definition. let (overloads, implementation) = self.overloads_and_implementation(db); - if let Some(implementation) = implementation { - if overloads.is_empty() { - return CallableSignature::single(implementation.signature(db)); - } + if let Some(implementation) = implementation + && overloads.is_empty() + { + return CallableSignature::single(implementation.signature(db)); } CallableSignature::from_overloads(overloads.iter().map(|overload| overload.signature(db))) @@ -1048,8 +1048,8 @@ fn is_instance_truthiness<'db>( class: ClassLiteral<'db>, ) -> Truthiness { let is_instance = |ty: &Type<'_>| { - if let Type::NominalInstance(instance) = ty { - if instance + if let Type::NominalInstance(instance) = ty + && instance .class(db) .iter_mro(db) .filter_map(ClassBase::into_class) @@ -1057,9 +1057,8 @@ fn is_instance_truthiness<'db>( ClassType::Generic(c) => c.origin(db) == class, ClassType::NonGeneric(c) => c == class, }) - { - return true; - } + { + return true; } false }; @@ -1642,15 +1641,15 @@ impl KnownFunction { match second_argument { Type::ClassLiteral(class) => { - if let Some(protocol_class) = class.into_protocol_class(db) { - if !protocol_class.is_runtime_checkable(db) { - report_runtime_check_against_non_runtime_checkable_protocol( - context, - call_expression, - protocol_class, - self, - ); - } + if let Some(protocol_class) = class.into_protocol_class(db) + && !protocol_class.is_runtime_checkable(db) + { + report_runtime_check_against_non_runtime_checkable_protocol( + context, + call_expression, + protocol_class, + self, + ); } if self == KnownFunction::IsInstance { diff --git a/crates/ty_python_semantic/src/types/typed_dict.rs b/crates/ty_python_semantic/src/types/typed_dict.rs index 424ec1e6da..e29b836d8a 100644 --- a/crates/ty_python_semantic/src/types/typed_dict.rs +++ b/crates/ty_python_semantic/src/types/typed_dict.rs @@ -326,27 +326,26 @@ fn validate_from_dict_literal<'db, 'ast>( if let ast::Expr::Dict(dict_expr) = &arguments.args[0] { // Validate dict entries for dict_item in &dict_expr.items { - if let Some(ref key_expr) = dict_item.key { - if let ast::Expr::StringLiteral(ast::ExprStringLiteral { + if let Some(ref key_expr) = dict_item.key + && let ast::Expr::StringLiteral(ast::ExprStringLiteral { value: key_value, .. }) = key_expr - { - let key_str = key_value.to_str(); - provided_keys.insert(key_str); + { + let key_str = key_value.to_str(); + provided_keys.insert(key_str); - // Get the already-inferred argument type - let value_type = expression_type_fn(&dict_item.value); - validate_typed_dict_key_assignment( - context, - typed_dict, - key_str, - value_type, - error_node, - key_expr, - &dict_item.value, - TypedDictAssignmentKind::Constructor, - ); - } + // Get the already-inferred argument type + let value_type = expression_type_fn(&dict_item.value); + validate_typed_dict_key_assignment( + context, + typed_dict, + key_str, + value_type, + error_node, + key_expr, + &dict_item.value, + TypedDictAssignmentKind::Constructor, + ); } } } @@ -404,25 +403,24 @@ pub(super) fn validate_typed_dict_dict_literal<'db>( // Validate each key-value pair in the dictionary literal for item in &dict_expr.items { - if let Some(key_expr) = &item.key { - let key_ty = expression_type_fn(key_expr); - if let Type::StringLiteral(key_str) = key_ty { - let key_str = key_str.value(context.db()); - provided_keys.insert(key_str); + if let Some(key_expr) = &item.key + && let Type::StringLiteral(key_str) = expression_type_fn(key_expr) + { + let key_str = key_str.value(context.db()); + provided_keys.insert(key_str); - let value_type = expression_type_fn(&item.value); + let value_type = expression_type_fn(&item.value); - valid &= validate_typed_dict_key_assignment( - context, - typed_dict, - key_str, - value_type, - error_node, - key_expr, - &item.value, - TypedDictAssignmentKind::Constructor, - ); - } + valid &= validate_typed_dict_key_assignment( + context, + typed_dict, + key_str, + value_type, + error_node, + key_expr, + &item.value, + TypedDictAssignmentKind::Constructor, + ); } }