mirror of
https://github.com/astral-sh/ruff.git
synced 2026-10-28 19:00:26 +08:00
In #28092 I fixed a bug where ty's redundant-cast removal fix could create broken code if the original `cast()` call was multi-line. Doug commented that this could perhaps be a general-purpose utility -- and it turned out there were two existing similar Ruff rules with pre-existing multi-line support that could benefit from such a shared utility. This PR introduces a shared utility in `ruff_python_edits` for removing a function call in favor of one of its arguments. This is the common shape between redundant-cast removal, redundant `int()` removal, and redundant `round()` removal. The new utility is more robust than the previous code for any of the three cases. Previously removing redundant `int`, `round`, and `typing.cast` calls could discard parentheses needed for line continuation or for token separation. For example, `int(1).to_bytes()` could become `1.to_bytes()`, which is invalid. The shared helper preserves optional argument parentheses and the comments inside them, adds grouping where required, and avoids redundant parentheses when the surrounding syntax already allows the expression. Ruff and ty retain their own diagnostic eligibility and fix-safety decisions. ## Test plan - Extend RUF046 and RUF057 fixtures for split callees, assignment expressions, integer attributes, adjacent keywords, and comments inside optional parentheses. Existing snapshot cases retain their fix text and safety classifications. - Add compact helper tests in `ruff_python_edits` for conditional callees, comprehension iterables, and dictionary expressions inside f-strings. - Existing ty cast mdtests cover positional and keyword arguments, operator precedence, multiline values, and comment preservation.
Integration tests for ruff_python_ast
This crate includes integration tests for the ruff_python_ast crate.
The reason for having a separate crate is to avoid introducing a dev circular
dependency between the ruff_python_parser crate and the ruff_python_ast crate.
This crate shouldn't include any code, only tests.
Reference:
rust-analyzerissue: https://github.com/rust-lang/rust-analyzer/issues/3390- Ruff's pull request: https://github.com/astral-sh/ruff/pull/11261