mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 10:00:42 +00:00
Remove unused flags and functions from the semantic model (#14318)
This commit is contained in:
parent
eb55b9b5a0
commit
78e4753d74
5 changed files with 11 additions and 47 deletions
|
@ -1794,7 +1794,6 @@ impl<'a> Checker<'a> {
|
||||||
self.visit_expr(&generator.iter);
|
self.visit_expr(&generator.iter);
|
||||||
self.semantic.push_scope(ScopeKind::Generator(kind));
|
self.semantic.push_scope(ScopeKind::Generator(kind));
|
||||||
|
|
||||||
self.semantic.flags = flags | SemanticModelFlags::COMPREHENSION_ASSIGNMENT;
|
|
||||||
self.visit_expr(&generator.target);
|
self.visit_expr(&generator.target);
|
||||||
self.semantic.flags = flags;
|
self.semantic.flags = flags;
|
||||||
|
|
||||||
|
@ -1805,7 +1804,6 @@ impl<'a> Checker<'a> {
|
||||||
for generator in iterator {
|
for generator in iterator {
|
||||||
self.visit_expr(&generator.iter);
|
self.visit_expr(&generator.iter);
|
||||||
|
|
||||||
self.semantic.flags = flags | SemanticModelFlags::COMPREHENSION_ASSIGNMENT;
|
|
||||||
self.visit_expr(&generator.target);
|
self.visit_expr(&generator.target);
|
||||||
self.semantic.flags = flags;
|
self.semantic.flags = flags;
|
||||||
|
|
||||||
|
|
|
@ -23,8 +23,7 @@ pub(crate) fn is_typing_reference(reference: &ResolvedReference, settings: &Sett
|
||||||
// type definition to be considered a typing reference
|
// type definition to be considered a typing reference
|
||||||
|| (reference.in_type_definition()
|
|| (reference.in_type_definition()
|
||||||
&& (reference.in_typing_only_annotation()
|
&& (reference.in_typing_only_annotation()
|
||||||
|| reference.in_complex_string_type_definition()
|
|| reference.in_string_type_definition()
|
||||||
|| reference.in_simple_string_type_definition()
|
|
||||||
|| (settings.quote_annotations && reference.in_runtime_evaluated_annotation())))
|
|| (settings.quote_annotations && reference.in_runtime_evaluated_annotation())))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -151,8 +151,7 @@ pub(crate) fn import_private_name(
|
||||||
fn is_typing(reference: &ResolvedReference) -> bool {
|
fn is_typing(reference: &ResolvedReference) -> bool {
|
||||||
reference.in_type_checking_block()
|
reference.in_type_checking_block()
|
||||||
|| reference.in_typing_only_annotation()
|
|| reference.in_typing_only_annotation()
|
||||||
|| reference.in_complex_string_type_definition()
|
|| reference.in_string_type_definition()
|
||||||
|| reference.in_simple_string_type_definition()
|
|
||||||
|| reference.in_runtime_evaluated_annotation()
|
|| reference.in_runtime_evaluated_annotation()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1742,12 +1742,6 @@ impl<'a> SemanticModel<'a> {
|
||||||
.intersects(SemanticModelFlags::NAMED_EXPRESSION_ASSIGNMENT)
|
.intersects(SemanticModelFlags::NAMED_EXPRESSION_ASSIGNMENT)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `true` if the model is in a comprehension assignment (e.g., `_ for x in y`).
|
|
||||||
pub const fn in_comprehension_assignment(&self) -> bool {
|
|
||||||
self.flags
|
|
||||||
.intersects(SemanticModelFlags::COMPREHENSION_ASSIGNMENT)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return `true` if the model is visiting the r.h.s. of an `__all__` definition
|
/// Return `true` if the model is visiting the r.h.s. of an `__all__` definition
|
||||||
/// (e.g. `"foo"` in `__all__ = ["foo"]`)
|
/// (e.g. `"foo"` in `__all__ = ["foo"]`)
|
||||||
pub const fn in_dunder_all_definition(&self) -> bool {
|
pub const fn in_dunder_all_definition(&self) -> bool {
|
||||||
|
@ -2141,14 +2135,6 @@ bitflags! {
|
||||||
/// ```
|
/// ```
|
||||||
const NAMED_EXPRESSION_ASSIGNMENT = 1 << 19;
|
const NAMED_EXPRESSION_ASSIGNMENT = 1 << 19;
|
||||||
|
|
||||||
/// The model is in a comprehension variable assignment.
|
|
||||||
///
|
|
||||||
/// For example, the model could be visiting `x` in:
|
|
||||||
/// ```python
|
|
||||||
/// [_ for x in range(10)]
|
|
||||||
/// ```
|
|
||||||
const COMPREHENSION_ASSIGNMENT = 1 << 20;
|
|
||||||
|
|
||||||
/// The model is in a docstring as described in [PEP 257].
|
/// The model is in a docstring as described in [PEP 257].
|
||||||
///
|
///
|
||||||
/// For example, the model could be visiting either the module, class,
|
/// For example, the model could be visiting either the module, class,
|
||||||
|
@ -2168,7 +2154,7 @@ bitflags! {
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [PEP 257]: https://peps.python.org/pep-0257/#what-is-a-docstring
|
/// [PEP 257]: https://peps.python.org/pep-0257/#what-is-a-docstring
|
||||||
const PEP_257_DOCSTRING = 1 << 21;
|
const PEP_257_DOCSTRING = 1 << 20;
|
||||||
|
|
||||||
/// The model is visiting the r.h.s. of a module-level `__all__` definition.
|
/// The model is visiting the r.h.s. of a module-level `__all__` definition.
|
||||||
///
|
///
|
||||||
|
@ -2180,7 +2166,7 @@ bitflags! {
|
||||||
/// __all__ = ("bar",)
|
/// __all__ = ("bar",)
|
||||||
/// __all__ += ("baz,")
|
/// __all__ += ("baz,")
|
||||||
/// ```
|
/// ```
|
||||||
const DUNDER_ALL_DEFINITION = 1 << 22;
|
const DUNDER_ALL_DEFINITION = 1 << 21;
|
||||||
|
|
||||||
/// The model is in an f-string replacement field.
|
/// The model is in an f-string replacement field.
|
||||||
///
|
///
|
||||||
|
@ -2189,7 +2175,7 @@ bitflags! {
|
||||||
/// ```python
|
/// ```python
|
||||||
/// f"first {x} second {y}"
|
/// f"first {x} second {y}"
|
||||||
/// ```
|
/// ```
|
||||||
const F_STRING_REPLACEMENT_FIELD = 1 << 23;
|
const F_STRING_REPLACEMENT_FIELD = 1 << 22;
|
||||||
|
|
||||||
/// The model is visiting the bases tuple of a class.
|
/// The model is visiting the bases tuple of a class.
|
||||||
///
|
///
|
||||||
|
@ -2199,11 +2185,11 @@ bitflags! {
|
||||||
/// class Baz(Foo, Bar):
|
/// class Baz(Foo, Bar):
|
||||||
/// pass
|
/// pass
|
||||||
/// ```
|
/// ```
|
||||||
const CLASS_BASE = 1 << 24;
|
const CLASS_BASE = 1 << 23;
|
||||||
|
|
||||||
/// The model is visiting a class base that was initially deferred
|
/// The model is visiting a class base that was initially deferred
|
||||||
/// while traversing the AST. (This only happens in stub files.)
|
/// while traversing the AST. (This only happens in stub files.)
|
||||||
const DEFERRED_CLASS_BASE = 1 << 25;
|
const DEFERRED_CLASS_BASE = 1 << 24;
|
||||||
|
|
||||||
/// The model is in an attribute docstring.
|
/// The model is in an attribute docstring.
|
||||||
///
|
///
|
||||||
|
@ -2228,7 +2214,7 @@ bitflags! {
|
||||||
/// static-analysis tools.
|
/// static-analysis tools.
|
||||||
///
|
///
|
||||||
/// [PEP 257]: https://peps.python.org/pep-0257/#what-is-a-docstring
|
/// [PEP 257]: https://peps.python.org/pep-0257/#what-is-a-docstring
|
||||||
const ATTRIBUTE_DOCSTRING = 1 << 26;
|
const ATTRIBUTE_DOCSTRING = 1 << 25;
|
||||||
|
|
||||||
/// The context is in any type annotation.
|
/// The context is in any type annotation.
|
||||||
const ANNOTATION = Self::TYPING_ONLY_ANNOTATION.bits() | Self::RUNTIME_EVALUATED_ANNOTATION.bits() | Self::RUNTIME_REQUIRED_ANNOTATION.bits();
|
const ANNOTATION = Self::TYPING_ONLY_ANNOTATION.bits() | Self::RUNTIME_EVALUATED_ANNOTATION.bits() | Self::RUNTIME_REQUIRED_ANNOTATION.bits();
|
||||||
|
|
|
@ -63,28 +63,10 @@ impl ResolvedReference {
|
||||||
.intersects(SemanticModelFlags::RUNTIME_EVALUATED_ANNOTATION)
|
.intersects(SemanticModelFlags::RUNTIME_EVALUATED_ANNOTATION)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `true` if the context is in a "simple" string type definition.
|
/// Return `true` if the context is in a string type definition.
|
||||||
pub const fn in_simple_string_type_definition(&self) -> bool {
|
pub const fn in_string_type_definition(&self) -> bool {
|
||||||
self.flags
|
self.flags
|
||||||
.intersects(SemanticModelFlags::SIMPLE_STRING_TYPE_DEFINITION)
|
.intersects(SemanticModelFlags::STRING_TYPE_DEFINITION)
|
||||||
}
|
|
||||||
|
|
||||||
/// Return `true` if the context is in a "complex" string type definition.
|
|
||||||
pub const fn in_complex_string_type_definition(&self) -> bool {
|
|
||||||
self.flags
|
|
||||||
.intersects(SemanticModelFlags::COMPLEX_STRING_TYPE_DEFINITION)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return `true` if the context is in a `__future__` type definition.
|
|
||||||
pub const fn in_future_type_definition(&self) -> bool {
|
|
||||||
self.flags
|
|
||||||
.intersects(SemanticModelFlags::FUTURE_TYPE_DEFINITION)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return `true` if the context is in any kind of deferred type definition.
|
|
||||||
pub const fn in_deferred_type_definition(&self) -> bool {
|
|
||||||
self.flags
|
|
||||||
.intersects(SemanticModelFlags::DEFERRED_TYPE_DEFINITION)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `true` if the context is in any kind of type definition.
|
/// Return `true` if the context is in any kind of type definition.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue