Fix panic on access to definitions after analyzing definitions#23588
Fix panic on access to definitions after analyzing definitions#23588
Conversation
Summary -- This PR fixes #23587 by removing this `std::mem::take` call: https://github.com/astral-sh/ruff/blob/a62ba8c6e2bac0b899d90fd30a1b26c07aac44bb/crates/ruff_linter/src/checkers/ast/analyze/definitions.rs#L120 This was previously moving the `Definitions` out of the `Checker` and causing this operation to panic if the definitions were accessed in a later analysis phase: https://github.com/astral-sh/ruff/blob/a62ba8c6e2bac0b899d90fd30a1b26c07aac44bb/crates/ruff_linter/src/checkers/ast/analyze/definitions.rs#L120 which was apparently never done before `PLR1712` was added. I don't see any reason why this really needed to be a move as it only took a couple of lifetimes to handle it with borrowing, so this seems like the easiest fix. I also changed the indexing operation to a `get` call (with a `debug_assert`) since the method already returns an `Option`, but doing that alone would prevent `PLR1712` from firing in the module scope. Test Plan -- A new test based on the issue that also covers the module scope mentioned above
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| ANN001 | 1374 | 1374 | 0 | 0 | 0 |
| COM812 | 836 | 836 | 0 | 0 | 0 |
| ANN201 | 815 | 815 | 0 | 0 | 0 |
| PLR6301 | 462 | 462 | 0 | 0 | 0 |
| DOC201 | 371 | 371 | 0 | 0 | 0 |
| panic: | 371 | 0 | 371 | 0 | 0 |
| D212 | 370 | 370 | 0 | 0 | 0 |
| ERA001 | 300 | 300 | 0 | 0 | 0 |
| ANN202 | 256 | 256 | 0 | 0 | 0 |
| D102 | 209 | 209 | 0 | 0 | 0 |
| TRY003 | 200 | 200 | 0 | 0 | 0 |
| PLC0415 | 197 | 197 | 0 | 0 | 0 |
| E265 | 180 | 180 | 0 | 0 | 0 |
| D103 | 163 | 163 | 0 | 0 | 0 |
| ARG002 | 161 | 161 | 0 | 0 | 0 |
| Q000 | 161 | 161 | 0 | 0 | 0 |
| CPY001 | 138 | 138 | 0 | 0 | 0 |
| RUF067 | 137 | 137 | 0 | 0 | 0 |
| ANN401 | 132 | 132 | 0 | 0 | 0 |
| S101 | 117 | 117 | 0 | 0 | 0 |
| PLR0917 | 108 | 108 | 0 | 0 | 0 |
| D205 | 105 | 105 | 0 | 0 | 0 |
| EM102 | 103 | 103 | 0 | 0 | 0 |
| EM101 | 100 | 100 | 0 | 0 | 0 |
| T201 | 99 | 99 | 0 | 0 | 0 |
| AIR311 | 98 | 98 | 0 | 0 | 0 |
| DOC501 | 97 | 97 | 0 | 0 | 0 |
| E501 | 93 | 93 | 0 | 0 | 0 |
| E302 | 89 | 89 | 0 | 0 | 0 |
| FBT001 | 86 | 86 | 0 | 0 | 0 |
| PTH118 | 82 | 82 | 0 | 0 | 0 |
| C408 | 82 | 82 | 0 | 0 | 0 |
| SLF001 | 73 | 73 | 0 | 0 | 0 |
| ANN204 | 73 | 73 | 0 | 0 | 0 |
| E402 | 72 | 72 | 0 | 0 | 0 |
| PLR6201 | 71 | 71 | 0 | 0 | 0 |
| D100 | 70 | 70 | 0 | 0 | 0 |
| PLR0913 | 70 | 70 | 0 | 0 | 0 |
| FBT002 | 67 | 67 | 0 | 0 | 0 |
| ANN003 | 66 | 66 | 0 | 0 | 0 |
| FIX002 | 65 | 65 | 0 | 0 | 0 |
| TD002 | 64 | 64 | 0 | 0 | 0 |
| AIR001 | 64 | 64 | 0 | 0 | 0 |
| TD003 | 63 | 63 | 0 | 0 | 0 |
| D401 | 63 | 63 | 0 | 0 | 0 |
| D101 | 62 | 62 | 0 | 0 | 0 |
| D107 | 61 | 61 | 0 | 0 | 0 |
| D210 | 58 | 58 | 0 | 0 | 0 |
| TID252 | 58 | 58 | 0 | 0 | 0 |
| ARG003 | 53 | 53 | 0 | 0 | 0 |
| D415 | 49 | 49 | 0 | 0 | 0 |
| PLR2004 | 48 | 48 | 0 | 0 | 0 |
| D400 | 48 | 48 | 0 | 0 | 0 |
| ARG001 | 44 | 44 | 0 | 0 | 0 |
| Q002 | 43 | 43 | 0 | 0 | 0 |
| C901 | 42 | 42 | 0 | 0 | 0 |
| DTZ001 | 42 | 42 | 0 | 0 | 0 |
| RET505 | 42 | 42 | 0 | 0 | 0 |
| D300 | 41 | 41 | 0 | 0 | 0 |
| RUF012 | 38 | 38 | 0 | 0 | 0 |
| TC003 | 36 | 36 | 0 | 0 | 0 |
| D105 | 34 | 34 | 0 | 0 | 0 |
| LOG015 | 32 | 32 | 0 | 0 | 0 |
| D200 | 32 | 32 | 0 | 0 | 0 |
| B904 | 30 | 30 | 0 | 0 | 0 |
| INP001 | 29 | 29 | 0 | 0 | 0 |
| PTH123 | 29 | 29 | 0 | 0 | 0 |
| PLR6104 | 28 | 28 | 0 | 0 | 0 |
| PLC2701 | 27 | 27 | 0 | 0 | 0 |
| RUF052 | 27 | 27 | 0 | 0 | 0 |
| PLR0912 | 26 | 26 | 0 | 0 | 0 |
| E261 | 26 | 26 | 0 | 0 | 0 |
| RUF036 | 25 | 25 | 0 | 0 | 0 |
| A002 | 24 | 24 | 0 | 0 | 0 |
| RET504 | 23 | 23 | 0 | 0 | 0 |
| D420 | 22 | 22 | 0 | 0 | 0 |
| PTH120 | 22 | 22 | 0 | 0 | 0 |
| PLW0603 | 22 | 22 | 0 | 0 | 0 |
| S106 | 21 | 21 | 0 | 0 | 0 |
| TC006 | 21 | 21 | 0 | 0 | 0 |
| E305 | 21 | 21 | 0 | 0 | 0 |
| PT007 | 20 | 20 | 0 | 0 | 0 |
| BLE001 | 19 | 19 | 0 | 0 | 0 |
| PLR0904 | 19 | 19 | 0 | 0 | 0 |
| E241 | 18 | 18 | 0 | 0 | 0 |
| E251 | 18 | 18 | 0 | 0 | 0 |
| D404 | 17 | 17 | 0 | 0 | 0 |
| FURB118 | 17 | 17 | 0 | 0 | 0 |
| PLW1514 | 17 | 17 | 0 | 0 | 0 |
| RUF031 | 16 | 16 | 0 | 0 | 0 |
| SIM108 | 16 | 16 | 0 | 0 | 0 |
| PGH003 | 16 | 16 | 0 | 0 | 0 |
| N815 | 16 | 16 | 0 | 0 | 0 |
| UP035 | 16 | 16 | 0 | 0 | 0 |
| TC002 | 16 | 16 | 0 | 0 | 0 |
| D202 | 16 | 16 | 0 | 0 | 0 |
| PLR0914 | 15 | 15 | 0 | 0 | 0 |
| PTH100 | 15 | 15 | 0 | 0 | 0 |
| ANN205 | 15 | 15 | 0 | 0 | 0 |
| RUF022 | 14 | 14 | 0 | 0 | 0 |
| FBT003 | 14 | 14 | 0 | 0 | 0 |
| PLR0915 | 14 | 14 | 0 | 0 | 0 |
| PLR1702 | 14 | 14 | 0 | 0 | 0 |
| DOC402 | 14 | 14 | 0 | 0 | 0 |
| TRY400 | 14 | 14 | 0 | 0 | 0 |
| PLW3201 | 14 | 14 | 0 | 0 | 0 |
| ANN002 | 13 | 13 | 0 | 0 | 0 |
| AIR321 | 13 | 13 | 0 | 0 | 0 |
| D209 | 13 | 13 | 0 | 0 | 0 |
| ISC004 | 12 | 12 | 0 | 0 | 0 |
| D204 | 12 | 12 | 0 | 0 | 0 |
| TD004 | 11 | 11 | 0 | 0 | 0 |
| SIM102 | 11 | 11 | 0 | 0 | 0 |
| SIM117 | 11 | 11 | 0 | 0 | 0 |
| PLC1901 | 11 | 11 | 0 | 0 | 0 |
| TRY300 | 10 | 10 | 0 | 0 | 0 |
| SIM103 | 10 | 10 | 0 | 0 | 0 |
| RSE102 | 10 | 10 | 0 | 0 | 0 |
| FURB101 | 10 | 10 | 0 | 0 | 0 |
| E266 | 10 | 10 | 0 | 0 | 0 |
| PLR0911 | 9 | 9 | 0 | 0 | 0 |
| RUF069 | 9 | 9 | 0 | 0 | 0 |
| PTH110 | 9 | 9 | 0 | 0 | 0 |
| D419 | 9 | 9 | 0 | 0 | 0 |
| FIX004 | 8 | 8 | 0 | 0 | 0 |
| TD006 | 8 | 8 | 0 | 0 | 0 |
| N806 | 8 | 8 | 0 | 0 | 0 |
| S108 | 8 | 8 | 0 | 0 | 0 |
| PLC2801 | 8 | 8 | 0 | 0 | 0 |
| PT019 | 8 | 8 | 0 | 0 | 0 |
| RET503 | 8 | 8 | 0 | 0 | 0 |
| UP006 | 8 | 8 | 0 | 0 | 0 |
| PTH103 | 8 | 8 | 0 | 0 | 0 |
| PERF401 | 7 | 7 | 0 | 0 | 0 |
| ANN206 | 7 | 7 | 0 | 0 | 0 |
| C416 | 7 | 7 | 0 | 0 | 0 |
| TRY004 | 7 | 7 | 0 | 0 | 0 |
| PTH119 | 7 | 7 | 0 | 0 | 0 |
| TC001 | 7 | 7 | 0 | 0 | 0 |
| TRY002 | 7 | 7 | 0 | 0 | 0 |
| RUF065 | 7 | 7 | 0 | 0 | 0 |
| UP031 | 7 | 7 | 0 | 0 | 0 |
| S105 | 6 | 6 | 0 | 0 | 0 |
| S404 | 6 | 6 | 0 | 0 | 0 |
| ARG004 | 6 | 6 | 0 | 0 | 0 |
| RUF043 | 6 | 6 | 0 | 0 | 0 |
| FURB189 | 6 | 6 | 0 | 0 | 0 |
| E221 | 6 | 6 | 0 | 0 | 0 |
| E201 | 6 | 6 | 0 | 0 | 0 |
| E202 | 6 | 6 | 0 | 0 | 0 |
| A001 | 6 | 6 | 0 | 0 | 0 |
| PLR5501 | 5 | 5 | 0 | 0 | 0 |
| FURB110 | 5 | 5 | 0 | 0 | 0 |
| TRY201 | 5 | 5 | 0 | 0 | 0 |
| S403 | 5 | 5 | 0 | 0 | 0 |
| ARG005 | 5 | 5 | 0 | 0 | 0 |
| PGH004 | 5 | 5 | 0 | 0 | 0 |
| UP045 | 5 | 5 | 0 | 0 | 0 |
| E226 | 5 | 5 | 0 | 0 | 0 |
| RUF070 | 5 | 5 | 0 | 0 | 0 |
| RUF001 | 5 | 5 | 0 | 0 | 0 |
| FURB103 | 5 | 5 | 0 | 0 | 0 |
| PLR0916 | 5 | 5 | 0 | 0 | 0 |
| FURB140 | 4 | 4 | 0 | 0 | 0 |
| PERF203 | 4 | 4 | 0 | 0 | 0 |
| RUF027 | 4 | 4 | 0 | 0 | 0 |
| PTH107 | 4 | 4 | 0 | 0 | 0 |
| TD005 | 4 | 4 | 0 | 0 | 0 |
| B009 | 4 | 4 | 0 | 0 | 0 |
| RUF100 | 4 | 4 | 0 | 0 | 0 |
| G201 | 4 | 4 | 0 | 0 | 0 |
| D413 | 4 | 4 | 0 | 0 | 0 |
| DOC202 | 4 | 4 | 0 | 0 | 0 |
| D208 | 4 | 4 | 0 | 0 | 0 |
| S603 | 4 | 4 | 0 | 0 | 0 |
| RUF029 | 4 | 4 | 0 | 0 | 0 |
| RUF003 | 4 | 4 | 0 | 0 | 0 |
| S607 | 4 | 4 | 0 | 0 | 0 |
| PTH207 | 4 | 4 | 0 | 0 | 0 |
| TRY301 | 3 | 3 | 0 | 0 | 0 |
| C419 | 3 | 3 | 0 | 0 | 0 |
| PTH111 | 3 | 3 | 0 | 0 | 0 |
| FURB154 | 3 | 3 | 0 | 0 | 0 |
| FURB142 | 3 | 3 | 0 | 0 | 0 |
| FURB113 | 3 | 3 | 0 | 0 | 0 |
| B905 | 3 | 3 | 0 | 0 | 0 |
| SIM118 | 3 | 3 | 0 | 0 | 0 |
| S608 | 3 | 3 | 0 | 0 | 0 |
| RET501 | 3 | 3 | 0 | 0 | 0 |
| PIE800 | 3 | 3 | 0 | 0 | 0 |
| TD001 | 3 | 3 | 0 | 0 | 0 |
| N802 | 3 | 3 | 0 | 0 | 0 |
| S113 | 3 | 3 | 0 | 0 | 0 |
| PLR1714 | 3 | 3 | 0 | 0 | 0 |
| D418 | 3 | 3 | 0 | 0 | 0 |
| I001 | 3 | 3 | 0 | 0 | 0 |
| PLW2901 | 3 | 3 | 0 | 0 | 0 |
| D417 | 3 | 3 | 0 | 0 | 0 |
| B007 | 3 | 3 | 0 | 0 | 0 |
| RET506 | 3 | 3 | 0 | 0 | 0 |
| PYI036 | 3 | 3 | 0 | 0 | 0 |
| PT018 | 3 | 3 | 0 | 0 | 0 |
| TRY401 | 2 | 2 | 0 | 0 | 0 |
| RUF021 | 2 | 2 | 0 | 0 | 0 |
| PTH117 | 2 | 2 | 0 | 0 | 0 |
| FLY002 | 2 | 2 | 0 | 0 | 0 |
| PYI024 | 2 | 2 | 0 | 0 | 0 |
| DTZ005 | 2 | 2 | 0 | 0 | 0 |
| PIE790 | 2 | 2 | 0 | 0 | 0 |
| SIM114 | 2 | 2 | 0 | 0 | 0 |
| PTH208 | 2 | 2 | 0 | 0 | 0 |
| SIM910 | 2 | 2 | 0 | 0 | 0 |
| D104 | 2 | 2 | 0 | 0 | 0 |
| RUF002 | 2 | 2 | 0 | 0 | 0 |
| SIM105 | 2 | 2 | 0 | 0 | 0 |
| PIE808 | 2 | 2 | 0 | 0 | 0 |
| PD011 | 2 | 2 | 0 | 0 | 0 |
| DOC102 | 2 | 2 | 0 | 0 | 0 |
| D412 | 2 | 2 | 0 | 0 | 0 |
| PYI016 | 2 | 2 | 0 | 0 | 0 |
| FIX003 | 2 | 2 | 0 | 0 | 0 |
| S314 | 2 | 2 | 0 | 0 | 0 |
| ICN001 | 2 | 2 | 0 | 0 | 0 |
| N813 | 2 | 2 | 0 | 0 | 0 |
| S405 | 2 | 2 | 0 | 0 | 0 |
| B903 | 2 | 2 | 0 | 0 | 0 |
| DOC502 | 2 | 2 | 0 | 0 | 0 |
| F541 | 2 | 2 | 0 | 0 | 0 |
| PYI059 | 2 | 2 | 0 | 0 | 0 |
| LOG004 | 2 | 2 | 0 | 0 | 0 |
| PTH108 | 2 | 2 | 0 | 0 | 0 |
| SIM113 | 2 | 2 | 0 | 0 | 0 |
| PTH116 | 2 | 2 | 0 | 0 | 0 |
| PLC0414 | 2 | 2 | 0 | 0 | 0 |
| PTH113 | 2 | 2 | 0 | 0 | 0 |
| RUF059 | 1 | 1 | 0 | 0 | 0 |
| N817 | 1 | 1 | 0 | 0 | 0 |
| PLR1730 | 1 | 1 | 0 | 0 | 0 |
| N805 | 1 | 1 | 0 | 0 | 0 |
| EXE001 | 1 | 1 | 0 | 0 | 0 |
| B018 | 1 | 1 | 0 | 0 | 0 |
| DTZ901 | 1 | 1 | 0 | 0 | 0 |
| AIR002 | 1 | 1 | 0 | 0 | 0 |
| N803 | 1 | 1 | 0 | 0 | 0 |
| SIM401 | 1 | 1 | 0 | 0 | 0 |
| FURB129 | 1 | 1 | 0 | 0 | 0 |
| FURB171 | 1 | 1 | 0 | 0 | 0 |
| C417 | 1 | 1 | 0 | 0 | 0 |
| PTH106 | 1 | 1 | 0 | 0 | 0 |
| PLR0402 | 1 | 1 | 0 | 0 | 0 |
| FIX001 | 1 | 1 | 0 | 0 | 0 |
| C401 | 1 | 1 | 0 | 0 | 0 |
| S301 | 1 | 1 | 0 | 0 | 0 |
| PLR1736 | 1 | 1 | 0 | 0 | 0 |
| SIM201 | 1 | 1 | 0 | 0 | 0 |
| SIM110 | 1 | 1 | 0 | 0 | 0 |
| RUF005 | 1 | 1 | 0 | 0 | 0 |
| S104 | 1 | 1 | 0 | 0 | 0 |
| PYI032 | 1 | 1 | 0 | 0 | 0 |
| PLW1508 | 1 | 1 | 0 | 0 | 0 |
| UP007 | 1 | 1 | 0 | 0 | 0 |
| RUF015 | 1 | 1 | 0 | 0 | 0 |
| UP034 | 1 | 1 | 0 | 0 | 0 |
| F401 | 1 | 1 | 0 | 0 | 0 |
| NPY002 | 1 | 1 | 0 | 0 | 0 |
| E225 | 1 | 1 | 0 | 0 | 0 |
| B010 | 1 | 1 | 0 | 0 | 0 |
| Q001 | 1 | 1 | 0 | 0 | 0 |
| PYI030 | 1 | 1 | 0 | 0 | 0 |
| B006 | 1 | 1 | 0 | 0 | 0 |
| N812 | 1 | 1 | 0 | 0 | 0 |
| E303 | 1 | 1 | 0 | 0 | 0 |
| RET508 | 1 | 1 | 0 | 0 | 0 |
| E301 | 1 | 1 | 0 | 0 | 0 |
| E227 | 1 | 1 | 0 | 0 | 0 |
| PTH109 | 1 | 1 | 0 | 0 | 0 |
| ISC003 | 1 | 1 | 0 | 0 | 0 |
| FURB188 | 1 | 1 | 0 | 0 | 0 |
| E203 | 1 | 1 | 0 | 0 | 0 |
| FURB148 | 1 | 1 | 0 | 0 | 0 |
| ICN002 | 1 | 1 | 0 | 0 | 0 |
| PYI013 | 1 | 1 | 0 | 0 | 0 |
| PYI061 | 1 | 1 | 0 | 0 | 0 |
| SIM101 | 1 | 1 | 0 | 0 | 0 |
| PYI063 | 1 | 1 | 0 | 0 | 0 |
| PYI019 | 1 | 1 | 0 | 0 | 0 |
| S324 | 1 | 1 | 0 | 0 | 0 |
| S606 | 1 | 1 | 0 | 0 | 0 |
| PTH211 | 1 | 1 | 0 | 0 | 0 |
| S311 | 1 | 1 | 0 | 0 | 0 |
| PTH122 | 1 | 1 | 0 | 0 | 0 |
| PTH202 | 1 | 1 | 0 | 0 | 0 |
| D106 | 1 | 1 | 0 | 0 | 0 |
| D403 | 1 | 1 | 0 | 0 | 0 |
| RUF056 | 1 | 1 | 0 | 0 | 0 |
| B901 | 1 | 1 | 0 | 0 | 0 |
| S402 | 1 | 1 | 0 | 0 | 0 |
|
Well, I was wondering how this slipped through the ecosystem check, and I guess I either didn't notice or assumed it was a weird flake because it definitely showed up: #22205 (comment) 🤦 Maybe I should take a closer look this time as to why the numbers don't match up exactly (+10991 -371 here and +405 -32412 there), but the -371 panics here seems like a good start. |
|
#23592 (comment) shows the exact same ecosystem totals as seen here for a revert of #22205, so I think the other differences from the ecosystem report in #22205 are attributable to the change in the default rules. |
Summary
This PR fixes #23587 by removing this
std::mem::takecall:ruff/crates/ruff_linter/src/checkers/ast/analyze/definitions.rs
Line 120 in a62ba8c
This was previously moving the
Definitionsout of theCheckerand causingthis operation to panic if the definitions were accessed in a later analysis
phase:
ruff/crates/ruff_linter/src/checkers/ast/analyze/definitions.rs
Line 120 in a62ba8c
which was apparently never done before
PLR1712was added.I don't see any reason why this really needed to be a move as it only took a
couple of lifetimes to handle it with borrowing, so this seems like the easiest
fix.
I also changed the indexing operation to a
getcall (with adebug_assert)since the method already returns an
Option, but doing that alone would preventPLR1712from firing in the module scope.Test Plan
A new test based on the issue that also covers the module scope mentioned above