diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index ee0a17fee4..0cb15891ec 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -176,13 +176,12 @@ impl<'a> Checker<'a> { /// /// If the current expression in the context is not an f-string, returns ``None``. pub(crate) fn f_string_quote_style(&self) -> Option { - let model = &self.semantic; - if !model.in_f_string() { + if !self.semantic.in_f_string() { return None; } // Find the quote character used to start the containing f-string. - let expr = model.current_expression()?; + let expr = self.semantic.current_expression()?; let string_range = self.indexer.f_string_range(expr.start())?; let trailing_quote = trailing_quote(self.locator.slice(string_range))?; diff --git a/crates/ruff/src/rules/flake8_bandit/rules/bad_file_permissions.rs b/crates/ruff/src/rules/flake8_bandit/rules/bad_file_permissions.rs index 3cf3cb905b..1da82b4681 100644 --- a/crates/ruff/src/rules/flake8_bandit/rules/bad_file_permissions.rs +++ b/crates/ruff/src/rules/flake8_bandit/rules/bad_file_permissions.rs @@ -112,21 +112,21 @@ fn py_stat(call_path: &CallPath) -> Option { } } -fn int_value(expr: &Expr, model: &SemanticModel) -> Option { +fn int_value(expr: &Expr, semantic: &SemanticModel) -> Option { match expr { Expr::Constant(ast::ExprConstant { value: Constant::Int(value), .. }) => value.to_u16(), - Expr::Attribute(_) => model.resolve_call_path(expr).as_ref().and_then(py_stat), + Expr::Attribute(_) => semantic.resolve_call_path(expr).as_ref().and_then(py_stat), Expr::BinOp(ast::ExprBinOp { left, op, right, range: _, }) => { - let left_value = int_value(left, model)?; - let right_value = int_value(right, model)?; + let left_value = int_value(left, semantic)?; + let right_value = int_value(right, semantic)?; match op { Operator::BitAnd => Some(left_value & right_value), Operator::BitOr => Some(left_value | right_value), diff --git a/crates/ruff/src/rules/flake8_bandit/rules/hardcoded_sql_expression.rs b/crates/ruff/src/rules/flake8_bandit/rules/hardcoded_sql_expression.rs index 467a0676cd..9d82380f4e 100644 --- a/crates/ruff/src/rules/flake8_bandit/rules/hardcoded_sql_expression.rs +++ b/crates/ruff/src/rules/flake8_bandit/rules/hardcoded_sql_expression.rs @@ -53,7 +53,7 @@ fn matches_sql_statement(string: &str) -> bool { SQL_REGEX.is_match(string) } -fn matches_string_format_expression(expr: &Expr, model: &SemanticModel) -> bool { +fn matches_string_format_expression(expr: &Expr, semantic: &SemanticModel) -> bool { match expr { // "select * from table where val = " + "str" + ... // "select * from table where val = %s" % ... @@ -62,7 +62,7 @@ fn matches_string_format_expression(expr: &Expr, model: &SemanticModel) -> bool .. }) => { // Only evaluate the full BinOp, not the nested components. - if model + if semantic .current_expression_parent() .map_or(true, |parent| !parent.is_bin_op_expr()) { diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/function_call_in_argument_default.rs b/crates/ruff/src/rules/flake8_bugbear/rules/function_call_in_argument_default.rs index 01e52dbb67..48577e04dd 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/function_call_in_argument_default.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/function_call_in_argument_default.rs @@ -67,9 +67,9 @@ struct ArgumentDefaultVisitor<'a> { } impl<'a> ArgumentDefaultVisitor<'a> { - fn new(model: &'a SemanticModel<'a>, extend_immutable_calls: Vec>) -> Self { + fn new(semantic: &'a SemanticModel<'a>, extend_immutable_calls: Vec>) -> Self { Self { - semantic: model, + semantic, extend_immutable_calls, diagnostics: Vec::new(), } diff --git a/crates/ruff/src/rules/flake8_builtins/rules/builtin_attribute_shadowing.rs b/crates/ruff/src/rules/flake8_builtins/rules/builtin_attribute_shadowing.rs index 271bc997ce..f4069ff24b 100644 --- a/crates/ruff/src/rules/flake8_builtins/rules/builtin_attribute_shadowing.rs +++ b/crates/ruff/src/rules/flake8_builtins/rules/builtin_attribute_shadowing.rs @@ -131,7 +131,7 @@ pub(crate) fn builtin_method_shadowing( fn is_standard_library_override( name: &str, class_def: &ast::StmtClassDef, - model: &SemanticModel, + semantic: &SemanticModel, ) -> bool { let Some(Arguments { args: bases, .. }) = class_def.arguments.as_deref() else { return false; @@ -139,13 +139,13 @@ fn is_standard_library_override( match name { // Ex) `Event#set` "set" => bases.iter().any(|base| { - model + semantic .resolve_call_path(base) .is_some_and(|call_path| matches!(call_path.as_slice(), ["threading", "Event"])) }), // Ex) `Filter#filter` "filter" => bases.iter().any(|base| { - model + semantic .resolve_call_path(base) .is_some_and(|call_path| matches!(call_path.as_slice(), ["logging", "Filter"])) }), diff --git a/crates/ruff/src/rules/flake8_pyi/rules/exit_annotations.rs b/crates/ruff/src/rules/flake8_pyi/rules/exit_annotations.rs index fc43b9b485..5a183d94d9 100644 --- a/crates/ruff/src/rules/flake8_pyi/rules/exit_annotations.rs +++ b/crates/ruff/src/rules/flake8_pyi/rules/exit_annotations.rs @@ -242,11 +242,11 @@ fn check_positional_args( /// Return the non-`None` annotation element of a PEP 604-style union or `Optional` annotation. fn non_none_annotation_element<'a>( annotation: &'a Expr, - model: &SemanticModel, + semantic: &SemanticModel, ) -> Option<&'a Expr> { // E.g., `typing.Union` or `typing.Optional` if let Expr::Subscript(ExprSubscript { value, slice, .. }) = annotation { - if model.match_typing_expr(value, "Optional") { + if semantic.match_typing_expr(value, "Optional") { return if is_const_none(slice) { None } else { @@ -254,7 +254,7 @@ fn non_none_annotation_element<'a>( }; } - if !model.match_typing_expr(value, "Union") { + if !semantic.match_typing_expr(value, "Union") { return None; } @@ -297,8 +297,8 @@ fn non_none_annotation_element<'a>( } /// Return `true` if the [`Expr`] is the `object` builtin or the `_typeshed.Unused` type. -fn is_object_or_unused(expr: &Expr, model: &SemanticModel) -> bool { - model +fn is_object_or_unused(expr: &Expr, semantic: &SemanticModel) -> bool { + semantic .resolve_call_path(expr) .as_ref() .is_some_and(|call_path| { @@ -310,34 +310,34 @@ fn is_object_or_unused(expr: &Expr, model: &SemanticModel) -> bool { } /// Return `true` if the [`Expr`] is `BaseException`. -fn is_base_exception(expr: &Expr, model: &SemanticModel) -> bool { - model +fn is_base_exception(expr: &Expr, semantic: &SemanticModel) -> bool { + semantic .resolve_call_path(expr) .as_ref() .is_some_and(|call_path| matches!(call_path.as_slice(), ["" | "builtins", "BaseException"])) } /// Return `true` if the [`Expr`] is the `types.TracebackType` type. -fn is_traceback_type(expr: &Expr, model: &SemanticModel) -> bool { - model +fn is_traceback_type(expr: &Expr, semantic: &SemanticModel) -> bool { + semantic .resolve_call_path(expr) .as_ref() .is_some_and(|call_path| matches!(call_path.as_slice(), ["types", "TracebackType"])) } /// Return `true` if the [`Expr`] is, e.g., `Type[BaseException]`. -fn is_base_exception_type(expr: &Expr, model: &SemanticModel) -> bool { +fn is_base_exception_type(expr: &Expr, semantic: &SemanticModel) -> bool { let Expr::Subscript(ExprSubscript { value, slice, .. }) = expr else { return false; }; - if model.match_typing_expr(value, "Type") - || model + if semantic.match_typing_expr(value, "Type") + || semantic .resolve_call_path(value) .as_ref() .is_some_and(|call_path| matches!(call_path.as_slice(), ["" | "builtins", "type"])) { - is_base_exception(slice, model) + is_base_exception(slice, semantic) } else { false } diff --git a/crates/ruff/src/rules/flake8_pyi/rules/redundant_literal_union.rs b/crates/ruff/src/rules/flake8_pyi/rules/redundant_literal_union.rs index 51e333fc50..b1c112c0f9 100644 --- a/crates/ruff/src/rules/flake8_pyi/rules/redundant_literal_union.rs +++ b/crates/ruff/src/rules/flake8_pyi/rules/redundant_literal_union.rs @@ -121,7 +121,7 @@ impl fmt::Display for ExprType { /// Return the [`ExprType`] of an [`Expr]` if it is a builtin type (e.g. `int`, `bool`, `float`, /// `str`, `bytes`, or `complex`). -fn match_builtin_type(expr: &Expr, model: &SemanticModel) -> Option { +fn match_builtin_type(expr: &Expr, semantic: &SemanticModel) -> Option { let name = expr.as_name_expr()?; let result = match name.id.as_str() { "int" => ExprType::Int, @@ -132,7 +132,7 @@ fn match_builtin_type(expr: &Expr, model: &SemanticModel) -> Option { "complex" => ExprType::Complex, _ => return None, }; - if !model.is_builtin(name.id.as_str()) { + if !semantic.is_builtin(name.id.as_str()) { return None; } Some(result) diff --git a/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_str_subclass.rs b/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_str_subclass.rs index 45213759fb..c72057ea64 100644 --- a/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_str_subclass.rs +++ b/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_str_subclass.rs @@ -66,10 +66,10 @@ pub(crate) fn no_slots_in_str_subclass(checker: &mut Checker, stmt: &Stmt, class /// Return `true` if the class is a subclass of `str`, but _not_ a subclass of `enum.Enum`, /// `enum.IntEnum`, etc. -fn is_str_subclass(bases: &[Expr], model: &SemanticModel) -> bool { +fn is_str_subclass(bases: &[Expr], semantic: &SemanticModel) -> bool { let mut is_str_subclass = false; for base in bases { - if let Some(call_path) = model.resolve_call_path(base) { + if let Some(call_path) = semantic.resolve_call_path(base) { match call_path.as_slice() { ["" | "builtins", "str"] => { is_str_subclass = true; diff --git a/crates/ruff/src/rules/perflint/rules/incorrect_dict_iterator.rs b/crates/ruff/src/rules/perflint/rules/incorrect_dict_iterator.rs index 58e1ed7e07..0419d1790b 100644 --- a/crates/ruff/src/rules/perflint/rules/incorrect_dict_iterator.rs +++ b/crates/ruff/src/rules/perflint/rules/incorrect_dict_iterator.rs @@ -148,9 +148,11 @@ impl fmt::Display for DictSubset { } /// Returns `true` if the given expression is either an unused value or a tuple of unused values. -fn is_unused(expr: &Expr, model: &SemanticModel) -> bool { +fn is_unused(expr: &Expr, semantic: &SemanticModel) -> bool { match expr { - Expr::Tuple(ast::ExprTuple { elts, .. }) => elts.iter().all(|expr| is_unused(expr, model)), + Expr::Tuple(ast::ExprTuple { elts, .. }) => { + elts.iter().all(|expr| is_unused(expr, semantic)) + } Expr::Name(ast::ExprName { id, .. }) => { // Treat a variable as used if it has any usages, _or_ it's shadowed by another variable // with usages. @@ -167,10 +169,10 @@ fn is_unused(expr: &Expr, model: &SemanticModel) -> bool { // // print(bar) // ``` - let scope = model.current_scope(); + let scope = semantic.current_scope(); scope .get_all(id) - .map(|binding_id| model.binding(binding_id)) + .map(|binding_id| semantic.binding(binding_id)) .filter(|binding| binding.range.start() >= expr.range().start()) .all(|binding| !binding.is_used()) } diff --git a/crates/ruff/src/rules/pyupgrade/rules/redundant_open_modes.rs b/crates/ruff/src/rules/pyupgrade/rules/redundant_open_modes.rs index d09ec73155..cddf140161 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/redundant_open_modes.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/redundant_open_modes.rs @@ -116,11 +116,11 @@ const OPEN_FUNC_NAME: &str = "open"; const MODE_KEYWORD_ARGUMENT: &str = "mode"; /// Returns `true` if the given `call` is a call to the `open` builtin. -fn is_open_builtin(func: &Expr, model: &SemanticModel) -> bool { +fn is_open_builtin(func: &Expr, semantic: &SemanticModel) -> bool { let Some(ast::ExprName { id, .. }) = func.as_name_expr() else { return false; }; - id.as_str() == OPEN_FUNC_NAME && model.is_builtin(id) + id.as_str() == OPEN_FUNC_NAME && semantic.is_builtin(id) } #[derive(Debug, Copy, Clone)] diff --git a/crates/ruff/src/rules/ruff/rules/unnecessary_iterable_allocation_for_first_element.rs b/crates/ruff/src/rules/ruff/rules/unnecessary_iterable_allocation_for_first_element.rs index ec61a8b4f9..13c581fdc4 100644 --- a/crates/ruff/src/rules/ruff/rules/unnecessary_iterable_allocation_for_first_element.rs +++ b/crates/ruff/src/rules/ruff/rules/unnecessary_iterable_allocation_for_first_element.rs @@ -149,7 +149,7 @@ struct IterationTarget { /// /// As a special-case, given `[x for x in y]`, returns the range of `y` (rather than the /// redundant comprehension). -fn match_iteration_target(expr: &Expr, model: &SemanticModel) -> Option { +fn match_iteration_target(expr: &Expr, semantic: &SemanticModel) -> Option { let result = match expr { Expr::Call(ast::ExprCall { func, @@ -166,7 +166,7 @@ fn match_iteration_target(expr: &Expr, model: &SemanticModel) -> Option SemanticModel<'a> { exceptions } - /// Generate a [`Snapshot`] of the current model. + /// Generate a [`Snapshot`] of the current semantic model. pub fn snapshot(&self) -> Snapshot { Snapshot { scope_id: self.scope_id, @@ -1124,7 +1124,7 @@ impl<'a> SemanticModel<'a> { } } - /// Restore the model to the given [`Snapshot`]. + /// Restore the semantic model to the given [`Snapshot`]. pub fn restore(&mut self, snapshot: Snapshot) { let Snapshot { scope_id,