|
1 | 1 | use ruff_diagnostics::{Diagnostic, Fix, FixAvailability, Violation};
|
2 | 2 | use ruff_macros::{derive_message_formats, ViolationMetadata};
|
3 | 3 | use ruff_python_ast::helpers::is_dunder;
|
4 |
| -use ruff_python_semantic::{Binding, BindingKind, ScopeId}; |
| 4 | +use ruff_python_semantic::{Binding, ScopeId}; |
5 | 5 | use ruff_python_stdlib::{
|
6 | 6 | builtins::is_python_builtin, identifiers::is_identifier, keyword::is_keyword,
|
7 | 7 | };
|
@@ -93,13 +93,27 @@ pub(crate) fn used_dummy_variable(checker: &Checker, binding: &Binding) -> Optio
|
93 | 93 | if binding.is_unused() {
|
94 | 94 | return None;
|
95 | 95 | }
|
96 |
| - // Only variables defined via function arguments or assignments. |
97 |
| - if !matches!( |
98 |
| - binding.kind, |
99 |
| - BindingKind::Argument | BindingKind::Assignment |
100 |
| - ) { |
| 96 | + |
| 97 | + // We only emit the lint on variables defined via assignments. |
| 98 | + // |
| 99 | + // ## Why not also emit the lint on function parameters? |
| 100 | + // |
| 101 | + // There isn't universal agreement that leading underscores indicate "unused" parameters |
| 102 | + // in Python (many people use them for "private" parameters), so this would be a lot more |
| 103 | + // controversial than emitting the lint on assignments. Even if it's decided that it's |
| 104 | + // desirable to emit a lint on function parameters with "dummy variable" names, it would |
| 105 | + // possibly have to be a separate rule or we'd have to put it behind a configuration flag, |
| 106 | + // as there's much less community consensus about the issue. |
| 107 | + // See <https://github.com/astral-sh/ruff/issues/14796>. |
| 108 | + // |
| 109 | + // Moreover, autofixing the diagnostic for function parameters is much more troublesome than |
| 110 | + // autofixing the diagnostic for assignments. See: |
| 111 | + // - <https://github.com/astral-sh/ruff/issues/14790> |
| 112 | + // - <https://github.com/astral-sh/ruff/issues/14799> |
| 113 | + if !binding.kind.is_assignment() { |
101 | 114 | return None;
|
102 | 115 | }
|
| 116 | + |
103 | 117 | // This excludes `global` and `nonlocal` variables.
|
104 | 118 | if binding.is_global() || binding.is_nonlocal() {
|
105 | 119 | return None;
|
|
0 commit comments