mirror of
				https://github.com/astral-sh/ruff.git
				synced 2025-11-03 21:24:29 +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;
 | 
					                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 {
 | 
				
			||||||
                            enclosing_scope,
 | 
					                        enclosing_scope,
 | 
				
			||||||
                            enclosing_place: place_id.into(),
 | 
					                        enclosing_place: place_id.into(),
 | 
				
			||||||
                            nested_scope,
 | 
					                        nested_scope,
 | 
				
			||||||
                            nested_laziness: ScopeLaziness::Lazy,
 | 
					                        nested_laziness: ScopeLaziness::Lazy,
 | 
				
			||||||
                        };
 | 
					                    };
 | 
				
			||||||
                        if let Some(id) = self.enclosing_snapshots.get(&key) {
 | 
					                    if let Some(id) = self.enclosing_snapshots.get(&key) {
 | 
				
			||||||
                            return self.use_def_maps[enclosing_scope]
 | 
					                        return self.use_def_maps[enclosing_scope]
 | 
				
			||||||
                                .enclosing_snapshot(*id, key.nested_laziness);
 | 
					                            .enclosing_snapshot(*id, key.nested_laziness);
 | 
				
			||||||
                        }
 | 
					 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                return EnclosingSnapshotResult::NoLongerInEagerContext;
 | 
					                return EnclosingSnapshotResult::NoLongerInEagerContext;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -622,10 +622,10 @@ 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`
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -616,10 +616,10 @@ 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)
 | 
				
			||||||
| 
						 | 
					@ -1057,9 +1057,8 @@ fn is_instance_truthiness<'db>(
 | 
				
			||||||
                    ClassType::Generic(c) => c.origin(db) == class,
 | 
					                    ClassType::Generic(c) => c.origin(db) == class,
 | 
				
			||||||
                    ClassType::NonGeneric(c) => c == class,
 | 
					                    ClassType::NonGeneric(c) => c == class,
 | 
				
			||||||
                })
 | 
					                })
 | 
				
			||||||
            {
 | 
					        {
 | 
				
			||||||
                return true;
 | 
					            return true;
 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        false
 | 
					        false
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
| 
						 | 
					@ -1642,15 +1641,15 @@ 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(
 | 
					                        {
 | 
				
			||||||
                                    context,
 | 
					                            report_runtime_check_against_non_runtime_checkable_protocol(
 | 
				
			||||||
                                    call_expression,
 | 
					                                context,
 | 
				
			||||||
                                    protocol_class,
 | 
					                                call_expression,
 | 
				
			||||||
                                    self,
 | 
					                                protocol_class,
 | 
				
			||||||
                                );
 | 
					                                self,
 | 
				
			||||||
                            }
 | 
					                            );
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        if self == KnownFunction::IsInstance {
 | 
					                        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] {
 | 
					    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
 | 
				
			||||||
                {
 | 
					            {
 | 
				
			||||||
                    let key_str = key_value.to_str();
 | 
					                let key_str = key_value.to_str();
 | 
				
			||||||
                    provided_keys.insert(key_str);
 | 
					                provided_keys.insert(key_str);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    // Get the already-inferred argument type
 | 
					                // Get the already-inferred argument type
 | 
				
			||||||
                    let value_type = expression_type_fn(&dict_item.value);
 | 
					                let value_type = expression_type_fn(&dict_item.value);
 | 
				
			||||||
                    validate_typed_dict_key_assignment(
 | 
					                validate_typed_dict_key_assignment(
 | 
				
			||||||
                        context,
 | 
					                    context,
 | 
				
			||||||
                        typed_dict,
 | 
					                    typed_dict,
 | 
				
			||||||
                        key_str,
 | 
					                    key_str,
 | 
				
			||||||
                        value_type,
 | 
					                    value_type,
 | 
				
			||||||
                        error_node,
 | 
					                    error_node,
 | 
				
			||||||
                        key_expr,
 | 
					                    key_expr,
 | 
				
			||||||
                        &dict_item.value,
 | 
					                    &dict_item.value,
 | 
				
			||||||
                        TypedDictAssignmentKind::Constructor,
 | 
					                    TypedDictAssignmentKind::Constructor,
 | 
				
			||||||
                    );
 | 
					                );
 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -404,25 +403,24 @@ 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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                let value_type = expression_type_fn(&item.value);
 | 
					            let value_type = expression_type_fn(&item.value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                valid &= validate_typed_dict_key_assignment(
 | 
					            valid &= validate_typed_dict_key_assignment(
 | 
				
			||||||
                    context,
 | 
					                context,
 | 
				
			||||||
                    typed_dict,
 | 
					                typed_dict,
 | 
				
			||||||
                    key_str,
 | 
					                key_str,
 | 
				
			||||||
                    value_type,
 | 
					                value_type,
 | 
				
			||||||
                    error_node,
 | 
					                error_node,
 | 
				
			||||||
                    key_expr,
 | 
					                key_expr,
 | 
				
			||||||
                    &item.value,
 | 
					                &item.value,
 | 
				
			||||||
                    TypedDictAssignmentKind::Constructor,
 | 
					                TypedDictAssignmentKind::Constructor,
 | 
				
			||||||
                );
 | 
					            );
 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue