Fix remaining CPython formatter errors except for function argument separator comments (#5210)

## Summary

This fixes two problems discovered when trying to format the cpython
repo with `cargo run --bin ruff_dev -- check-formatter-stability
projects/cpython`:

The first is to ignore try/except trailing comments for now since they
lead to unstable formatting on the dummy.

The second is to avoid dropping trailing if comments through placement:
This changes the placement to keep a comment trailing an if-elif or
if-elif-else to keep the comment a trailing comment on the entire if.
Previously the last comment would have been lost.
```python
if "first if":
    pass
elif "first elif":
    pass
```

The last remaining problem in cpython so far is function signature
argument separator comment placement which is its own PR on top of this.

## Test Plan

I added test fixtures of minimized examples with links back to the
original cpython location
This commit is contained in:
konstin 2023-06-21 19:45:53 +02:00 committed by GitHub
parent bf1a94ee54
commit bc63cc9b3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 148 additions and 34 deletions

View file

@ -35,3 +35,30 @@ elif aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
else:
...
# Regression test: Don't drop the trailing comment by associating it with the elif
# instead of the else.
# Originally found in https://github.com/python/cpython/blob/ab3823a97bdeefb0266b3c8d493f7f6223ce3686/Lib/dataclasses.py#L539
if "if 1":
pass
elif "elif 1":
pass
# Don't drop this comment 1
x = 1
if "if 2":
pass
elif "elif 2":
pass
else:
pass
# Don't drop this comment 2
x = 2
if "if 3":
pass
else:
pass
# Don't drop this comment 3
x = 3