ruff/crates/ruff_linter/src
Samuel Cormier-Iijima c306f85691
F841: support fixing unused assignments in tuples by renaming variables (#9107)
## Summary

A fairly common pattern which triggers F841 is unused variables from
tuple assignments, e.g.:

    user, created = User.objects.get_or_create(...)
          ^ F841: Local variable `created` is assigned to but never used

This error is currently not auto-fixable.

This PR adds support for fixing the error automatically by renaming the
unused variable to have a leading underscore (i.e. `_created`) **iff**
the `dummy-variable-rgx` setting would match it.

I considered using `renamers::Renamer` here, but because by the nature
of the error there should be no references to it, that seemed like
overkill. Also note that the fix might break by shadowing the new name
if it is already used elsewhere in the scope. I left it as is because

1. the renamed variable matches the "unused" regex, so it should
hopefully not already be used,
2. the fix is marked as unsafe so it should be reviewed manually
anyways, and
3. I'm not actually sure how to check the scope for the new variable
name 😅
2023-12-12 13:23:46 -05:00
..
checkers Allow matplotlib.use calls to intersperse imports (#9094) 2023-12-11 17:06:25 +00:00
comments Rename ruff crate to ruff_linter (#7529) 2023-09-20 08:38:27 +02:00
cst Update string nodes for implicit concatenation (#7927) 2023-11-24 17:55:41 -06:00
docstrings Split Constant to individual literal nodes (#8064) 2023-10-30 12:13:23 +05:30
fix [pylint] Add add_argument utility and autofix for PLW1514 (#8928) 2023-12-01 18:23:56 +00:00
importer Update to Rust 1.74 and use new clippy lints table (#8722) 2023-11-16 18:12:46 -05:00
lex Rename ruff crate to ruff_linter (#7529) 2023-09-20 08:38:27 +02:00
message Update to Rust 1.74 and use new clippy lints table (#8722) 2023-11-16 18:12:46 -05:00
registry [refurb] Implement implicit-cwd (FURB177) (#7704) 2023-09-29 02:18:59 +00:00
rules F841: support fixing unused assignments in tuples by renaming variables (#9107) 2023-12-12 13:23:46 -05:00
settings Hide unsafe fix suggestions when explicitly disabled (#9095) 2023-12-11 15:42:53 -06:00
snapshots Allow transparent cell magics (#8911) 2023-12-07 14:15:43 -06:00
codes.rs [refurb] Implement hashlib-digest-hex (FURB181) (#9077) 2023-12-10 02:00:11 +00:00
directives.rs Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
doc_lines.rs Split Constant to individual literal nodes (#8064) 2023-10-30 12:13:23 +05:30
fs.rs Rename ruff crate to ruff_linter (#7529) 2023-09-20 08:38:27 +02:00
lib.rs Rename Autofix to Fix (#7657) 2023-09-28 10:53:05 +00:00
line_width.rs Rename tab-size to indent-width (#8082) 2023-10-24 10:01:24 -04:00
linter.rs Avoid E703 for last expression in a cell (#8821) 2023-11-23 07:40:57 -06:00
logging.rs Use OneIndexed in NotebookIndex (#7921) 2023-10-13 06:23:49 +05:30
noqa.rs add instructions on line-level suppression to file-level suppression warning (#8052) 2023-10-18 18:46:59 -04:00
packaging.rs Rename ruff crate to ruff_linter (#7529) 2023-09-20 08:38:27 +02:00
pyproject_toml.rs Update pyproject-toml to 0.8 (#8351) 2023-10-30 10:05:37 +00:00
registry.rs Update string nodes for implicit concatenation (#7927) 2023-11-24 17:55:41 -06:00
renamer.rs Add a BindingKind for WithItem variables (#8594) 2023-11-09 22:44:49 -05:00
rule_redirects.rs Rename ruff crate to ruff_linter (#7529) 2023-09-20 08:38:27 +02:00
rule_selector.rs Add explicit-preview-rules to toggle explicit selection of preview rules (#7390) 2023-09-28 15:00:33 -05:00
source_kind.rs Use SourceKind::diff for formatter (#8240) 2023-10-26 11:08:13 +05:30
test.rs Treat display as a builtin in IPython (#8707) 2023-11-16 01:58:44 +00:00
upstream_categories.rs Rename ruff crate to ruff_linter (#7529) 2023-09-20 08:38:27 +02:00