diff --git a/pyupgrade.py b/pyupgrade.py
index d2a6990..eafc8a3 100644
--- a/pyupgrade.py
+++ b/pyupgrade.py
@@ -46,6 +46,7 @@ def unparse_parsed_string(parsed):
 
 
 UNIMPORTANT_WS = 'UNIMPORTANT_WS'
+NON_CODING_TOKENS = frozenset(('COMMENT', 'NL', UNIMPORTANT_WS))
 Token = collections.namedtuple(
     'Token', ('name', 'src', 'line', 'utf8_byte_offset'),
 )
@@ -145,7 +146,7 @@ def _fix_format_literals(contents_text):
             to_replace.append((string_start, string_end))
             string_start, string_end, seen_dot = None, None, False
         # NL is the non-breaking newline token
-        elif token.name not in ('COMMENT', 'NL', UNIMPORTANT_WS):
+        elif token.name not in NON_CODING_TOKENS:
             string_start, string_end, seen_dot = None, None, False
 
     for start, end in reversed(to_replace):
@@ -291,6 +292,13 @@ def _get_victims(tokens, start, arg):
 
         i += 1
 
+    # May need to remove a trailing comma
+    i -= 2
+    while tokens[i].name in NON_CODING_TOKENS:
+        i -= 1
+    if tokens[i].src == ',':
+        ends = sorted(set(ends + [i]))
+
     return Victims(starts, ends, first_comma_index, arg_index)
 
 

SyntaxError rewriting dict(thing,) or set(thing,) (trailing comma)
For example

```python
>>> set(x for x in range(1),)
{0}
>>> {x for x in range(1),}
  File "<stdin>", line 1
    {x for x in range(1),}
                        ^
SyntaxError: invalid syntax
```
diff --git a/tests/pyupgrade_test.py b/tests/pyupgrade_test.py
index b2ff30f..7b48712 100644
--- a/tests/pyupgrade_test.py
+++ b/tests/pyupgrade_test.py
@@ -130,6 +130,17 @@ def test_roundtrip_tokenize(filename):
         # Remove trailing commas on inline things
         ('set((1,))', '{1}'),
         ('set((1, ))', '{1}'),
+        # Remove trailing commas after things
+        ('set([1, 2, 3,],)', '{1, 2, 3}'),
+        ('set(x for x in y,)', '{x for x in y}'),
+        (
+            'set(\n'
+            '    x for x in y,\n'
+            ')',
+            '{\n'
+            '    x for x in y\n'
+            '}',
+        ),
     ),
 )
 def test_sets(s, expected):
@@ -167,6 +178,25 @@ def test_sets(s, expected):
         ),
         # This doesn't get fixed by autopep8 and can cause a syntax error
         ('dict((a, b)for a, b in y)', '{a: b for a, b in y}'),
+        # Need to remove trailing commas on the element
+        (
+            'dict(\n'
+            '    (\n'
+            '        a,\n'
+            '        b,\n'
+            '    )\n'
+            '    for a, b in y\n'
+            ')',
+            # Ideally, this'll go through some other formatting tool before
+            # being committed.  Shrugs!
+            '{\n'
+            '    \n'
+            '        a:\n'
+            '        b\n'
+            '    \n'
+            '    for a, b in y\n'
+            '}',
+        ),
     ),
 )
 def test_dictcomps(s, expected):

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest
3.6
requirements-dev.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs==22.2.0
certifi==2021.5.30
cfgv==3.3.1
coverage==6.2
distlib==0.3.9
filelock==3.4.1
flake8==5.0.4
identify==2.4.4
importlib-metadata==4.2.0
importlib-resources==5.2.3
iniconfig==1.1.1
mccabe==0.7.0
nodeenv==1.6.0
packaging==21.3
platformdirs==2.4.0
pluggy==1.0.0
pre-commit==2.17.0
py==1.11.0
pycodestyle==2.9.1
pyflakes==2.5.0
pyparsing==3.1.4
pytest==7.0.1
-e git+https://github.com/asottile/pyupgrade.git@47f2b57f4b8ea963617199885ff46e0af7e63e45#egg=pyupgrade
PyYAML==6.0.1
toml==0.10.2
tomli==1.2.3
typing_extensions==4.1.1
virtualenv==20.16.2
zipp==3.6.0

name: pyupgrade
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - attrs==22.2.0
      - cfgv==3.3.1
      - coverage==6.2
      - distlib==0.3.9
      - filelock==3.4.1
      - flake8==5.0.4
      - identify==2.4.4
      - importlib-metadata==4.2.0
      - importlib-resources==5.2.3
      - iniconfig==1.1.1
      - mccabe==0.7.0
      - nodeenv==1.6.0
      - packaging==21.3
      - platformdirs==2.4.0
      - pluggy==1.0.0
      - pre-commit==2.17.0
      - py==1.11.0
      - pycodestyle==2.9.1
      - pyflakes==2.5.0
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pyyaml==6.0.1
      - toml==0.10.2
      - tomli==1.2.3
      - typing-extensions==4.1.1
      - virtualenv==20.16.2
      - zipp==3.6.0
prefix: /opt/conda/envs/pyupgrade

