<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/108333>108333</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang-format generates extra trailing spaces in the last line with InsertNewlineAtEOF enabled
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-format
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
kevinptt0323
</td>
</tr>
</table>
<pre>
clang-format generates extra trailing spaces in the last line when the following three statements are true:
1. `InsertNewlineAtEOF` is true.
1. The file does not end with `EOF`
1. The argument `-lines` is specified
Minimal reproducible example:
```sh
$ clang-format --version
Ubuntu clang-format version 18.1.3 (1ubuntu1)
$ cat test.cpp
namespace foo {
int bar;
} // namespace foo$ xxd test.cpp
00000000: 6e61 6d65 7370 6163 6520 666f 6f20 7b0a namespace foo {.
00000010: 696e 7420 6261 723b 0a7d 202f 2f20 6e61 int bar;.} // na
00000020: 6d65 7370 6163 6520 666f 6f mespace foo
$ clang-format -style '{InsertNewlineAtEOF: true}' test.cpp -lines 1:3 | xxd
00000000: 6e61 6d65 7370 6163 6520 666f 6f20 7b0a namespace foo {.
00000010: 696e 7420 6261 723b 0a7d 202f 2f20 6e61 int bar;.} // na
00000020: 6d65 7370 6163 6520 666f 6f0a 2020 2020 mespace foo.
00000030: 2020 2020 2020 2020 2020 2020 2020
$ clang-format -style '{InsertNewlineAtEOF: true}' test.cpp | xxd
00000000: 6e61 6d65 7370 6163 6520 666f 6f20 7b0a namespace foo {.
00000010: 696e 7420 6261 723b 0a7d 202f 2f20 6e61 int bar;.} // na
00000020: 6d65 7370 6163 6520 666f 6f0a mespace foo.
$ clang-format -style '{InsertNewlineAtEOF: false}' test.cpp -lines 1:3 | xxd
00000000: 6e61 6d65 7370 6163 6520 666f 6f20 7b0a namespace foo {.
00000010: 696e 7420 6261 723b 0a7d 202f 2f20 6e61 int bar;.} // na
00000020: 6d65 7370 6163 6520 666f 6f mespace foo
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzklcGO8zQQx5_GuYwa2ePUTg85dFkqcQAu8ABOM2kMrhPZzm6_t0duwrcp366QgNNiVa4ymfn575lRxsRoL56oYfsntn8uzJyGMTS_04v1U0pcoizasfvSnJ3xl10_hqtJcCFPwSSKQLcUDKRgrLP-AnEyZ4pgPaSBwJmYwFlP8DrQYupH58bX7JqGQAQxmURX8imCCQQpzMTkkfFnxtddlMAU_8FHCuknes24Y_r-5xNTHGy8R5RfPX_JR1hH0I0UwY8JyHfwatOQIUvUg7MJlzkfn1_vMjuu3DjR2faWuq2WH623V-Mg0BTGbj7b1hHQzVwnt5Gt-PKLw2rACh7St9u9UIh29Mv7X9vZp_nRZXUAUZeilMCwFvPdTTA8bLAmQaKYyvM0LVZvrnSvAvTjCEw_LWbrE7QmMLk-M_0MDE8MT_AQkaG3W_cXKF8Xk0dQpASoTu1BS81BCSVB7ZGDUqoH1SMH3XID8I2ScgsTC-ygCHSVo1EJ0Chb4EZ3gBx7wAy7nwdv-ssH6VskLsiPpcHD2t76gzrF9MURMNRMP73TgfK4NKx-Zqi_pgyWPgLB5FEC09_lhH6qPHKTuXzZtnksYcuRd86b54fbe-u_LcknrcL73Vz-89z1xsX_Xz__3Xfhzw960TWyO8iDKagRGpUQhwrrYmiIy_rA-aHiVdd1nTa1MsSRoxS10dQWtkGOFT8I5HupeV3WppJno_ZVrbAX2LGK09VYVzr3ci3HcClsjDM1gtdSysKZlly8D2nEbVEZYh7boclxu3a-RFZxZ2OKb6Rkk6N_O8DzCP22Z4C8aR11xRxcM6Q0xTwG70W42DTMbXkerwxPWcv6t5vC-BudE8PT_YqR4Wm95UuDfwQAAP__s7ZgzQ">