<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/141861>141861</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang-format] git-clang-format can indent modified lines incorrectly
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-format
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          andrurogerz
      </td>
    </tr>
</table>

<pre>
    ## Overview
I observed this behavior while authoring #141650. When running `git clang-format` on the modifications made to `llvm/IR/BasicBlock.h`, it incorrectly indents some function declarations that were modified. However, when running `clang-format` first on the entire file and committing its seemingly unrelated changes, `git clang-format` no longer adds the incorrect indentation.

## Details
Looking into this a bit further, I think it is just a matter of `git clang-format` matching indentation of a modified line to the the indentation of the preceding declaration in the same scope. In this case, the author of `llvm/IR/BasicBlock.h` formatted these two `getParent` method declarations as follows:
```
  const Function *getParent() const { return Parent; }
        Function *getParent()       { return Parent; }
```
While there is something appealing about this format, it incorrectly indents the second `getParent` declaration. #141650 does not modify either of these `getParent` declaration lines, but it does modify the next method declaration in the class. When `git clang-format` reformats the modified line, it matches its indentation to the improperly indented `getParent` declaration.

## Repro
I made a very simple repo [here](https://github.com/andrurogerz/clang-format-issue).

1. Clone the repro source:
```
git clone git@github.com:andrurogerz/clang-format-issue.git
cd clang-format-issue
```
2. Format the `HEAD` change:
```
git clang-format HEAD~1
```
### Expected results
The `example.hpp` file should be unmodified because the line that changed is still formatted correctly.

### Actual results
Indentation of the `doSomething` method in `example.hpp` incorrectly matches the preceding `getSomething` method declaration.
If you instead `clang-format -i example.hpp`, it will fix the indentation of the second `getSomething` method instead (which is correct).
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyEVkGP4jgT_TXmUuooMZCGAwcYBg3SJ32r2ZXm7NhF4hnHjmwHmj3sb1_ZDnTS07BRDlFiV7169eo5zDlZa8QNWe7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