mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-01 04:18:05 +00:00
[ty] Refactor: Use let-chains in a few places (#20985)
This commit is contained in:
parent
44e678a222
commit
54cd9d889d
4 changed files with 66 additions and 70 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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`
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue