syn-canon 0.3.0

Canonical form of a syn file, equal across formatting drift and local binder names
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
//! Formatting drift rustfmt introduces, folded before hashing.

use alloc::boxed::Box;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use proc_macro2::{Group, TokenStream, TokenTree};
use quote::ToTokens;
use syn::visit_mut::VisitMut;

/// `group` with its stream mapped through `f`, delimiter and span kept.
pub(crate) fn map_group(group: &Group, f: impl FnOnce(TokenStream) -> TokenStream) -> TokenTree {
    let mut rebuilt = Group::new(group.delimiter(), f(group.stream()));
    rebuilt.set_span(group.span());
    TokenTree::Group(rebuilt)
}

/// `lit` in its canonical decimal spelling.
pub(crate) fn canonical_literal(lit: proc_macro2::Literal) -> TokenTree {
    let span = lit.span();
    let rebuilt = match syn::Lit::new(lit) {
        syn::Lit::Int(i) => {
            let decimal = format!("{}{}", i.base10_digits(), i.suffix());
            match syn::parse_str::<syn::LitInt>(&decimal) {
                Ok(mut read)
                    if read.base10_digits() == i.base10_digits() && read.suffix() == i.suffix() =>
                {
                    read.set_span(span);
                    syn::Lit::Int(read)
                }
                // The suffix reads as a radix prefix or an exponent after the digits, `0b0buu`.
                _ => syn::Lit::Int(i),
            }
        }
        syn::Lit::Float(f) => {
            let digits = canon_float_str(f.base10_digits());
            syn::Lit::Float(syn::LitFloat::new(&format!("{digits}{}", f.suffix()), span))
        }
        syn::Lit::Str(s) => syn::Lit::Str(syn::LitStr::new(&s.value(), span)),
        syn::Lit::ByteStr(b) => syn::Lit::ByteStr(syn::LitByteStr::new(&b.value(), span)),
        syn::Lit::CStr(c) => syn::Lit::CStr(syn::LitCStr::new(c.value().as_c_str(), span)),
        syn::Lit::Char(c) => syn::Lit::Char(syn::LitChar::new(c.value(), span)),
        syn::Lit::Byte(b) => syn::Lit::Byte(syn::LitByte::new(b.value(), span)),
        other => other,
    };
    rebuilt
        .to_token_stream()
        .into_iter()
        .next()
        .expect("rebuilt literal produces at least one token")
}

/// Canonical decimal string for a float's digit part, text only so the
/// canonical form never depends on the feature set. `syn` has already
/// dropped the underscores, a `+` exponent sign and an upper case `E`.
fn canon_float_str(s: &str) -> String {
    let (mantissa, exponent) = match s.split_once('e') {
        Some((mantissa, exponent)) => (mantissa, Some(exponent)),
        None => (s, None),
    };
    let (whole, fraction) = mantissa.split_once('.').unwrap_or((mantissa, ""));
    let fraction = fraction.trim_end_matches('0');
    let mut out = String::with_capacity(s.len());
    out.push_str(whole);
    out.push('.');
    out.push_str(if fraction.is_empty() { "0" } else { fraction });
    if let Some(exponent) = exponent {
        out.push('e');
        let (sign, digits) = exponent
            .strip_prefix('-')
            .map_or(("", exponent), |digits| ("-", digits));
        out.push_str(sign);
        let digits = digits.trim_start_matches('0');
        out.push_str(if digits.is_empty() { "0" } else { digits });
    }
    out
}

/// Fold arm braces, doc attributes, and `use` shapes in place. When `compiles`, also fold the
/// spellings only a compiling program makes equivalent. An `observed` file is the input of an
/// enclosing attribute macro and keeps its spelling. With `std_names`, the scopes around the
/// file leave every std derive, formatting macro and crate root bound to the std item.
pub(crate) fn normalize_file(
    file: &mut syn::File,
    compiles: bool,
    helpers: &mut crate::helpers::HelperSyntax,
    observed: bool,
    std_names: bool,
) {
    if compiles {
        if !observed {
            crate::const_items::inline_array_constants(file);
        }
        helpers.retain_file(file);
    }
    if observed {
        ObservedUses {
            helpers: Some(&mut *helpers),
        }
        .visit_file_mut(file);
        helpers.retain_file(file);
        return;
    }
    Drift {
        compiles,
        std_names,
        helpers: Some(&mut *helpers),
        ..Drift::default()
    }
    .visit_file_mut(file);
    helpers.retain_file(file);
}

/// The parse-only drift of one block, for an independent context normalization inside the
/// scopes `frames` record.
pub(crate) fn normalize_block(block: &mut syn::Block, frames: &[crate::scope::Frame]) {
    Drift::within(frames).visit_block_mut(block);
}

/// The parse-only drift of one match arm, for an independent context normalization inside the
/// scopes `frames` record.
pub(crate) fn normalize_arm(arm: &mut syn::Arm, frames: &[crate::scope::Frame]) {
    Drift::within(frames).visit_arm_mut(arm);
}

/// The parse-only drift of one expression, for an independent context normalization inside
/// the scopes `frames` record.
pub(crate) fn normalize_expr(expr: &mut syn::Expr, frames: &[crate::scope::Frame]) {
    Drift::within(frames).visit_expr_mut(expr);
}

/// The std names the scopes `frames` record bind to something else.
fn frames_rebound(frames: &[crate::scope::Frame]) -> u32 {
    use crate::scope::Ns;

    let imported = frames
        .iter()
        .fold(0, |rebound, frame| rebound | frame.std_rebound);
    STD_DERIVES
        .into_iter()
        .chain(FORMAT_MACROS.map(|(name, _, _)| name))
        .map(|name| (Ns::Macro, name))
        .chain(CRATE_ROOTS.map(|name| (Ns::Type, name)))
        .filter(|&(ns, name)| frames.iter().any(|frame| frame.lookup(ns, name).is_some()))
        .fold(imported, |rebound, (_, name)| rebound | std_name_bit(name))
}

/// The parse-only tail-return fold of a function body.
pub(crate) fn fold_fn_body(block: &mut syn::Block) {
    fold_tail_return(&mut block.stmts);
}

/// The drift folds of everything outside macro input. A node an attribute macro or a
/// procedural derive reads keeps its spelling, apart from the block `use` hoisting name
/// resolution relies on.
#[derive(Default)]
struct Drift<'a> {
    /// The code compiles, so an unneeded `mut` and the semicolon of a `()` block fold.
    compiles: bool,
    /// Inside an impl header's trait path or self type, where a path may not
    /// elide its lifetime (E0726).
    in_impl_header: bool,
    /// The scopes around the file leave every std name bound to the std item.
    std_names: bool,
    /// The std names the file rebinds wherever it sits.
    file_rebound: u32,
    /// The std name bindings of the scopes around the current node, innermost last.
    std_scopes: Vec<StdScope>,
    helpers: Option<&'a mut crate::helpers::HelperSyntax>,
}

impl VisitMut for Drift<'_> {
    #[expect(
        clippy::result_large_err,
        reason = "a non-use entry goes back to the list"
    )]
    fn visit_file_mut(&mut self, file: &mut syn::File) {
        if has_macro_attribute(&file.attrs) {
            self.observed().visit_file_mut(file);
            return;
        }
        self.file_rebound = file_rebound(file);
        let mut root = StdScope::of(file.items.iter(), true);
        if !self.std_names {
            root.rebound = StdScope::ALL;
        }
        hoist_uses(
            &mut file.items,
            |item| match item {
                syn::Item::Use(u) => Ok(u),
                other => Err(other),
            },
            syn::Item::Use,
        );
        self.std_scopes.push(root);
        syn::visit_mut::visit_file_mut(self, file);
        self.std_scopes.pop();
    }

    #[expect(
        clippy::result_large_err,
        reason = "a non-use entry goes back to the list"
    )]
    fn visit_block_mut(&mut self, block: &mut syn::Block) {
        let handles = self
            .helpers
            .as_mut()
            .map(|helpers| helpers.take_block(block));
        drop_empty_stmts(&mut block.stmts);
        hoist_uses(
            &mut block.stmts,
            |stmt| match stmt {
                syn::Stmt::Item(syn::Item::Use(u)) => Ok(u),
                other => Err(other),
            },
            |u| syn::Stmt::Item(syn::Item::Use(u)),
        );
        hoist_block_items(&mut block.stmts);
        semicolon_non_tail_macros(&mut block.stmts);
        if self.compiles {
            drop_block_statement_semicolons(&mut block.stmts);
        }
        if let Some(handles) = handles {
            self.helpers
                .as_mut()
                .expect("tracked helper syntax")
                .put_block(block, handles);
        }
        self.std_scopes.push(StdScope::of(
            block.stmts.iter().filter_map(|stmt| match stmt {
                syn::Stmt::Item(item) => Some(item),
                _ => None,
            }),
            false,
        ));
        syn::visit_mut::visit_block_mut(self, block);
        self.std_scopes.pop();
    }

    fn visit_item_mod_mut(&mut self, node: &mut syn::ItemMod) {
        let Some((_, items)) = &node.content else {
            return;
        };
        self.std_scopes.push(StdScope::of(items.iter(), true));
        syn::visit_mut::visit_item_mod_mut(self, node);
        self.std_scopes.pop();
    }

    fn visit_item_fn_mut(&mut self, node: &mut syn::ItemFn) {
        syn::visit_mut::visit_item_fn_mut(self, node);
        self.fold_fn_body(&node.sig, &mut node.block);
    }

    fn visit_impl_item_fn_mut(&mut self, node: &mut syn::ImplItemFn) {
        syn::visit_mut::visit_impl_item_fn_mut(self, node);
        self.fold_fn_body(&node.sig, &mut node.block);
    }

    fn visit_trait_item_fn_mut(&mut self, node: &mut syn::TraitItemFn) {
        syn::visit_mut::visit_trait_item_fn_mut(self, node);
        if let Some(block) = &mut node.default {
            self.fold_fn_body(&node.sig, block);
        }
    }

    fn visit_expr_async_mut(&mut self, node: &mut syn::ExprAsync) {
        syn::visit_mut::visit_expr_async_mut(self, node);
        fold_tail_return(&mut node.block.stmts);
    }

    fn visit_arm_mut(&mut self, arm: &mut syn::Arm) {
        if has_macro_attribute(&arm.attrs) {
            self.observed().visit_arm_mut(arm);
            return;
        }
        strip_inert_attrs(&mut arm.attrs);
        unwrap_arm_block(arm);
        // The printer writes the comma a non-block arm needs, a written one is drift.
        arm.comma = None;
        syn::visit_mut::visit_arm_mut(self, arm);
        if self.compiles {
            drop_binding_mut(&mut arm.pat);
        }
    }

    fn visit_local_mut(&mut self, local: &mut syn::Local) {
        if has_macro_attribute(&local.attrs) {
            self.observed().visit_local_mut(local);
            return;
        }
        syn::visit_mut::visit_local_mut(self, local);
        if self.compiles {
            drop_binding_mut(&mut local.pat);
        }
    }

    fn visit_expr_mut(&mut self, expr: &mut syn::Expr) {
        if has_macro_attribute(expr_attrs(expr)) {
            self.observed().visit_expr_mut(expr);
            return;
        }
        syn::visit_mut::visit_expr_mut(self, expr);
        fold_paren_expr(expr);
        if let syn::Expr::If(expr_if) = expr {
            collapse_else_if(expr_if);
            if self.compiles {
                fold_empty_else(expr_if);
            }
        }
        if let syn::Expr::Closure(closure) = expr {
            let protected_inputs = closure
                .inputs
                .iter_mut()
                .any(|pat| pat_attrs(pat).is_some_and(|attrs| has_macro_attribute(attrs)));
            if !protected_inputs {
                if closure.inputs.trailing_punct() {
                    closure.inputs.pop_punct();
                }
                if matches!(
                    closure.body.as_ref(),
                    syn::Expr::Block(block)
                        if block.attrs.is_empty()
                            && block.label.is_none()
                            && block.block.stmts.is_empty()
                ) {
                    fold_unit_return(&mut closure.output);
                }
            }
            if let syn::Expr::Block(block) = closure.body.as_mut() {
                fold_tail_return(&mut block.block.stmts);
            }
            unwrap_single_expr_block(&mut closure.body);
        }
        if self.compiles {
            if let syn::Expr::Return(ret) = expr
                && ret.expr.as_ref().is_some_and(
                    |value| matches!(value.as_ref(), syn::Expr::Tuple(tuple) if tuple.elems.is_empty() && tuple.attrs.is_empty()),
                )
            {
                ret.expr = None;
            }
            match expr {
                syn::Expr::Closure(closure) => closure.inputs.iter_mut().for_each(drop_binding_mut),
                syn::Expr::ForLoop(for_loop) => drop_binding_mut(&mut for_loop.pat),
                syn::Expr::Let(expr_let) => drop_binding_mut(&mut expr_let.pat),
                _ => {}
            }
            if let Some(block) = unit_block(expr) {
                drop_tail_semicolon(&mut block.stmts);
            }
        }
        fold_signed_zero(expr);
        if self.compiles {
            fold_scalar_const(expr);
        }
    }

    fn visit_pat_mut(&mut self, pat: &mut syn::Pat) {
        if let Some(attrs) = pat_attrs(pat) {
            if has_macro_attribute(attrs) {
                self.observed().visit_pat_mut(pat);
                return;
            }
            strip_inert_attrs(attrs);
        }
        syn::visit_mut::visit_pat_mut(self, pat);
        fold_paren_pat(pat);
        if self.compiles {
            fold_pattern_spelling(pat);
        }
    }

    fn visit_fn_arg_mut(&mut self, node: &mut syn::FnArg) {
        let attrs = match node {
            syn::FnArg::Receiver(receiver) => &mut receiver.attrs,
            syn::FnArg::Typed(typed) => &mut typed.attrs,
        };
        if has_macro_attribute(attrs) {
            self.observed().visit_fn_arg_mut(node);
            return;
        }
        strip_inert_attrs(attrs);
        syn::visit_mut::visit_fn_arg_mut(self, node);
        if self.compiles {
            match node {
                syn::FnArg::Receiver(receiver) => receiver.mutability = None,
                syn::FnArg::Typed(typed) => drop_binding_mut(&mut typed.pat),
            }
        }
        if let syn::FnArg::Receiver(receiver) = node {
            fold_explicit_receiver(receiver);
        }
    }

    fn visit_named_arg_mut(&mut self, node: &mut syn::NamedArg) {
        if has_macro_attribute(&node.attrs) {
            self.observed().visit_named_arg_mut(node);
            return;
        }
        strip_inert_attrs(&mut node.attrs);
        syn::visit_mut::visit_named_arg_mut(self, node);
    }

    fn visit_type_mut(&mut self, ty: &mut syn::Type) {
        if has_macro_attribute(type_attrs(ty)) {
            self.observed().visit_type_mut(ty);
            return;
        }
        syn::visit_mut::visit_type_mut(self, ty);
        fold_paren_type(ty);
    }

    fn visit_type_path_mut(&mut self, node: &mut syn::TypePath) {
        syn::visit_mut::visit_type_path_mut(self, node);
        fold_type_path(&mut node.path);
    }

    fn visit_type_fn_ptr_mut(&mut self, node: &mut syn::TypeFnPtr) {
        syn::visit_mut::visit_type_fn_ptr_mut(self, node);
        if self.compiles {
            fold_fn_pointer_lifetime(node);
        }
        fold_unit_return(&mut node.output);
    }

    fn visit_abi_mut(&mut self, node: &mut syn::Abi) {
        if self.compiles && node.name.is_none() {
            node.name = Some(syn::LitStr::new("C", node.extern_token.span));
        }
    }

    fn visit_vis_restricted_mut(&mut self, node: &mut syn::VisRestricted) {
        syn::visit_mut::visit_vis_restricted_mut(self, node);
        if ["crate", "self", "super"]
            .iter()
            .any(|name| node.path.is_ident(name))
        {
            node.in_token = None;
        }
    }

    fn visit_parenthesized_generic_arguments_mut(
        &mut self,
        node: &mut syn::ParenthesizedGenericArguments,
    ) {
        syn::visit_mut::visit_parenthesized_generic_arguments_mut(self, node);
        fold_unit_return(&mut node.output);
    }

    fn visit_expr_method_call_mut(&mut self, node: &mut syn::ExprMethodCall) {
        syn::visit_mut::visit_expr_method_call_mut(self, node);
        if node
            .turbofish
            .as_ref()
            .is_some_and(|args| args.args.is_empty())
        {
            node.turbofish = None;
        }
    }

    // `syn::Attribute` payloads are opaque macro input.
    fn visit_attribute_mut(&mut self, _node: &mut syn::Attribute) {}

    fn visit_angle_bracketed_generic_arguments_mut(
        &mut self,
        node: &mut syn::AngleBracketedGenericArguments,
    ) {
        syn::visit_mut::visit_angle_bracketed_generic_arguments_mut(self, node);
        node.args.pop_punct();
        if self.compiles {
            for arg in &mut node.args {
                if let syn::GenericArgument::Const(expr) = arg {
                    fold_scalar_argument_block(expr);
                }
            }
        }
    }

    fn visit_generics_mut(&mut self, generics: &mut syn::Generics) {
        let observed = generics.params.iter().any(|param| {
            has_macro_attribute(match param {
                syn::GenericParam::Lifetime(param) => &param.attrs,
                syn::GenericParam::Type(param) => &param.attrs,
                syn::GenericParam::Const(param) => &param.attrs,
            })
        });
        if observed {
            self.observed().visit_generics_mut(generics);
            return;
        }
        syn::visit_mut::visit_generics_mut(self, generics);
        bounds_to_where(generics);
        generics.params.pop_punct();
        if let Some(clause) = &mut generics.where_clause {
            for predicate in &mut clause.predicates {
                match predicate {
                    syn::WherePredicate::Type(predicate) => {
                        drop_trailing_plus(&mut predicate.bounds);
                    }
                    syn::WherePredicate::Lifetime(predicate) => {
                        drop_trailing_plus(&mut predicate.bounds);
                    }
                    _ => {}
                }
            }
        }
    }

    fn visit_item_trait_mut(&mut self, node: &mut syn::ItemTrait) {
        syn::visit_mut::visit_item_trait_mut(self, node);
        drop_trailing_plus(&mut node.supertraits);
    }

    fn visit_item_trait_alias_mut(&mut self, node: &mut syn::ItemTraitAlias) {
        syn::visit_mut::visit_item_trait_alias_mut(self, node);
        drop_trailing_plus(&mut node.bounds);
    }

    fn visit_trait_bound_mut(&mut self, node: &mut syn::TraitBound) {
        syn::visit_mut::visit_trait_bound_mut(self, node);
        fold_type_path(&mut node.path);
        if node.paren_token.is_some() && !fn_sugar_output(&node.path) {
            node.paren_token = None;
        }
    }

    fn visit_trait_item_type_mut(&mut self, node: &mut syn::TraitItemType) {
        syn::visit_mut::visit_trait_item_type_mut(self, node);
        drop_trailing_plus(&mut node.bounds);
    }

    fn visit_type_impl_trait_mut(&mut self, node: &mut syn::TypeImplTrait) {
        syn::visit_mut::visit_type_impl_trait_mut(self, node);
        drop_trailing_plus(&mut node.bounds);
    }

    fn visit_type_trait_object_mut(&mut self, node: &mut syn::TypeTraitObject) {
        syn::visit_mut::visit_type_trait_object_mut(self, node);
        drop_trailing_plus(&mut node.bounds);
    }

    fn visit_constraint_mut(&mut self, node: &mut syn::Constraint) {
        syn::visit_mut::visit_constraint_mut(self, node);
        drop_trailing_plus(&mut node.bounds);
    }

    fn visit_type_reference_mut(&mut self, node: &mut syn::TypeReference) {
        syn::visit_mut::visit_type_reference_mut(self, node);
        if node.lifetime.as_ref().is_some_and(|l| l.ident == "_") {
            node.lifetime = None;
        }
    }

    fn visit_path_arguments_mut(&mut self, node: &mut syn::PathArguments) {
        syn::visit_mut::visit_path_arguments_mut(self, node);
        if !self.in_impl_header {
            drop_anonymous_lifetimes(node);
        }
        if matches!(node, syn::PathArguments::AngleBracketed(args) if args.args.is_empty()) {
            *node = syn::PathArguments::None;
        }
    }

    fn visit_item_impl_mut(&mut self, node: &mut syn::ItemImpl) {
        for attr in &mut node.attrs {
            self.visit_attribute_mut(attr);
        }
        self.visit_generics_mut(&mut node.generics);
        let outer = core::mem::replace(&mut self.in_impl_header, true);
        if let Some((path, _)) = &mut node.trait_ {
            self.visit_path_mut(path);
            fold_type_path(path);
        }
        self.visit_type_mut(&mut node.self_ty);
        self.in_impl_header = outer;
        for item in &mut node.items {
            self.visit_impl_item_mut(item);
        }
    }

    fn visit_item_const_mut(&mut self, node: &mut syn::ItemConst) {
        syn::visit_mut::visit_item_const_mut(self, node);
        StaticRefs.visit_type_mut(&mut node.ty);
    }

    fn visit_item_static_mut(&mut self, node: &mut syn::ItemStatic) {
        syn::visit_mut::visit_item_static_mut(self, node);
        StaticRefs.visit_type_mut(&mut node.ty);
    }

    fn visit_signature_mut(&mut self, sig: &mut syn::Signature) {
        syn::visit_mut::visit_signature_mut(self, sig);
        fold_unit_return(&mut sig.output);
    }

    fn visit_stmt_mut(&mut self, stmt: &mut syn::Stmt) {
        match stmt {
            syn::Stmt::Macro(v) => strip_inert_attrs(&mut v.attrs),
            syn::Stmt::Local(v) => strip_inert_attrs(&mut v.attrs),
            _ => {}
        }
        syn::visit_mut::visit_stmt_mut(self, stmt);
    }

    fn visit_item_mut(&mut self, item: &mut syn::Item) {
        if item_attrs(item).is_some_and(|attrs| has_macro_attribute(attrs)) {
            self.observed().visit_item_mut(item);
            return;
        }
        if let syn::Item::Struct(syn::ItemStruct { attrs, .. })
        | syn::Item::Enum(syn::ItemEnum { attrs, .. })
        | syn::Item::Union(syn::ItemUnion { attrs, .. }) = item
        {
            self.mark_rebound_derives(attrs);
            if has_macro_derive(attrs) {
                self.observed().visit_item_mut(item);
                return;
            }
            fold_std_derives(attrs);
        }
        if let Some(attrs) = item_attrs(item) {
            strip_inert_attrs(attrs);
        }
        syn::visit_mut::visit_item_mut(self, item);
    }

    fn visit_impl_item_mut(&mut self, item: &mut syn::ImplItem) {
        let attrs = match item {
            syn::ImplItem::Const(v) => &mut v.attrs,
            syn::ImplItem::Fn(v) => &mut v.attrs,
            syn::ImplItem::Type(v) => &mut v.attrs,
            syn::ImplItem::Macro(v) => &mut v.attrs,
            _ => return,
        };
        if has_macro_attribute(attrs) {
            self.observed().visit_impl_item_mut(item);
            return;
        }
        strip_inert_attrs(attrs);
        syn::visit_mut::visit_impl_item_mut(self, item);
    }

    fn visit_trait_item_mut(&mut self, item: &mut syn::TraitItem) {
        let attrs = match item {
            syn::TraitItem::Const(v) => &mut v.attrs,
            syn::TraitItem::Fn(v) => &mut v.attrs,
            syn::TraitItem::Type(v) => &mut v.attrs,
            syn::TraitItem::Macro(v) => &mut v.attrs,
            _ => return,
        };
        if has_macro_attribute(attrs) {
            self.observed().visit_trait_item_mut(item);
            return;
        }
        strip_inert_attrs(attrs);
        syn::visit_mut::visit_trait_item_mut(self, item);
    }

    fn visit_foreign_item_mut(&mut self, item: &mut syn::ForeignItem) {
        let attrs = match item {
            syn::ForeignItem::Fn(v) => &v.attrs,
            syn::ForeignItem::Static(v) => &v.attrs,
            syn::ForeignItem::Type(v) => &v.attrs,
            syn::ForeignItem::Macro(v) => &v.attrs,
            _ => return,
        };
        if has_macro_attribute(attrs) {
            self.observed().visit_foreign_item_mut(item);
            return;
        }
        syn::visit_mut::visit_foreign_item_mut(self, item);
    }

    fn visit_field_mut(&mut self, field: &mut syn::Field) {
        if has_macro_attribute(&field.attrs) {
            self.observed().visit_field_mut(field);
            return;
        }
        strip_inert_attrs(&mut field.attrs);
        syn::visit_mut::visit_field_mut(self, field);
    }

    fn visit_variant_mut(&mut self, variant: &mut syn::Variant) {
        if has_macro_attribute(&variant.attrs) {
            self.observed().visit_variant_mut(variant);
            return;
        }
        strip_inert_attrs(&mut variant.attrs);
        syn::visit_mut::visit_variant_mut(self, variant);
    }

    fn visit_expr_macro_mut(&mut self, node: &mut syn::ExprMacro) {
        normalize_macro_delim(&mut node.mac);
        inline_format_args(&mut node.mac, self.rebound_names());
        syn::visit_mut::visit_expr_macro_mut(self, node);
    }

    fn visit_stmt_macro_mut(&mut self, node: &mut syn::StmtMacro) {
        normalize_macro_delim(&mut node.mac);
        inline_format_args(&mut node.mac, self.rebound_names());
        syn::visit_mut::visit_stmt_macro_mut(self, node);
    }

    fn visit_type_macro_mut(&mut self, node: &mut syn::TypeMacro) {
        normalize_macro_delim(&mut node.mac);
        syn::visit_mut::visit_type_macro_mut(self, node);
    }

    fn visit_item_macro_mut(&mut self, node: &mut syn::ItemMacro) {
        if node.ident.is_none() {
            normalize_macro_delim(&mut node.mac);
            node.semi_token.get_or_insert_with(Default::default);
        }
        syn::visit_mut::visit_item_macro_mut(self, node);
    }

    fn visit_impl_item_macro_mut(&mut self, node: &mut syn::ImplItemMacro) {
        normalize_macro_delim(&mut node.mac);
        node.semi_token.get_or_insert_with(Default::default);
        syn::visit_mut::visit_impl_item_macro_mut(self, node);
    }

    fn visit_trait_item_macro_mut(&mut self, node: &mut syn::TraitItemMacro) {
        normalize_macro_delim(&mut node.mac);
        node.semi_token.get_or_insert_with(Default::default);
        syn::visit_mut::visit_trait_item_macro_mut(self, node);
    }
}

