mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
Add a new Binding::is_unused
method (#12729)
This commit is contained in:
parent
b14fee9320
commit
d380b37a09
9 changed files with 19 additions and 9 deletions
|
@ -22,7 +22,7 @@ pub(crate) fn bindings(checker: &mut Checker) {
|
||||||
for binding in &*checker.semantic.bindings {
|
for binding in &*checker.semantic.bindings {
|
||||||
if checker.enabled(Rule::UnusedVariable) {
|
if checker.enabled(Rule::UnusedVariable) {
|
||||||
if binding.kind.is_bound_exception()
|
if binding.kind.is_bound_exception()
|
||||||
&& !binding.is_used()
|
&& binding.is_unused()
|
||||||
&& !checker
|
&& !checker
|
||||||
.settings
|
.settings
|
||||||
.dummy_variable_rgx
|
.dummy_variable_rgx
|
||||||
|
|
|
@ -4,6 +4,7 @@ use ruff_python_ast as ast;
|
||||||
use ruff_python_ast::helpers;
|
use ruff_python_ast::helpers;
|
||||||
use ruff_python_ast::helpers::{NameFinder, StoredNameFinder};
|
use ruff_python_ast::helpers::{NameFinder, StoredNameFinder};
|
||||||
use ruff_python_ast::visitor::Visitor;
|
use ruff_python_ast::visitor::Visitor;
|
||||||
|
use ruff_python_semantic::Binding;
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
@ -137,7 +138,7 @@ pub(crate) fn unused_loop_control_variable(checker: &mut Checker, stmt_for: &ast
|
||||||
.get_all(name)
|
.get_all(name)
|
||||||
.map(|binding_id| checker.semantic().binding(binding_id))
|
.map(|binding_id| checker.semantic().binding(binding_id))
|
||||||
.filter(|binding| binding.start() >= expr.start())
|
.filter(|binding| binding.start() >= expr.start())
|
||||||
.all(|binding| !binding.is_used())
|
.all(Binding::is_unused)
|
||||||
{
|
{
|
||||||
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
|
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
|
||||||
rename,
|
rename,
|
||||||
|
|
|
@ -298,7 +298,7 @@ fn call<'a>(
|
||||||
.get(arg.name.as_str())
|
.get(arg.name.as_str())
|
||||||
.map(|binding_id| semantic.binding(binding_id))?;
|
.map(|binding_id| semantic.binding(binding_id))?;
|
||||||
if binding.kind.is_argument()
|
if binding.kind.is_argument()
|
||||||
&& !binding.is_used()
|
&& binding.is_unused()
|
||||||
&& !dummy_variable_rgx.is_match(arg.name.as_str())
|
&& !dummy_variable_rgx.is_match(arg.name.as_str())
|
||||||
{
|
{
|
||||||
Some(Diagnostic::new(
|
Some(Diagnostic::new(
|
||||||
|
|
|
@ -42,7 +42,7 @@ pub(crate) fn unused_annotation(
|
||||||
for (name, range) in scope.bindings().filter_map(|(name, binding_id)| {
|
for (name, range) in scope.bindings().filter_map(|(name, binding_id)| {
|
||||||
let binding = checker.semantic().binding(binding_id);
|
let binding = checker.semantic().binding(binding_id);
|
||||||
if binding.kind.is_annotation()
|
if binding.kind.is_annotation()
|
||||||
&& !binding.is_used()
|
&& binding.is_unused()
|
||||||
&& !checker.settings.dummy_variable_rgx.is_match(name)
|
&& !checker.settings.dummy_variable_rgx.is_match(name)
|
||||||
{
|
{
|
||||||
Some((name.to_string(), binding.range()))
|
Some((name.to_string(), binding.range()))
|
||||||
|
|
|
@ -262,9 +262,9 @@ pub(crate) fn unused_variable(checker: &Checker, scope: &Scope, diagnostics: &mu
|
||||||
|| binding.kind.is_named_expr_assignment()
|
|| binding.kind.is_named_expr_assignment()
|
||||||
|| binding.kind.is_with_item_var())
|
|| binding.kind.is_with_item_var())
|
||||||
&& (!binding.is_unpacked_assignment() || checker.settings.preview.is_enabled())
|
&& (!binding.is_unpacked_assignment() || checker.settings.preview.is_enabled())
|
||||||
|
&& binding.is_unused()
|
||||||
&& !binding.is_nonlocal()
|
&& !binding.is_nonlocal()
|
||||||
&& !binding.is_global()
|
&& !binding.is_global()
|
||||||
&& !binding.is_used()
|
|
||||||
&& !checker.settings.dummy_variable_rgx.is_match(name)
|
&& !checker.settings.dummy_variable_rgx.is_match(name)
|
||||||
&& !matches!(
|
&& !matches!(
|
||||||
name,
|
name,
|
||||||
|
|
|
@ -123,7 +123,7 @@ pub(crate) fn no_self_use(
|
||||||
if scope
|
if scope
|
||||||
.get("self")
|
.get("self")
|
||||||
.map(|binding_id| semantic.binding(binding_id))
|
.map(|binding_id| semantic.binding(binding_id))
|
||||||
.is_some_and(|binding| binding.kind.is_argument() && !binding.is_used())
|
.is_some_and(|binding| binding.kind.is_argument() && binding.is_unused())
|
||||||
{
|
{
|
||||||
diagnostics.push(Diagnostic::new(
|
diagnostics.push(Diagnostic::new(
|
||||||
NoSelfUse {
|
NoSelfUse {
|
||||||
|
|
|
@ -124,7 +124,7 @@ pub(crate) fn sort_dunder_slots(checker: &Checker, binding: &Binding) -> Option<
|
||||||
if let Some(sorted_source_code) = display.generate_sorted_source_code(&items, checker) {
|
if let Some(sorted_source_code) = display.generate_sorted_source_code(&items, checker) {
|
||||||
let edit = Edit::range_replacement(sorted_source_code, display.range());
|
let edit = Edit::range_replacement(sorted_source_code, display.range());
|
||||||
|
|
||||||
let applicability = if display.kind.is_set_literal() || !binding.is_used() {
|
let applicability = if display.kind.is_set_literal() || binding.is_unused() {
|
||||||
Applicability::Safe
|
Applicability::Safe
|
||||||
} else {
|
} else {
|
||||||
Applicability::Unsafe
|
Applicability::Unsafe
|
||||||
|
|
|
@ -36,9 +36,18 @@ pub struct Binding<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Binding<'a> {
|
impl<'a> Binding<'a> {
|
||||||
|
/// Return `true` if this [`Binding`] is unused.
|
||||||
|
///
|
||||||
|
/// This method is the opposite of [`Binding::is_used`].
|
||||||
|
pub fn is_unused(&self) -> bool {
|
||||||
|
self.references.is_empty()
|
||||||
|
}
|
||||||
|
|
||||||
/// Return `true` if this [`Binding`] is used.
|
/// Return `true` if this [`Binding`] is used.
|
||||||
|
///
|
||||||
|
/// This method is the opposite of [`Binding::is_unused`].
|
||||||
pub fn is_used(&self) -> bool {
|
pub fn is_used(&self) -> bool {
|
||||||
!self.references.is_empty()
|
!self.is_unused()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator over all references for the current [`Binding`].
|
/// Returns an iterator over all references for the current [`Binding`].
|
||||||
|
|
|
@ -1455,7 +1455,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
.get_all(id)
|
.get_all(id)
|
||||||
.map(|binding_id| self.binding(binding_id))
|
.map(|binding_id| self.binding(binding_id))
|
||||||
.filter(|binding| binding.start() >= expr.start())
|
.filter(|binding| binding.start() >= expr.start())
|
||||||
.all(|binding| !binding.is_used())
|
.all(Binding::is_unused)
|
||||||
}
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue