Repository: asottile/add-trailing-comma

Issue:
False negative with statement ending with parens
```python
if (foo and
    bar()):
    pass
```

I expect to be rewritten to

```python
if (
    foo and
    bar()
):
    pass
```

But I imagine the same code that prevents this from being rewritten is firing:

```python
foo('bar {}'.format(
    'baz',
))
```

which I don't think should be rewritten.

Need to come up with a more clever heuristic here.

Discussion:

Patch:
diff --git a/add_trailing_comma.py b/add_trailing_comma.py
index 9eaa33f..34e1012 100644
--- a/add_trailing_comma.py
+++ b/add_trailing_comma.py
@@ -271,7 +271,7 @@ def _fix_brace(fix_data, add_comma, tokens):
             # Don't unhug single element things with a multi-line component
             # inside.
             not fix_data.multi_arg and
-            hug_open and
+            tokens[first_brace + 1].src in START_BRACES and
             tokens[last_brace - 1].src in END_BRACES or
             # Don't unhug when containing a single token (such as a triple
             # quoted string).


Test patch:
diff --git a/tests/add_trailing_comma_test.py b/tests/add_trailing_comma_test.py
index fa4593d..9fde7b4 100644
--- a/tests/add_trailing_comma_test.py
+++ b/tests/add_trailing_comma_test.py
@@ -594,6 +594,17 @@ def test_noop_unhugs(src):
             '    )\n'
             ']',
         ),
+        pytest.param(
+            'x = [x\n'
+            '     for x in y()]\n',
+
+            'x = [\n'
+            '    x\n'
+            '    for x in y()\n'
+            ']\n',
+
+            id='#42: listcomp unhug ends in brace',
+        ),
     ),
 )
 def test_fix_unhugs(src, expected):