/// Hoists the `use` items of every block in a node a macro reads, the one rewrite name
/// resolution needs there, and leaves the rest as written.
struct ObservedUses<'a> {
    helpers: Option<&'a mut crate::helpers::HelperSyntax>,
}

impl VisitMut for ObservedUses<'_> {
    #[expect(
        clippy::result_large_err,
        reason = "a non-use entry goes back to the list"
    )]
    fn visit_block_mut(&mut self, block: &mut syn::Block) {
        let handles = self
            .helpers
            .as_mut()
            .map(|helpers| helpers.take_block(block));
        hoist_uses(
            &mut block.stmts,
            |stmt| match stmt {
                syn::Stmt::Item(syn::Item::Use(u)) => Ok(u),
                other => Err(other),
            },
            |u| syn::Stmt::Item(syn::Item::Use(u)),
        );
        if let Some(handles) = handles {
            self.helpers
                .as_mut()
                .expect("tracked helper syntax")
                .put_block(block, handles);
        }
        syn::visit_mut::visit_block_mut(self, block);
    }

    fn visit_attribute_mut(&mut self, _node: &mut syn::Attribute) {}
}

impl Drift<'_> {
    /// The parse-only drift of a context inside the scopes `frames` record.
    fn within(frames: &[crate::scope::Frame]) -> Self {
        Drift {
            std_scopes: Vec::from([StdScope {
                module: true,
                rebound: frames_rebound(frames),
                supers: 0,
            }]),
            ..Drift::default()
        }
    }

    /// The visitor of a node a macro reads.
    fn observed(&mut self) -> ObservedUses<'_> {
        ObservedUses {
            helpers: self.helpers.as_deref_mut(),
        }
    }

    /// Spell each std derive name an enclosing binding may give another macro as a raw
    /// identifier, which names the same binding and which no pass reads as the std derive.
    fn mark_rebound_derives(&self, attrs: &mut [syn::Attribute]) {
        use syn::parse::Parser;

        let rebound = self.rebound_names();
        if rebound == 0 {
            return;
        }
        let parser = syn::punctuated::Punctuated::<syn::Path, syn::Token![,]>::parse_terminated;
        let lists = attrs.iter_mut().filter_map(|attr| match &mut attr.meta {
            syn::Meta::List(list) if list.path.is_ident("derive") => {
                let paths = parser.parse2(list.tokens.clone()).ok()?;
                Some((list, paths))
            }
            _ => None,
        });
        for (list, mut paths) in lists {
            for path in &mut paths {
                if path.leading_colon.is_none()
                    && path.segments.len() == 1
                    && let Some(index) = STD_DERIVES
                        .iter()
                        .position(|std| path.segments[0].ident == std)
                    && rebound & (1 << index) != 0
                {
                    let ident = &mut path.segments[0].ident;
                    *ident = syn::Ident::new_raw(STD_DERIVES[index], ident.span());
                }
            }
            list.tokens = paths.into_token_stream();
        }
    }

    /// The std names the scopes from the innermost module scope inward may bind to something
    /// else, with every module a `use super::*` reaches, all of them when one reaches past the
    /// outermost scope.
    fn rebound_names(&self) -> u32 {
        let mut rebound = self.file_rebound;
        // The module levels past the innermost one a `use super::*` brings into view.
        let mut levels = 0;
        for scope in self.std_scopes.iter().rev() {
            rebound |= scope.rebound;
            levels = levels.max(scope.supers);
            if scope.module {
                if levels == 0 {
                    return rebound;
                }
                levels -= 1;
            }
        }
        StdScope::ALL
    }

    /// Fold a function body's tail `return`, and its tail semicolon when the
    /// signature makes the value `()`, before and after the return fold since
    /// each can expose the other.
    fn fold_fn_body(&self, sig: &syn::Signature, block: &mut syn::Block) {
        let unit = self.compiles && matches!(sig.output, syn::ReturnType::Default);
        if unit {
            drop_tail_semicolon(&mut block.stmts);
        }
        fold_tail_return(&mut block.stmts);
        if unit {
            drop_tail_semicolon(&mut block.stmts);
        }
    }
}

/// The block of a loop, or the then block of an `if` chain without a final
/// `else`, whose value rustc requires to be `()`.
fn unit_block(expr: &mut syn::Expr) -> Option<&mut syn::Block> {
    match expr {
        syn::Expr::ForLoop(e) => Some(&mut e.body),
        syn::Expr::While(e) => Some(&mut e.body),
        syn::Expr::Loop(e) => Some(&mut e.body),
        syn::Expr::If(e) if discards_value(e) => Some(&mut e.then_branch),
        _ => None,
    }
}

/// Whether the `if` chain from `expr_if` ends without an `else` block.
fn discards_value(mut expr_if: &syn::ExprIf) -> bool {
    loop {
        match expr_if.else_branch.as_ref().map(|(_, e)| e.as_ref()) {
            None => return true,
            Some(syn::Expr::If(inner)) => expr_if = inner,
            Some(_) => return false,
        }
    }
}

/// Drop the `mut` of a pattern that is one binding, by-value whatever the
/// scrutinee. Nested under a reference, `mut` resets a borrowing binding mode
/// before edition 2024, so a nested one stays.
fn drop_binding_mut(pat: &mut syn::Pat) {
    let pat = match pat {
        syn::Pat::Type(typed) => typed.pat.as_mut(),
        syn::Pat::Guard(guarded) => guarded.pat.as_mut(),
        other => other,
    };
    if let syn::Pat::Ident(ident) = pat
        && ident.by_ref.is_none()
    {
        ident.mutability = None;
    }
}

/// Normalize an explicit `Self` receiver without changing its binding or borrow kind.
fn fold_explicit_receiver(receiver: &mut syn::Receiver) {
    if matches!(
        &receiver.kind,
        syn::ReceiverKind::Typed(_, ty)
            if matches!(ty.as_ref(), syn::Type::Path(path)
                if path.attrs.is_empty() && path.qself.is_none() && path.path.is_ident("Self"))
    ) {
        receiver.kind = syn::ReceiverKind::Value;
        return;
    }
    if receiver.mutability.is_some() {
        return;
    }
    let syn::ReceiverKind::Typed(_colon, ty) = &mut receiver.kind else {
        return;
    };
    let syn::Type::Reference(reference) = &mut **ty else {
        return;
    };
    if reference.mutability.is_some() {
        return;
    }
    let syn::Type::Path(path) = reference.elem.as_ref() else {
        return;
    };
    if path.attrs.is_empty() && path.qself.is_none() && path.path.is_ident("Self") {
        let (ampersand, lifetime) = (
            reference.and_token,
            core::mem::take(&mut reference.lifetime),
        );
        receiver.kind = syn::ReceiverKind::Reference(ampersand, lifetime, None);
    }
}

fn fold_pattern_spelling(pat: &mut syn::Pat) {
    match pat {
        syn::Pat::Or(pat) => pat.leading_vert = None,
        syn::Pat::Ident(pat)
            if pat.subpat.as_ref().is_some_and(
                |(_, sub)| matches!(sub.as_ref(), syn::Pat::Wild(wild) if wild.attrs.is_empty()),
            ) =>
        {
            let name = pat.ident.to_string();
            let name = name.strip_prefix("r#").unwrap_or(&name);
            if !name.starts_with(char::is_uppercase) {
                pat.subpat = None;
            }
        }
        _ => {}
    }
}

/// Drop the semicolon of a tail statement whose value is `()` either way, the
/// rewrite clippy's `semicolon_if_nothing_returned` makes in reverse.
fn drop_tail_semicolon(stmts: &mut [syn::Stmt]) {
    match stmts.last_mut() {
        Some(syn::Stmt::Expr(_, semi)) => *semi = None,
        Some(syn::Stmt::Macro(mac)) => mac.semi_token = None,
        _ => {}
    }
}

/// Drop the semicolon after every non-tail statement ending in a block, an
/// empty statement rustc's `redundant_semicolons` reports.
fn drop_block_statement_semicolons(stmts: &mut [syn::Stmt]) {
    let Some((_, rest)) = stmts.split_last_mut() else {
        return;
    };
    for stmt in rest {
        if let syn::Stmt::Expr(expr, semi) = stmt
            && matches!(
                expr,
                syn::Expr::If(_)
                    | syn::Expr::Match(_)
                    | syn::Expr::Block(_)
                    | syn::Expr::Unsafe(_)
                    | syn::Expr::While(_)
                    | syn::Expr::Loop(_)
                    | syn::Expr::ForLoop(_)
                    | syn::Expr::TryBlock(_)
                    | syn::Expr::Const(_)
            )
        {
            *semi = None;
        }
    }
}

/// An arm body `{ expr }` becomes `expr`, the form rustfmt writes.
fn unwrap_arm_block(arm: &mut syn::Arm) {
    unwrap_single_expr_block(&mut arm.body);
}

/// A closure or arm body `{ expr }` becomes `expr` when the block holds
/// exactly one tail expression and carries no label or attributes.
fn unwrap_single_expr_block(body: &mut Box<syn::Expr>) {
    let syn::Expr::Block(block) = body.as_mut() else {
        return;
    };
    if block.label.is_some()
        || !block.attrs.is_empty()
        || !matches!(block.block.stmts.as_slice(), [syn::Stmt::Expr(_, None)])
    {
        return;
    }
    if let Some(syn::Stmt::Expr(expr, None)) = block.block.stmts.pop() {
        **body = expr;
    }
}

/// An `else { if … }` whose block holds only that `if`, with no attribute
/// on it, becomes `else if …`. An else block itself never carries a label
/// or attributes.
fn collapse_else_if(expr_if: &mut syn::ExprIf) {
    let Some((_, otherwise)) = &mut expr_if.else_branch else {
        return;
    };
    let syn::Expr::Block(block) = otherwise.as_mut() else {
        return;
    };
    if !matches!(block.block.stmts.as_slice(), [syn::Stmt::Expr(syn::Expr::If(inner), None)] if inner.attrs.is_empty())
    {
        return;
    }
    if let Some(syn::Stmt::Expr(inner, None)) = block.block.stmts.pop() {
        **otherwise = inner;
    }
}

/// Remove every bare empty statement from `stmts`.
fn drop_empty_stmts(stmts: &mut Vec<syn::Stmt>) {
    stmts.retain(|stmt| {
        !matches!(
            stmt,
            syn::Stmt::Expr(syn::Expr::Verbatim(v), Some(_)) if v.is_empty()
        )
    });
}

/// Replace a tail `return expr`, with or without the semicolon, with
/// `expr`, then propagate through every tail position forwarding its value,
/// an `if` only with an `else` clause because rustc rejects a valued return
/// in a discarded then position (`E0317`). A tail unit `return` adds
/// nothing and goes. A return keeping a live attribute, `#[cfg]` for
/// instance, must not fold, the fold would drop the condition.
fn fold_tail_return(stmts: &mut Vec<syn::Stmt>) {
    let Some(last) = stmts.last_mut() else {
        return;
    };
    match last {
        syn::Stmt::Expr(syn::Expr::Return(ret), _) => {
            strip_inert_attrs(&mut ret.attrs);
            if !ret.attrs.is_empty() {
                return;
            }
            if let Some(mut inner) = ret.expr.take() {
                fold_tail_expr(&mut inner);
                *last = syn::Stmt::Expr(*inner, None);
            } else {
                stmts.pop();
                fold_tail_return(stmts);
            }
        }
        syn::Stmt::Expr(expr, None) => fold_tail_expr(expr),
        _ => {}
    }
}

/// Continue the fold through an expression whose tail value is the enclosing
/// tail's value.
fn fold_tail_expr(expr: &mut syn::Expr) {
    match expr {
        syn::Expr::Block(block) => fold_tail_return(&mut block.block.stmts),
        syn::Expr::Unsafe(block) => fold_tail_return(&mut block.block.stmts),
        syn::Expr::If(expr_if) if expr_if.else_branch.is_some() => {
            fold_tail_return(&mut expr_if.then_branch.stmts);
            if let Some((_, otherwise)) = &mut expr_if.else_branch {
                fold_tail_expr(otherwise);
            }
        }
        syn::Expr::Match(expr_match) => {
            for arm in &mut expr_match.arms {
                fold_tail_expr(&mut arm.body);
                unwrap_single_expr_block(&mut arm.body);
            }
        }
        // An arm body `return value`, written bare or as a block the arm fold unwrapped.
        syn::Expr::Return(ret) => {
            strip_inert_attrs(&mut ret.attrs);
            if ret.attrs.is_empty()
                && let Some(inner) = ret.expr.take()
            {
                *expr = *inner;
                fold_tail_expr(expr);
            }
        }
        _ => {}
    }
}

/// Give every statement macro but the last one a semicolon, which the
/// brace form omits. A tail macro's value is the block's value.
fn semicolon_non_tail_macros(stmts: &mut [syn::Stmt]) {
    let Some((_, rest)) = stmts.split_last_mut() else {
        return;
    };
    for stmt in rest {
        if let syn::Stmt::Macro(mac) = stmt {
            mac.semi_token.get_or_insert_with(Default::default);
        }
    }
}

/// Drop an explicit `-> ()`, which a signature means by omitting it.
fn fold_unit_return(output: &mut syn::ReturnType) {
    if let syn::ReturnType::Type(_, ty) = output
        && matches!(ty.as_ref(), syn::Type::Tuple(t) if t.attrs.is_empty() && t.elems.is_empty())
    {
        *output = syn::ReturnType::Default;
    }
}

