mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-09 21:28:21 +00:00
Change model: &SemanticModel
to semantic: &SemanticModel
(#6406)
Use the same naming conventions everywhere. See: https://github.com/astral-sh/ruff/pull/6314/files#r1284457874.
This commit is contained in:
parent
404e334fec
commit
3d06fe743d
12 changed files with 42 additions and 41 deletions
|
@ -176,13 +176,12 @@ impl<'a> Checker<'a> {
|
||||||
///
|
///
|
||||||
/// If the current expression in the context is not an f-string, returns ``None``.
|
/// If the current expression in the context is not an f-string, returns ``None``.
|
||||||
pub(crate) fn f_string_quote_style(&self) -> Option<Quote> {
|
pub(crate) fn f_string_quote_style(&self) -> Option<Quote> {
|
||||||
let model = &self.semantic;
|
if !self.semantic.in_f_string() {
|
||||||
if !model.in_f_string() {
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the quote character used to start the containing f-string.
|
// 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 string_range = self.indexer.f_string_range(expr.start())?;
|
||||||
let trailing_quote = trailing_quote(self.locator.slice(string_range))?;
|
let trailing_quote = trailing_quote(self.locator.slice(string_range))?;
|
||||||
|
|
||||||
|
|
|
@ -112,21 +112,21 @@ fn py_stat(call_path: &CallPath) -> Option<u16> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn int_value(expr: &Expr, model: &SemanticModel) -> Option<u16> {
|
fn int_value(expr: &Expr, semantic: &SemanticModel) -> Option<u16> {
|
||||||
match expr {
|
match expr {
|
||||||
Expr::Constant(ast::ExprConstant {
|
Expr::Constant(ast::ExprConstant {
|
||||||
value: Constant::Int(value),
|
value: Constant::Int(value),
|
||||||
..
|
..
|
||||||
}) => value.to_u16(),
|
}) => 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 {
|
Expr::BinOp(ast::ExprBinOp {
|
||||||
left,
|
left,
|
||||||
op,
|
op,
|
||||||
right,
|
right,
|
||||||
range: _,
|
range: _,
|
||||||
}) => {
|
}) => {
|
||||||
let left_value = int_value(left, model)?;
|
let left_value = int_value(left, semantic)?;
|
||||||
let right_value = int_value(right, model)?;
|
let right_value = int_value(right, semantic)?;
|
||||||
match op {
|
match op {
|
||||||
Operator::BitAnd => Some(left_value & right_value),
|
Operator::BitAnd => Some(left_value & right_value),
|
||||||
Operator::BitOr => Some(left_value | right_value),
|
Operator::BitOr => Some(left_value | right_value),
|
||||||
|
|
|
@ -53,7 +53,7 @@ fn matches_sql_statement(string: &str) -> bool {
|
||||||
SQL_REGEX.is_match(string)
|
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 {
|
match expr {
|
||||||
// "select * from table where val = " + "str" + ...
|
// "select * from table where val = " + "str" + ...
|
||||||
// "select * from table where val = %s" % ...
|
// "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.
|
// Only evaluate the full BinOp, not the nested components.
|
||||||
if model
|
if semantic
|
||||||
.current_expression_parent()
|
.current_expression_parent()
|
||||||
.map_or(true, |parent| !parent.is_bin_op_expr())
|
.map_or(true, |parent| !parent.is_bin_op_expr())
|
||||||
{
|
{
|
||||||
|
|
|
@ -67,9 +67,9 @@ struct ArgumentDefaultVisitor<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ArgumentDefaultVisitor<'a> {
|
impl<'a> ArgumentDefaultVisitor<'a> {
|
||||||
fn new(model: &'a SemanticModel<'a>, extend_immutable_calls: Vec<CallPath<'a>>) -> Self {
|
fn new(semantic: &'a SemanticModel<'a>, extend_immutable_calls: Vec<CallPath<'a>>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
semantic: model,
|
semantic,
|
||||||
extend_immutable_calls,
|
extend_immutable_calls,
|
||||||
diagnostics: Vec::new(),
|
diagnostics: Vec::new(),
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,7 +131,7 @@ pub(crate) fn builtin_method_shadowing(
|
||||||
fn is_standard_library_override(
|
fn is_standard_library_override(
|
||||||
name: &str,
|
name: &str,
|
||||||
class_def: &ast::StmtClassDef,
|
class_def: &ast::StmtClassDef,
|
||||||
model: &SemanticModel,
|
semantic: &SemanticModel,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let Some(Arguments { args: bases, .. }) = class_def.arguments.as_deref() else {
|
let Some(Arguments { args: bases, .. }) = class_def.arguments.as_deref() else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -139,13 +139,13 @@ fn is_standard_library_override(
|
||||||
match name {
|
match name {
|
||||||
// Ex) `Event#set`
|
// Ex) `Event#set`
|
||||||
"set" => bases.iter().any(|base| {
|
"set" => bases.iter().any(|base| {
|
||||||
model
|
semantic
|
||||||
.resolve_call_path(base)
|
.resolve_call_path(base)
|
||||||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["threading", "Event"]))
|
.is_some_and(|call_path| matches!(call_path.as_slice(), ["threading", "Event"]))
|
||||||
}),
|
}),
|
||||||
// Ex) `Filter#filter`
|
// Ex) `Filter#filter`
|
||||||
"filter" => bases.iter().any(|base| {
|
"filter" => bases.iter().any(|base| {
|
||||||
model
|
semantic
|
||||||
.resolve_call_path(base)
|
.resolve_call_path(base)
|
||||||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["logging", "Filter"]))
|
.is_some_and(|call_path| matches!(call_path.as_slice(), ["logging", "Filter"]))
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -242,11 +242,11 @@ fn check_positional_args(
|
||||||
/// Return the non-`None` annotation element of a PEP 604-style union or `Optional` annotation.
|
/// Return the non-`None` annotation element of a PEP 604-style union or `Optional` annotation.
|
||||||
fn non_none_annotation_element<'a>(
|
fn non_none_annotation_element<'a>(
|
||||||
annotation: &'a Expr,
|
annotation: &'a Expr,
|
||||||
model: &SemanticModel,
|
semantic: &SemanticModel,
|
||||||
) -> Option<&'a Expr> {
|
) -> Option<&'a Expr> {
|
||||||
// E.g., `typing.Union` or `typing.Optional`
|
// E.g., `typing.Union` or `typing.Optional`
|
||||||
if let Expr::Subscript(ExprSubscript { value, slice, .. }) = annotation {
|
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) {
|
return if is_const_none(slice) {
|
||||||
None
|
None
|
||||||
} else {
|
} 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;
|
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.
|
/// Return `true` if the [`Expr`] is the `object` builtin or the `_typeshed.Unused` type.
|
||||||
fn is_object_or_unused(expr: &Expr, model: &SemanticModel) -> bool {
|
fn is_object_or_unused(expr: &Expr, semantic: &SemanticModel) -> bool {
|
||||||
model
|
semantic
|
||||||
.resolve_call_path(expr)
|
.resolve_call_path(expr)
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.is_some_and(|call_path| {
|
.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`.
|
/// Return `true` if the [`Expr`] is `BaseException`.
|
||||||
fn is_base_exception(expr: &Expr, model: &SemanticModel) -> bool {
|
fn is_base_exception(expr: &Expr, semantic: &SemanticModel) -> bool {
|
||||||
model
|
semantic
|
||||||
.resolve_call_path(expr)
|
.resolve_call_path(expr)
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["" | "builtins", "BaseException"]))
|
.is_some_and(|call_path| matches!(call_path.as_slice(), ["" | "builtins", "BaseException"]))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `true` if the [`Expr`] is the `types.TracebackType` type.
|
/// Return `true` if the [`Expr`] is the `types.TracebackType` type.
|
||||||
fn is_traceback_type(expr: &Expr, model: &SemanticModel) -> bool {
|
fn is_traceback_type(expr: &Expr, semantic: &SemanticModel) -> bool {
|
||||||
model
|
semantic
|
||||||
.resolve_call_path(expr)
|
.resolve_call_path(expr)
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["types", "TracebackType"]))
|
.is_some_and(|call_path| matches!(call_path.as_slice(), ["types", "TracebackType"]))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `true` if the [`Expr`] is, e.g., `Type[BaseException]`.
|
/// 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 {
|
let Expr::Subscript(ExprSubscript { value, slice, .. }) = expr else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
if model.match_typing_expr(value, "Type")
|
if semantic.match_typing_expr(value, "Type")
|
||||||
|| model
|
|| semantic
|
||||||
.resolve_call_path(value)
|
.resolve_call_path(value)
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["" | "builtins", "type"]))
|
.is_some_and(|call_path| matches!(call_path.as_slice(), ["" | "builtins", "type"]))
|
||||||
{
|
{
|
||||||
is_base_exception(slice, model)
|
is_base_exception(slice, semantic)
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
|
@ -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`,
|
/// Return the [`ExprType`] of an [`Expr]` if it is a builtin type (e.g. `int`, `bool`, `float`,
|
||||||
/// `str`, `bytes`, or `complex`).
|
/// `str`, `bytes`, or `complex`).
|
||||||
fn match_builtin_type(expr: &Expr, model: &SemanticModel) -> Option<ExprType> {
|
fn match_builtin_type(expr: &Expr, semantic: &SemanticModel) -> Option<ExprType> {
|
||||||
let name = expr.as_name_expr()?;
|
let name = expr.as_name_expr()?;
|
||||||
let result = match name.id.as_str() {
|
let result = match name.id.as_str() {
|
||||||
"int" => ExprType::Int,
|
"int" => ExprType::Int,
|
||||||
|
@ -132,7 +132,7 @@ fn match_builtin_type(expr: &Expr, model: &SemanticModel) -> Option<ExprType> {
|
||||||
"complex" => ExprType::Complex,
|
"complex" => ExprType::Complex,
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
if !model.is_builtin(name.id.as_str()) {
|
if !semantic.is_builtin(name.id.as_str()) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
Some(result)
|
Some(result)
|
||||||
|
|
|
@ -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`,
|
/// Return `true` if the class is a subclass of `str`, but _not_ a subclass of `enum.Enum`,
|
||||||
/// `enum.IntEnum`, etc.
|
/// `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;
|
let mut is_str_subclass = false;
|
||||||
for base in bases {
|
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() {
|
match call_path.as_slice() {
|
||||||
["" | "builtins", "str"] => {
|
["" | "builtins", "str"] => {
|
||||||
is_str_subclass = true;
|
is_str_subclass = true;
|
||||||
|
|
|
@ -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.
|
/// 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 {
|
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, .. }) => {
|
Expr::Name(ast::ExprName { id, .. }) => {
|
||||||
// Treat a variable as used if it has any usages, _or_ it's shadowed by another variable
|
// Treat a variable as used if it has any usages, _or_ it's shadowed by another variable
|
||||||
// with usages.
|
// with usages.
|
||||||
|
@ -167,10 +169,10 @@ fn is_unused(expr: &Expr, model: &SemanticModel) -> bool {
|
||||||
//
|
//
|
||||||
// print(bar)
|
// print(bar)
|
||||||
// ```
|
// ```
|
||||||
let scope = model.current_scope();
|
let scope = semantic.current_scope();
|
||||||
scope
|
scope
|
||||||
.get_all(id)
|
.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())
|
.filter(|binding| binding.range.start() >= expr.range().start())
|
||||||
.all(|binding| !binding.is_used())
|
.all(|binding| !binding.is_used())
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,11 +116,11 @@ const OPEN_FUNC_NAME: &str = "open";
|
||||||
const MODE_KEYWORD_ARGUMENT: &str = "mode";
|
const MODE_KEYWORD_ARGUMENT: &str = "mode";
|
||||||
|
|
||||||
/// Returns `true` if the given `call` is a call to the `open` builtin.
|
/// 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 {
|
let Some(ast::ExprName { id, .. }) = func.as_name_expr() else {
|
||||||
return false;
|
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)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
|
|
@ -149,7 +149,7 @@ struct IterationTarget {
|
||||||
///
|
///
|
||||||
/// As a special-case, given `[x for x in y]`, returns the range of `y` (rather than the
|
/// As a special-case, given `[x for x in y]`, returns the range of `y` (rather than the
|
||||||
/// redundant comprehension).
|
/// redundant comprehension).
|
||||||
fn match_iteration_target(expr: &Expr, model: &SemanticModel) -> Option<IterationTarget> {
|
fn match_iteration_target(expr: &Expr, semantic: &SemanticModel) -> Option<IterationTarget> {
|
||||||
let result = match expr {
|
let result = match expr {
|
||||||
Expr::Call(ast::ExprCall {
|
Expr::Call(ast::ExprCall {
|
||||||
func,
|
func,
|
||||||
|
@ -166,7 +166,7 @@ fn match_iteration_target(expr: &Expr, model: &SemanticModel) -> Option<Iteratio
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
|
|
||||||
if !model.is_builtin(id.as_str()) {
|
if !semantic.is_builtin(id.as_str()) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1113,7 +1113,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
exceptions
|
exceptions
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate a [`Snapshot`] of the current model.
|
/// Generate a [`Snapshot`] of the current semantic model.
|
||||||
pub fn snapshot(&self) -> Snapshot {
|
pub fn snapshot(&self) -> Snapshot {
|
||||||
Snapshot {
|
Snapshot {
|
||||||
scope_id: self.scope_id,
|
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) {
|
pub fn restore(&mut self, snapshot: Snapshot) {
|
||||||
let Snapshot {
|
let Snapshot {
|
||||||
scope_id,
|
scope_id,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue