|
| 1 | +--- |
| 2 | +source: crates/ruff_linter/src/rules/flake8_return/mod.rs |
| 3 | +assertion_line: 34 |
| 4 | +--- |
| 5 | +RET504.py:6:12: RET504 [*] Unnecessary assignment to `a` before `return` statement |
| 6 | + | |
| 7 | +4 | def x(): |
| 8 | +5 | a = 1 |
| 9 | +6 | return a # RET504 |
| 10 | + | ^ RET504 |
| 11 | + | |
| 12 | + = help: Remove unnecessary assignment |
| 13 | + |
| 14 | +ℹ Unsafe fix |
| 15 | +2 2 | # Errors |
| 16 | +3 3 | ### |
| 17 | +4 4 | def x(): |
| 18 | +5 |- a = 1 |
| 19 | +6 |- return a # RET504 |
| 20 | + 5 |+ return 1 |
| 21 | +7 6 | |
| 22 | +8 7 | |
| 23 | +9 8 | # Can be refactored false positives |
| 24 | + |
| 25 | +RET504.py:23:12: RET504 [*] Unnecessary assignment to `formatted` before `return` statement |
| 26 | + | |
| 27 | +21 | # clean up after any blank components |
| 28 | +22 | formatted = formatted.replace("()", "").replace(" ", " ").strip() |
| 29 | +23 | return formatted |
| 30 | + | ^^^^^^^^^ RET504 |
| 31 | + | |
| 32 | + = help: Remove unnecessary assignment |
| 33 | + |
| 34 | +ℹ Unsafe fix |
| 35 | +19 19 | def x(): |
| 36 | +20 20 | formatted = _USER_AGENT_FORMATTER.format(format_string, **values) |
| 37 | +21 21 | # clean up after any blank components |
| 38 | +22 |- formatted = formatted.replace("()", "").replace(" ", " ").strip() |
| 39 | +23 |- return formatted |
| 40 | + 22 |+ return formatted.replace("()", "").replace(" ", " ").strip() |
| 41 | +24 23 | |
| 42 | +25 24 | |
| 43 | +26 25 | # https://github.com/afonasev/flake8-return/issues/47#issue-641117366 |
| 44 | + |
| 45 | +RET504.py:246:12: RET504 [*] Unnecessary assignment to `queryset` before `return` statement |
| 46 | + | |
| 47 | +244 | queryset = Model.filter(a=1) |
| 48 | +245 | queryset = queryset.filter(c=3) |
| 49 | +246 | return queryset |
| 50 | + | ^^^^^^^^ RET504 |
| 51 | + | |
| 52 | + = help: Remove unnecessary assignment |
| 53 | + |
| 54 | +ℹ Unsafe fix |
| 55 | +242 242 | |
| 56 | +243 243 | def get_queryset(): |
| 57 | +244 244 | queryset = Model.filter(a=1) |
| 58 | +245 |- queryset = queryset.filter(c=3) |
| 59 | +246 |- return queryset |
| 60 | + 245 |+ return queryset.filter(c=3) |
| 61 | +247 246 | |
| 62 | +248 247 | |
| 63 | +249 248 | def get_queryset(): |
| 64 | + |
| 65 | +RET504.py:251:12: RET504 [*] Unnecessary assignment to `queryset` before `return` statement |
| 66 | + | |
| 67 | +249 | def get_queryset(): |
| 68 | +250 | queryset = Model.filter(a=1) |
| 69 | +251 | return queryset # RET504 |
| 70 | + | ^^^^^^^^ RET504 |
| 71 | + | |
| 72 | + = help: Remove unnecessary assignment |
| 73 | + |
| 74 | +ℹ Unsafe fix |
| 75 | +247 247 | |
| 76 | +248 248 | |
| 77 | +249 249 | def get_queryset(): |
| 78 | +250 |- queryset = Model.filter(a=1) |
| 79 | +251 |- return queryset # RET504 |
| 80 | + 250 |+ return Model.filter(a=1) |
| 81 | +252 251 | |
| 82 | +253 252 | |
| 83 | +254 253 | # Function arguments |
| 84 | + |
| 85 | +RET504.py:269:12: RET504 [*] Unnecessary assignment to `val` before `return` statement |
| 86 | + | |
| 87 | +267 | return val |
| 88 | +268 | val = 1 |
| 89 | +269 | return val # RET504 |
| 90 | + | ^^^ RET504 |
| 91 | + | |
| 92 | + = help: Remove unnecessary assignment |
| 93 | + |
| 94 | +ℹ Unsafe fix |
| 95 | +265 265 | def str_to_bool(val): |
| 96 | +266 266 | if isinstance(val, bool): |
| 97 | +267 267 | return val |
| 98 | +268 |- val = 1 |
| 99 | +269 |- return val # RET504 |
| 100 | + 268 |+ return 1 |
| 101 | +270 269 | |
| 102 | +271 270 | |
| 103 | +272 271 | def str_to_bool(val): |
| 104 | + |
| 105 | +RET504.py:321:12: RET504 [*] Unnecessary assignment to `x` before `return` statement |
| 106 | + | |
| 107 | +319 | with open("foo.txt", "r") as f: |
| 108 | +320 | x = f.read() |
| 109 | +321 | return x # RET504 |
| 110 | + | ^ RET504 |
| 111 | + | |
| 112 | + = help: Remove unnecessary assignment |
| 113 | + |
| 114 | +ℹ Unsafe fix |
| 115 | +317 317 | # `with` statements |
| 116 | +318 318 | def foo(): |
| 117 | +319 319 | with open("foo.txt", "r") as f: |
| 118 | +320 |- x = f.read() |
| 119 | +321 |- return x # RET504 |
| 120 | + 320 |+ return f.read() |
| 121 | +322 321 | |
| 122 | +323 322 | |
| 123 | +324 323 | def foo(): |
| 124 | + |
| 125 | +RET504.py:342:12: RET504 [*] Unnecessary assignment to `b` before `return` statement |
| 126 | + | |
| 127 | +340 | a = 1 |
| 128 | +341 | b=a |
| 129 | +342 | return b # RET504 |
| 130 | + | ^ RET504 |
| 131 | + | |
| 132 | + = help: Remove unnecessary assignment |
| 133 | + |
| 134 | +ℹ Unsafe fix |
| 135 | +338 338 | # Fix cases |
| 136 | +339 339 | def foo(): |
| 137 | +340 340 | a = 1 |
| 138 | +341 |- b=a |
| 139 | +342 |- return b # RET504 |
| 140 | + 341 |+ return a |
| 141 | +343 342 | |
| 142 | +344 343 | |
| 143 | +345 344 | def foo(): |
| 144 | + |
| 145 | +RET504.py:348:12: RET504 [*] Unnecessary assignment to `b` before `return` statement |
| 146 | + | |
| 147 | +346 | a = 1 |
| 148 | +347 | b =a |
| 149 | +348 | return b # RET504 |
| 150 | + | ^ RET504 |
| 151 | + | |
| 152 | + = help: Remove unnecessary assignment |
| 153 | + |
| 154 | +ℹ Unsafe fix |
| 155 | +344 344 | |
| 156 | +345 345 | def foo(): |
| 157 | +346 346 | a = 1 |
| 158 | +347 |- b =a |
| 159 | +348 |- return b # RET504 |
| 160 | + 347 |+ return a |
| 161 | +349 348 | |
| 162 | +350 349 | |
| 163 | +351 350 | def foo(): |
| 164 | + |
| 165 | +RET504.py:354:12: RET504 [*] Unnecessary assignment to `b` before `return` statement |
| 166 | + | |
| 167 | +352 | a = 1 |
| 168 | +353 | b= a |
| 169 | +354 | return b # RET504 |
| 170 | + | ^ RET504 |
| 171 | + | |
| 172 | + = help: Remove unnecessary assignment |
| 173 | + |
| 174 | +ℹ Unsafe fix |
| 175 | +350 350 | |
| 176 | +351 351 | def foo(): |
| 177 | +352 352 | a = 1 |
| 178 | +353 |- b= a |
| 179 | +354 |- return b # RET504 |
| 180 | + 353 |+ return a |
| 181 | +355 354 | |
| 182 | +356 355 | |
| 183 | +357 356 | def foo(): |
| 184 | + |
| 185 | +RET504.py:359:12: RET504 [*] Unnecessary assignment to `a` before `return` statement |
| 186 | + | |
| 187 | +357 | def foo(): |
| 188 | +358 | a = 1 # Comment |
| 189 | +359 | return a |
| 190 | + | ^ RET504 |
| 191 | + | |
| 192 | + = help: Remove unnecessary assignment |
| 193 | + |
| 194 | +ℹ Unsafe fix |
| 195 | +355 355 | |
| 196 | +356 356 | |
| 197 | +357 357 | def foo(): |
| 198 | +358 |- a = 1 # Comment |
| 199 | +359 |- return a |
| 200 | + 358 |+ return 1 # Comment |
| 201 | +360 359 | |
| 202 | +361 360 | |
| 203 | +362 361 | # Regression test for: https://github.com/astral-sh/ruff/issues/7098 |
| 204 | + |
| 205 | +RET504.py:365:12: RET504 [*] Unnecessary assignment to `D` before `return` statement |
| 206 | + | |
| 207 | +363 | def mavko_debari(P_kbar): |
| 208 | +364 | D=0.4853881 + 3.6006116*P - 0.0117368*(P-1.3822)**2 |
| 209 | +365 | return D |
| 210 | + | ^ RET504 |
| 211 | + | |
| 212 | + = help: Remove unnecessary assignment |
| 213 | + |
| 214 | +ℹ Unsafe fix |
| 215 | +361 361 | |
| 216 | +362 362 | # Regression test for: https://github.com/astral-sh/ruff/issues/7098 |
| 217 | +363 363 | def mavko_debari(P_kbar): |
| 218 | +364 |- D=0.4853881 + 3.6006116*P - 0.0117368*(P-1.3822)**2 |
| 219 | +365 |- return D |
| 220 | + 364 |+ return 0.4853881 + 3.6006116*P - 0.0117368*(P-1.3822)**2 |
| 221 | +366 365 | |
| 222 | +367 366 | |
| 223 | +368 367 | # contextlib suppress in with statement |
| 224 | + |
| 225 | +RET504.py:400:12: RET504 [*] Unnecessary assignment to `y` before `return` statement |
| 226 | + | |
| 227 | +398 | x = 1 |
| 228 | +399 | y = y + 2 |
| 229 | +400 | return y # RET504 |
| 230 | + | ^ RET504 |
| 231 | + | |
| 232 | + = help: Remove unnecessary assignment |
| 233 | + |
| 234 | +ℹ Unsafe fix |
| 235 | +396 396 | x = 2 |
| 236 | +397 397 | with contextlib.suppress(Exception): |
| 237 | +398 398 | x = 1 |
| 238 | +399 |- y = y + 2 |
| 239 | +400 |- return y # RET504 |
| 240 | + 399 |+ return y + 2 |
| 241 | +401 400 | |
| 242 | +402 401 | |
| 243 | +403 402 | def foo(): |
| 244 | + |
| 245 | +RET504.py:423:16: RET504 [*] Unnecessary assignment to `services` before `return` statement |
| 246 | + | |
| 247 | +421 | if "services" in a: |
| 248 | +422 | services = a["services"] |
| 249 | +423 | return services |
| 250 | + | ^^^^^^^^ RET504 |
| 251 | +424 | |
| 252 | +425 | # See: https://github.com/astral-sh/ruff/issues/18411 |
| 253 | + | |
| 254 | + = help: Remove unnecessary assignment |
| 255 | + |
| 256 | +ℹ Unsafe fix |
| 257 | +419 419 | # See: https://github.com/astral-sh/ruff/issues/10732 |
| 258 | +420 420 | def func(a: dict[str, int]) -> list[dict[str, int]]: |
| 259 | +421 421 | if "services" in a: |
| 260 | +422 |- services = a["services"] |
| 261 | +423 |- return services |
| 262 | + 422 |+ return a["services"] |
| 263 | +424 423 | |
| 264 | +425 424 | # See: https://github.com/astral-sh/ruff/issues/18411 |
| 265 | +426 425 | def f(): |
| 266 | + |
| 267 | +RET504.py:429:12: RET504 [*] Unnecessary assignment to `x` before `return` statement |
| 268 | + | |
| 269 | +427 | (#= |
| 270 | +428 | x) = 1 |
| 271 | +429 | return x |
| 272 | + | ^ RET504 |
| 273 | +430 | |
| 274 | +431 | def f(): |
| 275 | + | |
| 276 | + = help: Remove unnecessary assignment |
| 277 | + |
| 278 | +ℹ Unsafe fix |
| 279 | +424 424 | |
| 280 | +425 425 | # See: https://github.com/astral-sh/ruff/issues/18411 |
| 281 | +426 426 | def f(): |
| 282 | +427 |- (#= |
| 283 | +428 |- x) = 1 |
| 284 | +429 |- return x |
| 285 | + 427 |+ return 1 |
| 286 | +430 428 | |
| 287 | +431 429 | def f(): |
| 288 | +432 430 | (#= |
| 289 | + |
| 290 | +RET504.py:435:9: RET504 [*] Unnecessary assignment to `x` before `return` statement |
| 291 | + | |
| 292 | +433 | x) = 1 |
| 293 | +434 | return ( |
| 294 | +435 | x |
| 295 | + | ^ RET504 |
| 296 | +436 | ) |
| 297 | + | |
| 298 | + = help: Remove unnecessary assignment |
| 299 | + |
| 300 | +ℹ Unsafe fix |
| 301 | +429 429 | return x |
| 302 | +430 430 | |
| 303 | +431 431 | def f(): |
| 304 | +432 |- (#= |
| 305 | +433 |- x) = 1 |
| 306 | +434 |- return ( |
| 307 | +435 |- x |
| 308 | +436 |- ) |
| 309 | + 432 |+ return 1 |
0 commit comments