fn fold_fn_pointer_lifetime(node: &mut syn::TypeFnPtr) {
    use syn::visit::Visit;

    let Some(bound) = &node.lifetimes else {
        return;
    };
    let Some(syn::GenericParam::Lifetime(binder)) = bound.lifetimes.first() else {
        return;
    };
    if bound.lifetimes.len() != 1
        || node.inputs.len() != 1
        || node.variadic.is_some()
        || !binder.attrs.is_empty()
        || !binder.bounds.is_empty()
        || !node.inputs[0].attrs.is_empty()
    {
        return;
    }
    let spelling = binder.lifetime.ident.to_string();
    if spelling.starts_with("r#") || matches!(spelling.as_str(), "static" | "_") {
        return;
    }
    let syn::Type::Reference(reference) = &mut node.inputs[0].ty else {
        return;
    };
    if reference.lifetime.as_ref() != Some(&binder.lifetime) {
        return;
    }
    let mut uses = LifetimeUses {
        name: &binder.lifetime.ident,
        count: 0,
        opaque: false,
    };
    uses.visit_type_reference(reference);
    uses.visit_return_type(&node.output);
    if uses.opaque || uses.count != 1 {
        return;
    }
    reference.lifetime = None;
    node.lifetimes = None;
}

struct LifetimeUses<'a> {
    name: &'a syn::Ident,
    count: usize,
    opaque: bool,
}

impl<'ast> syn::visit::Visit<'ast> for LifetimeUses<'_> {
    fn visit_lifetime(&mut self, lifetime: &'ast syn::Lifetime) {
        self.count += usize::from(lifetime.ident == *self.name);
    }

    fn visit_attribute(&mut self, _: &'ast syn::Attribute) {
        self.opaque = true;
    }

    fn visit_bound_lifetimes(&mut self, _: &'ast syn::BoundLifetimes) {
        self.opaque = true;
    }

    fn visit_type(&mut self, ty: &'ast syn::Type) {
        if matches!(
            ty,
            syn::Type::FnPtr(_) | syn::Type::Group(_) | syn::Type::Verbatim(_)
        ) {
            self.opaque = true;
        }
        syn::visit::visit_type(self, ty);
    }

    fn visit_macro(&mut self, _: &'ast syn::Macro) {
        self.opaque = true;
    }

    fn visit_expr(&mut self, expr: &'ast syn::Expr) {
        if matches!(expr, syn::Expr::Group(_) | syn::Expr::Verbatim(_)) {
            self.opaque = true;
        }
        syn::visit::visit_expr(self, expr);
    }

    fn visit_type_param_bound(&mut self, bound: &'ast syn::TypeParamBound) {
        if matches!(bound, syn::TypeParamBound::Verbatim(_)) {
            self.opaque = true;
        }
        syn::visit::visit_type_param_bound(self, bound);
    }

    fn visit_token_stream(&mut self, _: &'ast TokenStream) {
        self.opaque = true;
    }

    fn visit_lit(&mut self, literal: &'ast syn::Lit) {
        if matches!(literal, syn::Lit::Verbatim(_)) {
            self.opaque = true;
        }
        syn::visit::visit_lit(self, literal);
    }
}

fn fold_empty_else(expr_if: &mut syn::ExprIf) {
    if expr_if
        .else_branch
        .as_ref()
        .is_some_and(|(_, alternative)| {
            matches!(
                alternative.as_ref(),
                syn::Expr::Block(block)
                    if block.attrs.is_empty()
                        && block.label.is_none()
                        && block.block.stmts.is_empty()
            )
        })
    {
        expr_if.else_branch = None;
    }
}

fn fold_scalar_const(expr: &mut syn::Expr) {
    let syn::Expr::Const(block) = expr else {
        return;
    };
    if block.attrs.is_empty()
        && scalar_tail(&block.block.stmts)
        && let Some(syn::Stmt::Expr(value, None)) = block.block.stmts.pop()
    {
        *expr = value;
    }
}

pub(crate) fn fold_scalar_argument_block(expr: &mut syn::Expr) {
    let syn::Expr::Block(block) = expr else {
        return;
    };
    if block.attrs.is_empty()
        && block.label.is_none()
        && scalar_tail(&block.block.stmts)
        && let Some(syn::Stmt::Expr(value, None)) = block.block.stmts.pop()
    {
        *expr = value;
    }
}

fn scalar_tail(stmts: &[syn::Stmt]) -> bool {
    matches!(
        stmts,
        [syn::Stmt::Expr(syn::Expr::Lit(literal), None)]
            if literal.attrs.is_empty()
                && matches!(
                    literal.lit,
                    syn::Lit::Int(_)
                        | syn::Lit::Float(_)
                        | syn::Lit::Bool(_)
                        | syn::Lit::Char(_)
                        | syn::Lit::Byte(_)
                )
    )
}

fn fold_signed_zero(expr: &mut syn::Expr) {
    let syn::Expr::Unary(unary) = expr else {
        return;
    };
    let syn::Expr::Lit(literal) = unary.expr.as_ref() else {
        return;
    };
    if unary.attrs.is_empty()
        && literal.attrs.is_empty()
        && matches!(unary.op, syn::UnOp::Neg(_))
        && matches!(
            &literal.lit,
            syn::Lit::Int(integer)
                if integer.base10_digits().bytes().all(|digit| digit == b'0')
                    && ["i8", "i16", "i32", "i64", "i128", "isize"]
                        .contains(&integer.suffix())
        )
    {
        *expr = core::mem::replace(unary.expr.as_mut(), syn::Expr::Verbatim(TokenStream::new()));
    }
}

fn fold_type_path(path: &mut syn::Path) {
    for segment in &mut path.segments {
        if let syn::PathArguments::AngleBracketed(args) = &mut segment.arguments {
            args.colon2_token = None;
        }
    }
}

/// Remove a no-attribute `Expr::Paren` wrapper, leaving the inner expression.
fn fold_paren_expr(expr: &mut syn::Expr) {
    if !matches!(expr, syn::Expr::Paren(p) if p.attrs.is_empty()) {
        return;
    }
    let dummy = syn::Expr::Verbatim(proc_macro2::TokenStream::new());
    if let syn::Expr::Paren(paren) = core::mem::replace(expr, dummy) {
        *expr = *paren.expr;
    }
}

/// Remove a no-attribute `Pat::Paren` wrapper, leaving the inner pattern.
fn fold_paren_pat(pat: &mut syn::Pat) {
    if !matches!(pat, syn::Pat::Paren(p) if p.attrs.is_empty()) {
        return;
    }
    let dummy = syn::Pat::Verbatim(proc_macro2::TokenStream::new());
    if let syn::Pat::Paren(paren) = core::mem::replace(pat, dummy) {
        *pat = *paren.pat;
    }
}

/// Remove a `Type::Paren` wrapper, leaving the inner type.
fn fold_paren_type(ty: &mut syn::Type) {
    if !matches!(ty, syn::Type::Paren(_)) {
        return;
    }
    let dummy = syn::Type::Verbatim(proc_macro2::TokenStream::new());
    if let syn::Type::Paren(paren) = core::mem::replace(ty, dummy) {
        *ty = *paren.elem;
    }
}

/// Move the bounds written on type and lifetime parameters into the `where`
/// clause, ahead of its own predicates and in parameter order, the place
/// they would take written there.
fn bounds_to_where(generics: &mut syn::Generics) {
    let mut moved = Vec::new();
    for param in &mut generics.params {
        match param {
            syn::GenericParam::Type(ty) if !ty.bounds.is_empty() => {
                ty.colon_token = None;
                moved.push(syn::WherePredicate::Type(syn::PredicateType {
                    attrs: Vec::new(),
                    lifetimes: None,
                    bounded_ty: syn::Type::Path(syn::TypePath {
                        attrs: Vec::new(),
                        qself: None,
                        path: ty.ident.clone().into(),
                    }),
                    colon_token: syn::token::Colon::default(),
                    bounds: core::mem::take(&mut ty.bounds),
                }));
            }
            syn::GenericParam::Lifetime(lifetime) if !lifetime.bounds.is_empty() => {
                lifetime.colon_token = None;
                moved.push(syn::WherePredicate::Lifetime(syn::PredicateLifetime {
                    attrs: Vec::new(),
                    lifetime: lifetime.lifetime.clone(),
                    colon_token: syn::token::Colon::default(),
                    bounds: core::mem::take(&mut lifetime.bounds),
                }));
            }
            _ => {}
        }
    }
    if moved.is_empty() {
        return;
    }
    let clause = generics.make_where_clause();
    let written = core::mem::take(&mut clause.predicates);
    clause.predicates.extend(moved);
    clause.predicates.extend(written);
}

/// Bounds retain their order and values.
fn drop_trailing_plus<T>(bounds: &mut syn::punctuated::Punctuated<T, syn::Token![+]>) {
    bounds.pop_punct();
}

/// Function-trait output types can absorb a following bound separator.
fn fn_sugar_output(path: &syn::Path) -> bool {
    matches!(
        path.segments.last().map(|segment| &segment.arguments),
        Some(syn::PathArguments::Parenthesized(args)) if matches!(args.output, syn::ReturnType::Type(..))
    )
}

/// Drop every `'_` argument from angle-bracketed path arguments, and the
/// brackets once nothing is left, `Foo<'_>` spelling `Foo`.
fn drop_anonymous_lifetimes(arguments: &mut syn::PathArguments) {
    let syn::PathArguments::AngleBracketed(angle) = arguments else {
        return;
    };
    let anonymous = |arg: &syn::GenericArgument| matches!(arg, syn::GenericArgument::Lifetime(l) if l.ident == "_");
    if !angle.args.iter().any(anonymous) {
        return;
    }
    angle.args = core::mem::take(&mut angle.args)
        .into_iter()
        .filter(|arg| !anonymous(arg))
        .collect();
    if angle.args.is_empty() {
        *arguments = syn::PathArguments::None;
    }
}

/// The derives whose expansions are independent of each other, so their
/// order and grouping carry no meaning.
pub(crate) const STD_DERIVES: [&str; 9] = [
    "Clone",
    "Copy",
    "Debug",
    "Default",
    "Eq",
    "Hash",
    "Ord",
    "PartialEq",
    "PartialOrd",
];

/// The std formatting macros, each with its format string's index among the arguments and
/// whether it is a panic, whose lone literal before edition 2021 is no format string.
const FORMAT_MACROS: [(&str, usize, bool); 18] = [
    ("print", 0, false),
    ("println", 0, false),
    ("eprint", 0, false),
    ("eprintln", 0, false),
    ("format", 0, false),
    ("format_args", 0, false),
    ("write", 1, false),
    ("writeln", 1, false),
    ("panic", 0, true),
    ("unreachable", 0, true),
    ("todo", 0, true),
    ("unimplemented", 0, true),
    ("assert", 1, true),
    ("debug_assert", 1, true),
    ("assert_eq", 2, true),
    ("assert_ne", 2, true),
    ("debug_assert_eq", 2, true),
    ("debug_assert_ne", 2, true),
];

/// The crate roots the paths of std macros start from.
const CRATE_ROOTS: [&str; 3] = ["std", "core", "alloc"];

/// The bit of a std name a local binding can change, a std derive, a std formatting macro or a
/// crate root, zero for any other name.
pub(crate) fn std_name_bit(name: &str) -> u32 {
    STD_DERIVES
        .into_iter()
        .chain(FORMAT_MACROS.map(|(name, _, _)| name))
        .chain(CRATE_ROOTS)
        .position(|std| std == name)
        .map_or(0, |index| 1 << index)
}

/// The std names one scope's items may rebind, a bit per name, every bit set by a glob whose
/// module may hold a macro or a derive under a std name.
#[derive(Clone, Copy)]
struct StdScope {
    /// A module scope, whose bindings hide nothing from the scopes outside it.
    module: bool,
    rebound: u32,
    /// The most enclosing module levels a `use super::*` of the scope reaches, whose bindings
    /// the scope sees too.
    supers: usize,
}

impl StdScope {
    const ALL: u32 = ALL_STD_NAMES;

    fn of<'a>(items: impl Iterator<Item = &'a syn::Item>, module: bool) -> Self {
        let mut scope = Self {
            module,
            rebound: 0,
            supers: 0,
        };
        for item in items {
            scope.bind(item);
        }
        scope
    }

    /// Add the std names `item` binds to something else. A type-namespace item can only hide
    /// a crate root, a derive or a macro lives in the macro namespace.
    fn bind(&mut self, item: &syn::Item) {
        let bit = |ident: &syn::Ident| std_name_bit(&crate::scope::ident_name(ident));
        let root = |ident: &syn::Ident| {
            let name = crate::scope::ident_name(ident);
            if CRATE_ROOTS.contains(&name.as_str()) {
                std_name_bit(&name)
            } else {
                0
            }
        };
        let bits = match item {
            syn::Item::Use(item) => {
                self.bind_tree(&item.tree, &mut Vec::new());
                0
            }
            syn::Item::ExternCrate(krate) => krate
                .rename
                .as_ref()
                .filter(|(_, rename)| *rename != krate.ident)
                .map_or(0, |(_, rename)| root(rename)),
            syn::Item::Macro(syn::ItemMacro {
                ident: Some(ident), ..
            }) => bit(ident),
            _ => crate::alpha::type_item_ident(item).map_or(0, root),
        };
        self.rebound |= bits;
    }

    /// Add the std names `tree` binds to anything but the std item of that name. A path through
    /// a crate root re-exports the std derive or macro under its own name, and every other
    /// binding of the name may be another macro.
    fn bind_tree<'a>(&mut self, tree: &'a syn::UseTree, prefix: &mut Vec<&'a syn::Ident>) {
        let bit = |ident: &syn::Ident| std_name_bit(&crate::scope::ident_name(ident));
        let from_std = matches!(glob_target(prefix), GlobTarget::Std);
        match tree {
            syn::UseTree::Path(path) => {
                prefix.push(&path.ident);
                self.bind_tree(&path.tree, prefix);
                prefix.pop();
            }
            syn::UseTree::Name(_) if from_std => {}
            syn::UseTree::Name(name) => self.rebound |= bit(&name.ident),
            syn::UseTree::Rename(rename) if from_std && rename.ident == rename.rename => {}
            syn::UseTree::Rename(rename) => self.rebound |= bit(&rename.rename),
            syn::UseTree::Glob(_) => match glob_target(prefix) {
                GlobTarget::Std => {}
                GlobTarget::Ancestor(levels) => self.supers = self.supers.max(levels),
                GlobTarget::Unknown => self.rebound = Self::ALL,
            },
            syn::UseTree::Group(group) => {
                for tree in &group.items {
                    self.bind_tree(tree, prefix);
                }
            }
        }
    }
}

/// The module a glob import reads, as far as the std names it may bring in.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum GlobTarget {
    /// A module of `std`, `core` or `alloc`, whose names are the std items themselves.
    Std,
    /// The module that many levels above the importing one, zero for the importing module.
    Ancestor(usize),
    /// Any other module, which may hold a macro or a derive under any std name.
    Unknown,
}

/// The module a glob of the `use` path `prefix` reads. A crate root names the std crate, an
/// ambiguity with a local module of that name being an error.
pub(crate) fn glob_target<S: ?Sized + PartialEq<str>>(prefix: &[&S]) -> GlobTarget {
    let is = |segment: &&S, name: &str| **segment == *name;
    match prefix {
        [first, ..] if CRATE_ROOTS.iter().any(|root| is(first, root)) => GlobTarget::Std,
        [only] if is(only, "self") => GlobTarget::Ancestor(0),
        [_, ..] if prefix.iter().all(|segment| is(segment, "super")) => {
            GlobTarget::Ancestor(prefix.len())
        }
        _ => GlobTarget::Unknown,
    }
}

/// Whether a glob of the `use` path `prefix` may bring in a macro under a std name the scopes
/// visible to it do not already show, `depth` counting the inline modules around the import
/// whose bindings stay in view.
pub(crate) fn macro_glob<S: ?Sized + PartialEq<str>>(prefix: &[&S], depth: usize) -> bool {
    match glob_target(prefix) {
        GlobTarget::Std => false,
        GlobTarget::Ancestor(levels) => levels > depth,
        GlobTarget::Unknown => true,
    }
}

/// Every std name bit of [`std_name_bit`].
pub(crate) const ALL_STD_NAMES: u32 =
    (1 << (STD_DERIVES.len() + FORMAT_MACROS.len() + CRATE_ROOTS.len())) - 1;

/// The std name bit an import of `prefix` under `alias` rebinds, zero for a path through a std
/// crate root that keeps the std item under its own name.
pub(crate) fn import_rebound<S: ?Sized + PartialEq<str>>(
    prefix: &[&S],
    name: &str,
    alias: &str,
) -> u32 {
    if matches!(glob_target(prefix), GlobTarget::Std) && name == alias {
        0
    } else {
        std_name_bit(alias)
    }
}

/// The std names a file rebinds wherever it sits, by a `macro_rules!` definition of the name,
/// whose textual scope reaches past its module, or by a `#[macro_use] extern crate`, whose
/// macros the whole crate sees ahead of the std ones.
pub(crate) fn file_rebound(file: &syn::File) -> u32 {
    struct Definitions(u32);

    impl<'ast> syn::visit::Visit<'ast> for Definitions {
        fn visit_item_macro(&mut self, item: &'ast syn::ItemMacro) {
            if let Some(ident) = &item.ident {
                self.0 |= std_name_bit(&crate::scope::ident_name(ident));
            }
        }

        fn visit_item_extern_crate(&mut self, krate: &'ast syn::ItemExternCrate) {
            if krate
                .attrs
                .iter()
                .any(|attr| attr.path().is_ident("macro_use"))
            {
                self.0 = StdScope::ALL;
            }
        }
    }

    let mut definitions = Definitions(0);
    syn::visit::visit_file(&mut definitions, file);
    definitions.0
}

/// Whether a module's `items` leave every std name bound to the std item, and how many module
/// levels above it a `use super::*` reaches, whose bindings it sees too.
pub(crate) fn module_std_names(items: &[syn::Item]) -> (bool, usize) {
    let scope = StdScope::of(items.iter(), true);
    (scope.rebound == 0, scope.supers)
}

/// Whether `attrs` derive anything outside the std derives, a proc macro that reads the item.
pub(crate) fn has_macro_derive(attrs: &[syn::Attribute]) -> bool {
    attrs.iter().any(|attr| macro_derive_meta(&attr.meta))
}

/// Whether `meta` is a `derive` naming anything outside the std derives. A raw identifier
/// marks a std name another binding may hold.
pub(crate) fn macro_derive_meta(meta: &syn::Meta) -> bool {
    use syn::parse::Parser;

    let parser = syn::punctuated::Punctuated::<syn::Path, syn::Token![,]>::parse_terminated;
    meta.path().is_ident("derive")
        && meta.require_list().map_or(true, |list| {
            parser.parse2(list.tokens.clone()).map_or(true, |paths| {
                paths.iter().any(|path| {
                    path.get_ident()
                        .is_none_or(|name| !STD_DERIVES.iter().any(|std| name == std))
                })
            })
        })
}

pub(crate) fn has_macro_attribute(attrs: &[syn::Attribute]) -> bool {
    attrs.iter().any(|attr| macro_meta(&attr.meta))
}

/// Whether the attribute `meta` may be a macro that reads the item it sits on. A `cfg_attr` or
/// an `unsafe` wrapper reads as the attributes it holds.
pub(crate) fn macro_meta(meta: &syn::Meta) -> bool {
    let path = meta.path();
    if path.is_ident("cfg_attr") || path.is_ident("unsafe") {
        let skip = usize::from(path.is_ident("cfg_attr"));
        return crate::context::wrapped(meta, skip).is_none_or(|metas| {
            metas
                .iter()
                .any(|meta| macro_meta(meta) || macro_derive_meta(meta))
        });
    }
    // Built-in tool namespaces hold no macros.
    let tool = path.segments.len() > 1
        && path.segments.first().is_some_and(|first| {
            [
                "clippy",
                "diagnostic",
                "miri",
                "rust_analyzer",
                "rustdoc",
                "rustfmt",
            ]
            .iter()
            .any(|tool| first.ident == tool)
        });
    !tool
        && !inert_meta(meta)
        && ![
            "cfg",
            "derive",
            "deny",
            "forbid",
            "inline",
            "cold",
            "repr",
            "must_use",
            "deprecated",
            "non_exhaustive",
            "track_caller",
            "no_mangle",
            "export_name",
            "link_name",
            "link_section",
            "automatically_derived",
            "macro_use",
            "macro_export",
            "path",
            "no_std",
            "no_main",
            "no_implicit_prelude",
            "feature",
            "recursion_limit",
            "type_length_limit",
            "crate_name",
            "crate_type",
            "windows_subsystem",
        ]
        .iter()
        .any(|name| path.is_ident(name))
}