tests/pyupgrade_test.py::test_sets[set(\n tests/pyupgrade_test.py::test_dictcomps[dict(\n
tests/pyupgrade_test.py::test_roundtrip_text[] tests/pyupgrade_test.py::test_roundtrip_text[foo] tests/pyupgrade_test.py::test_roundtrip_text[{}] tests/pyupgrade_test.py::test_roundtrip_text[{0}] tests/pyupgrade_test.py::test_roundtrip_text[{named}] tests/pyupgrade_test.py::test_roundtrip_text[{!r}] tests/pyupgrade_test.py::test_roundtrip_text[{:>5}] tests/pyupgrade_test.py::test_roundtrip_text[{{] tests/pyupgrade_test.py::test_roundtrip_text[}}] tests/pyupgrade_test.py::test_roundtrip_text[{0!s:15}] tests/pyupgrade_test.py::test_intentionally_not_round_trip[{:}-{}] tests/pyupgrade_test.py::test_intentionally_not_round_trip[{0:}-{0}] tests/pyupgrade_test.py::test_intentionally_not_round_trip[{0!r:}-{0!r}] tests/pyupgrade_test.py::test_tokenize_src_simple tests/pyupgrade_test.py::test_roundtrip_tokenize[testing/resources/empty.py] tests/pyupgrade_test.py::test_roundtrip_tokenize[testing/resources/unicode_snowman.py] tests/pyupgrade_test.py::test_roundtrip_tokenize[testing/resources/backslash_continuation.py] tests/pyupgrade_test.py::test_sets[set()-set()] tests/pyupgrade_test.py::test_sets[set((\n))-set((\n))] tests/pyupgrade_test.py::test_sets[set tests/pyupgrade_test.py::test_sets[set(())-set()] tests/pyupgrade_test.py::test_sets[set([])-set()] tests/pyupgrade_test.py::test_sets[set(( tests/pyupgrade_test.py::test_sets[set((1, tests/pyupgrade_test.py::test_sets[set([1, tests/pyupgrade_test.py::test_sets[set(x tests/pyupgrade_test.py::test_sets[set([x tests/pyupgrade_test.py::test_sets[set((x tests/pyupgrade_test.py::test_sets[set(((1, tests/pyupgrade_test.py::test_sets[set((a, tests/pyupgrade_test.py::test_sets[set([(1, tests/pyupgrade_test.py::test_sets[set([((1, tests/pyupgrade_test.py::test_sets[set((((1, tests/pyupgrade_test.py::test_sets[set(\n(1, tests/pyupgrade_test.py::test_sets[set((\n1,\n2,\n))\n-{\n1,\n2,\n}\n] tests/pyupgrade_test.py::test_sets[set((frozenset(set((1, tests/pyupgrade_test.py::test_sets[set((1,))-{1}] tests/pyupgrade_test.py::test_dictcomps[x tests/pyupgrade_test.py::test_dictcomps[dict()-dict()] tests/pyupgrade_test.py::test_dictcomps[(-(] tests/pyupgrade_test.py::test_dictcomps[dict tests/pyupgrade_test.py::test_dictcomps[dict((a, tests/pyupgrade_test.py::test_dictcomps[dict([a, tests/pyupgrade_test.py::test_dictcomps[dict(((a, tests/pyupgrade_test.py::test_dictcomps[dict([(a, tests/pyupgrade_test.py::test_dictcomps[dict(((a), tests/pyupgrade_test.py::test_dictcomps[dict((k, tests/pyupgrade_test.py::test_format_literals['{}'.format(1)-'{}'.format(1)] tests/pyupgrade_test.py::test_format_literals['{'.format(1)-'{'.format(1)] tests/pyupgrade_test.py::test_format_literals['}'.format(1)-'}'.format(1)] tests/pyupgrade_test.py::test_format_literals[x tests/pyupgrade_test.py::test_format_literals['{0}'.format(1)-'{}'.format(1)] tests/pyupgrade_test.py::test_format_literals['''{0}\n{1}\n'''.format(1, tests/pyupgrade_test.py::test_format_literals['{0}' tests/pyupgrade_test.py::test_format_literals[print(\n tests/pyupgrade_test.py::test_imports_unicode_literals[import tests/pyupgrade_test.py::test_imports_unicode_literals[from tests/pyupgrade_test.py::test_imports_unicode_literals[x tests/pyupgrade_test.py::test_imports_unicode_literals["""docstring"""\nfrom tests/pyupgrade_test.py::test_unicode_literals[(-False-(] tests/pyupgrade_test.py::test_unicode_literals[u''-False-u''] tests/pyupgrade_test.py::test_unicode_literals[u''-True-''] tests/pyupgrade_test.py::test_unicode_literals[from tests/pyupgrade_test.py::test_unicode_literals["""with tests/pyupgrade_test.py::test_main_trivial tests/pyupgrade_test.py::test_main_noop tests/pyupgrade_test.py::test_main_changes_a_file tests/pyupgrade_test.py::test_main_syntax_error tests/pyupgrade_test.py::test_main_non_utf8_bytes tests/pyupgrade_test.py::test_py3_only_argument_unicode_literals
pyupgrade.py
pyupgrade.py