/// Merge an item's `derive` attributes into one sorted list at the place of
/// the first, when every derive among them is a std one. A proc macro derive
/// can depend on what ran before it, so any other name keeps them as written.
fn fold_std_derives(attrs: &mut Vec<syn::Attribute>) {
    use syn::parse::Parser;

    let mut names: Vec<syn::Ident> = Vec::new();
    let mut first = None;
    for (index, attr) in attrs.iter().enumerate() {
        if !attr.path().is_ident("derive") {
            continue;
        }
        let syn::Meta::List(list) = &attr.meta else {
            return;
        };
        let parser = syn::punctuated::Punctuated::<syn::Path, syn::Token![,]>::parse_terminated;
        let Ok(paths) = parser.parse2(list.tokens.clone()) else {
            return;
        };
        for path in paths {
            match path.get_ident() {
                Some(name) if STD_DERIVES.contains(&name.to_string().as_str()) => {
                    names.push(name.clone());
                }
                _ => return,
            }
        }
        first.get_or_insert(index);
    }
    let Some(first) = first else {
        return;
    };
    names.sort_by_cached_key(ToString::to_string);
    attrs.retain(|attr| !attr.path().is_ident("derive"));
    attrs.insert(first, syn::parse_quote!(#[derive(#(#names),*)]));
}

/// Elides the `'static` of references in a `const` or `static` type, where an
/// elided lifetime is `'static`. Inside a fn pointer or `Fn` sugar elision
/// means a fresh higher-ranked lifetime, so neither is entered.
struct StaticRefs;

impl VisitMut for StaticRefs {
    fn visit_type_reference_mut(&mut self, node: &mut syn::TypeReference) {
        if node.lifetime.as_ref().is_some_and(|l| l.ident == "static") {
            node.lifetime = None;
        }
        syn::visit_mut::visit_type_reference_mut(self, node);
    }

    fn visit_type_fn_ptr_mut(&mut self, _: &mut syn::TypeFnPtr) {}

    fn visit_parenthesized_generic_arguments_mut(
        &mut self,
        _: &mut syn::ParenthesizedGenericArguments,
    ) {
    }
}

/// True for attributes that carry no program meaning for doctests: doc
/// comments, lint-level directives, and the `rustfmt::` tool attributes. A
/// `doc` whose value is a macro call runs at compile time and can fail, so it
/// stays.
fn is_inert_attr(attr: &syn::Attribute) -> bool {
    inert_meta(&attr.meta)
}

fn inert_meta(meta: &syn::Meta) -> bool {
    let p = meta.path();
    (p.is_ident("doc") && !computed_doc(meta))
        || p.is_ident("allow")
        || p.is_ident("expect")
        || p.is_ident("warn")
        || p.segments.first().is_some_and(|s| s.ident == "rustfmt")
}

/// Whether `meta` is a `doc = …` whose value is a macro call.
fn computed_doc(meta: &syn::Meta) -> bool {
    matches!(meta, syn::Meta::NameValue(nv) if matches!(nv.value, syn::Expr::Macro(_)))
}

/// Drop inert attributes from the list.
fn strip_inert_attrs(attrs: &mut Vec<syn::Attribute>) {
    attrs.retain(|attr| !is_inert_attr(attr));
}

fn pat_attrs(pat: &mut syn::Pat) -> Option<&mut Vec<syn::Attribute>> {
    Some(match pat {
        syn::Pat::Const(p) => &mut p.attrs,
        syn::Pat::Guard(p) => &mut p.attrs,
        syn::Pat::Ident(p) => &mut p.attrs,
        syn::Pat::Lit(p) => &mut p.attrs,
        syn::Pat::Macro(p) => &mut p.attrs,
        syn::Pat::Or(p) => &mut p.attrs,
        syn::Pat::Paren(p) => &mut p.attrs,
        syn::Pat::Path(p) => &mut p.attrs,
        syn::Pat::Range(p) => &mut p.attrs,
        syn::Pat::Reference(p) => &mut p.attrs,
        syn::Pat::Rest(p) => &mut p.attrs,
        syn::Pat::Slice(p) => &mut p.attrs,
        syn::Pat::Struct(p) => &mut p.attrs,
        syn::Pat::Tuple(p) => &mut p.attrs,
        syn::Pat::TupleStruct(p) => &mut p.attrs,
        syn::Pat::Type(p) => &mut p.attrs,
        syn::Pat::Wild(p) => &mut p.attrs,
        _ => return None,
    })
}

pub(crate) fn type_attrs(ty: &syn::Type) -> &[syn::Attribute] {
    match ty {
        syn::Type::Array(ty) => &ty.attrs,
        syn::Type::FnPtr(ty) => &ty.attrs,
        syn::Type::Group(ty) => &ty.attrs,
        syn::Type::ImplTrait(ty) => &ty.attrs,
        syn::Type::Macro(ty) => &ty.attrs,
        syn::Type::Never(ty) => &ty.attrs,
        syn::Type::Paren(ty) => &ty.attrs,
        syn::Type::Path(ty) => &ty.attrs,
        syn::Type::Ptr(ty) => &ty.attrs,
        syn::Type::Reference(ty) => &ty.attrs,
        syn::Type::Slice(ty) => &ty.attrs,
        syn::Type::TraitObject(ty) => &ty.attrs,
        syn::Type::Tuple(ty) => &ty.attrs,
        _ => &[],
    }
}

pub(crate) fn expr_attrs(expr: &syn::Expr) -> &[syn::Attribute] {
    match expr {
        syn::Expr::Array(expr) => &expr.attrs,
        syn::Expr::Assign(expr) => &expr.attrs,
        syn::Expr::Async(expr) => &expr.attrs,
        syn::Expr::Await(expr) => &expr.attrs,
        syn::Expr::Binary(expr) => &expr.attrs,
        syn::Expr::Block(expr) => &expr.attrs,
        syn::Expr::Break(expr) => &expr.attrs,
        syn::Expr::Call(expr) => &expr.attrs,
        syn::Expr::Cast(expr) => &expr.attrs,
        syn::Expr::Closure(expr) => &expr.attrs,
        syn::Expr::Const(expr) => &expr.attrs,
        syn::Expr::Continue(expr) => &expr.attrs,
        syn::Expr::Field(expr) => &expr.attrs,
        syn::Expr::ForLoop(expr) => &expr.attrs,
        syn::Expr::Group(expr) => &expr.attrs,
        syn::Expr::If(expr) => &expr.attrs,
        syn::Expr::Index(expr) => &expr.attrs,
        syn::Expr::Let(expr) => &expr.attrs,
        syn::Expr::Lit(expr) => &expr.attrs,
        syn::Expr::Loop(expr) => &expr.attrs,
        syn::Expr::Macro(expr) => &expr.attrs,
        syn::Expr::Match(expr) => &expr.attrs,
        syn::Expr::MethodCall(expr) => &expr.attrs,
        syn::Expr::Paren(expr) => &expr.attrs,
        syn::Expr::Path(expr) => &expr.attrs,
        syn::Expr::Range(expr) => &expr.attrs,
        syn::Expr::RawAddr(expr) => &expr.attrs,
        syn::Expr::Reference(expr) => &expr.attrs,
        syn::Expr::Repeat(expr) => &expr.attrs,
        syn::Expr::Return(expr) => &expr.attrs,
        syn::Expr::Struct(expr) => &expr.attrs,
        syn::Expr::Try(expr) => &expr.attrs,
        syn::Expr::TryBlock(expr) => &expr.attrs,
        syn::Expr::Tuple(expr) => &expr.attrs,
        syn::Expr::Unary(expr) => &expr.attrs,
        syn::Expr::Unsafe(expr) => &expr.attrs,
        syn::Expr::While(expr) => &expr.attrs,
        syn::Expr::Yield(expr) => &expr.attrs,
        _ => &[],
    }
}

pub(crate) fn item_attrs(item: &mut syn::Item) -> Option<&mut Vec<syn::Attribute>> {
    Some(match item {
        syn::Item::Const(v) => &mut v.attrs,
        syn::Item::Enum(v) => &mut v.attrs,
        syn::Item::ExternCrate(v) => &mut v.attrs,
        syn::Item::Fn(v) => &mut v.attrs,
        syn::Item::ForeignMod(v) => &mut v.attrs,
        syn::Item::Impl(v) => &mut v.attrs,
        syn::Item::Macro(v) => &mut v.attrs,
        syn::Item::Mod(v) => &mut v.attrs,
        syn::Item::Static(v) => &mut v.attrs,
        syn::Item::Struct(v) => &mut v.attrs,
        syn::Item::Trait(v) => &mut v.attrs,
        syn::Item::TraitAlias(v) => &mut v.attrs,
        syn::Item::Type(v) => &mut v.attrs,
        syn::Item::Union(v) => &mut v.attrs,
        syn::Item::Use(v) => &mut v.attrs,
        _ => return None,
    })
}

/// Replace the `use` entries of `list` by one sorted item per leaf path,
/// placed at the front.
fn hoist_uses<T>(
    list: &mut Vec<T>,
    into_use: impl Fn(T) -> Result<syn::ItemUse, T>,
    wrap: impl Fn(syn::ItemUse) -> T,
) {
    let mut leaves = Vec::new();
    let rest: Vec<T> = core::mem::take(list)
        .into_iter()
        .filter_map(|entry| {
            let item = match into_use(entry) {
                Ok(item) => item,
                Err(entry) => return Some(entry),
            };
            let mut trees = Vec::new();
            flatten_use_tree(&[], item.tree, &mut trees);
            leaves.extend(trees.into_iter().map(|tree| syn::ItemUse {
                attrs: item.attrs.clone(),
                vis: item.vis.clone(),
                use_token: item.use_token,
                leading_colon: item.leading_colon,
                tree,
                semi_token: item.semi_token,
            }));
            None
        })
        .collect();
    leaves.sort_by_cached_key(|leaf| {
        format!(
            "{} {}",
            leaf.vis.to_token_stream(),
            leaf.tree.to_token_stream()
        )
    });
    list.extend(leaves.into_iter().map(wrap));
    list.extend(rest);
}

/// Hoist non-`use`, non-`macro_rules!` item statements that appear before
/// the first `macro_rules!` definition in a block to the front, right
/// after the sorted `use` items placed there by `hoist_uses`.
///
/// Only items whose attributes `strip_inert_attrs` would all remove are
/// hoisted. A live attribute may reference a local binding, and hoisting
/// it before that binding changes the visit order and breaks alpha
/// renaming.
///
/// Items in that region move to the front in their original relative order
/// among items. Non-item statements follow them in their original relative
/// order. Everything from the first `Stmt::Item(Item::Macro)` onward is
/// untouched, because `macro_rules!` visibility is sequential.
fn hoist_block_items(stmts: &mut Vec<syn::Stmt>) {
    let use_count = stmts.partition_point(|s| matches!(s, syn::Stmt::Item(syn::Item::Use(_))));

    let rel_macro = stmts[use_count..]
        .iter()
        .position(|s| matches!(s, syn::Stmt::Item(syn::Item::Macro(_))));
    let hoist_end = use_count + rel_macro.unwrap_or(stmts.len() - use_count);

    if hoist_end == use_count {
        return;
    }

    let tail = stmts.split_off(hoist_end);
    let region = stmts.split_off(use_count);

    let mut items = Vec::new();
    let mut rest = Vec::new();
    for mut stmt in region {
        let hoistable = match &mut stmt {
            syn::Stmt::Item(item) => {
                item_attrs(item).is_none_or(|attrs| attrs.iter().all(is_inert_attr))
            }
            _ => false,
        };
        if hoistable {
            items.push(stmt);
        } else {
            rest.push(stmt);
        }
    }

    stmts.extend(items);
    stmts.extend(rest);
    stmts.extend(tail);
}

/// Split `tree` into its leaf paths, `a::{self}` becoming `a`.
fn flatten_use_tree(prefix: &[syn::Ident], tree: syn::UseTree, out: &mut Vec<syn::UseTree>) {
    match tree {
        syn::UseTree::Path(path) => {
            let mut prefix = prefix.to_vec();
            prefix.push(path.ident);
            flatten_use_tree(&prefix, *path.tree, out);
        }
        syn::UseTree::Group(group) => {
            for item in group.items {
                flatten_use_tree(prefix, item, out);
            }
        }
        syn::UseTree::Name(name) if name.ident == "self" => {
            if let [head @ .., last] = prefix {
                let leaf = syn::UseTree::Name(syn::UseName {
                    ident: last.clone(),
                });
                out.push(path_tree(head, leaf));
            }
        }
        leaf => out.push(path_tree(prefix, leaf)),
    }
}

fn path_tree(prefix: &[syn::Ident], leaf: syn::UseTree) -> syn::UseTree {
    match prefix {
        [] => leaf,
        [head, rest @ ..] => syn::UseTree::Path(syn::UsePath {
            ident: head.clone(),
            colon2_token: syn::token::PathSep::default(),
            tree: Box::new(path_tree(rest, leaf)),
        }),
    }
}

/// Rewrite the outer delimiter of a macro call to parentheses.
fn normalize_macro_delim(mac: &mut syn::Macro) {
    if matches!(mac.delimiter, syn::MacroDelimiter::Paren(_)) {
        return;
    }
    let span = *mac.delimiter.span();
    mac.delimiter = syn::MacroDelimiter::Paren(syn::token::Paren { span });
}

/// The format string's index among a formatting macro's arguments and whether the macro is a
/// panic, whose lone literal before edition 2021 is no format string, for a call whose last
/// path segment spells a std formatting macro.
pub(crate) fn format_operand(path: &syn::Path) -> Option<(usize, bool)> {
    let name = path.segments.last()?.ident.to_string();
    FORMAT_MACROS
        .iter()
        .find(|(std, _, _)| *std == name)
        .map(|&(_, at, panic)| (at, panic))
}

/// The [`format_operand`] of a call `path` proves to name a std formatting macro, with
/// `rebound` the std names the scopes around the call bind to something else. A bare name
/// needs its own name free, a `std::`, `alloc::format` or `core::format_args` path its root.
fn std_format_operand(path: &syn::Path, rebound: u32) -> Option<(usize, bool)> {
    let operand = format_operand(path)?;
    let free = |ident: &syn::Ident| rebound & std_name_bit(&crate::scope::ident_name(ident)) == 0;
    let leaf = &path.segments.last()?.ident;
    let plain = path
        .segments
        .iter()
        .all(|segment| segment.arguments.is_none());
    let proven = match path.segments.len() {
        1 => path.leading_colon.is_none() && free(leaf),
        2 => {
            let root = &path.segments[0].ident;
            (root == "std"
                || (root == "alloc" && leaf == "format")
                || (root == "core" && leaf == "format_args"))
                && (path.leading_colon.is_some() || free(root))
        }
        _ => false,
    };
    (plain && proven).then_some(operand)
}

/// Move identifier arguments of a std formatting macro into its format string,
/// `println!("{}", x)` becoming `println!("{x}")`, in `mac` and in every proven formatting
/// call among its arguments, with `rebound` the std names the scopes around `mac` bind to
/// something else. Any other macro keeps its tokens as written. Returns whether `mac` changed.
fn inline_format_args(mac: &mut syn::Macro, rebound: u32) -> bool {
    use syn::parse::Parser;

    let Some((at, panic)) = std_format_operand(&mac.path, rebound) else {
        return false;
    };
    let parser = syn::punctuated::Punctuated::<syn::Expr, syn::Token![,]>::parse_terminated;
    let Ok(mut args) = parser.parse2(mac.tokens.clone()) else {
        return false;
    };
    let mut nested = NestedFormat {
        rebound,
        changed: false,
    };
    for arg in &mut args {
        nested.visit_expr_mut(arg);
    }
    let inlined = !panic && inline_positional(at, &mut args);
    if nested.changed || inlined {
        mac.tokens = args.to_token_stream();
    }
    nested.changed || inlined
}

/// Inlines the proven formatting calls inside a macro's arguments, which the drift visit never
/// enters.
struct NestedFormat {
    rebound: u32,
    changed: bool,
}

impl VisitMut for NestedFormat {
    fn visit_block_mut(&mut self, block: &mut syn::Block) {
        let outer = self.rebound;
        let scope = StdScope::of(
            block.stmts.iter().filter_map(|stmt| match stmt {
                syn::Stmt::Item(item) => Some(item),
                _ => None,
            }),
            false,
        );
        // A `use super::*` here reaches past the scopes the call's rebound covers.
        self.rebound |= if scope.supers > 0 {
            StdScope::ALL
        } else {
            scope.rebound
        };
        syn::visit_mut::visit_block_mut(self, block);
        self.rebound = outer;
    }

    fn visit_macro_mut(&mut self, mac: &mut syn::Macro) {
        self.changed |= inline_format_args(mac, self.rebound);
    }
}

/// Inline the identifier arguments after the format string at `at` among `args`. Returns
/// whether anything moved.
fn inline_positional(
    at: usize,
    args: &mut syn::punctuated::Punctuated<syn::Expr, syn::Token![,]>,
) -> bool {
    let positional: Vec<&syn::Expr> = args.iter().skip(at + 1).collect();
    if positional
        .iter()
        .any(|arg| matches!(arg, syn::Expr::Assign(_)))
    {
        return false;
    }
    let names: Vec<Option<String>> = positional.into_iter().map(capturable).collect();
    let Some(syn::Expr::Lit(syn::ExprLit {
        lit: syn::Lit::Str(format),
        ..
    })) = args.iter_mut().nth(at)
    else {
        return false;
    };
    let Some(text) = inline_placeholders(&format.value(), &names) else {
        return false;
    };
    *format = syn::LitStr::new(&text, format.span());
    let kept = core::mem::take(args)
        .into_iter()
        .enumerate()
        .filter(|(index, _)| *index <= at || names[index - at - 1].is_none())
        .map(|(_, arg)| arg);
    args.extend(kept);
    true
}

/// The identifier a format string captures for `arg`, `None` for any other
/// expression and for the names a placeholder cannot spell.
fn capturable(arg: &syn::Expr) -> Option<String> {
    let syn::Expr::Path(path) = arg else {
        return None;
    };
    if !path.attrs.is_empty() {
        return None;
    }
    let name = path.path.get_ident()?.to_string();
    let spellable =
        !name.starts_with("r#") && !matches!(name.as_str(), "self" | "Self" | "crate" | "super");
    spellable.then_some(name)
}

/// `format` with each implicit `{}` or `{:spec}` placeholder whose argument
/// in `names` is an identifier spelled inline. `None` when nothing moves, or
/// when an explicit index, a `$` or `*` spec or a count mismatch makes the
/// argument order matter.
fn inline_placeholders(format: &str, names: &[Option<String>]) -> Option<String> {
    let mut out = String::with_capacity(format.len());
    let mut next = 0;
    let mut chars = format.chars().peekable();
    while let Some(c) = chars.next() {
        match c {
            '{' | '}' if chars.peek() == Some(&c) => {
                chars.next();
                out.push(c);
                out.push(c);
            }
            '{' => {
                let mut body = String::new();
                loop {
                    match chars.next()? {
                        '}' => break,
                        c => body.push(c),
                    }
                }
                let (arg, spec) = body
                    .split_once(':')
                    .map_or((body.as_str(), None), |(a, s)| (a, Some(s)));
                let explicit_index = !arg.is_empty() && arg.bytes().all(|b| b.is_ascii_digit());
                if explicit_index || spec.is_some_and(|spec| spec.contains(['$', '*'])) {
                    return None;
                }
                out.push('{');
                if arg.is_empty() {
                    out.push_str(names.get(next)?.as_deref().unwrap_or_default());
                    next += 1;
                } else {
                    out.push_str(arg);
                }
                if let Some(spec) = spec {
                    out.push(':');
                    out.push_str(spec);
                }
                out.push('}');
            }
            '}' => return None,
            c => out.push(c),
        }
    }
    (next == names.len() && names.iter().any(Option::is_some)).then_some(out)
}