[llvm-branch-commits] [clang] 5340f7c - [git-clang-format] Don't format the line preceding a deletion (#215946)

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Sep 1 01:28:49 PDT 2026


Author: Fangrui Song
Date: 2026-09-01T10:28:34+02:00
New Revision: 5340f7cc8814b89c01a786e85e1736e10159a9f0

URL: https://github.com/llvm/llvm-project/commit/5340f7cc8814b89c01a786e85e1736e10159a9f0
DIFF: https://github.com/llvm/llvm-project/commit/5340f7cc8814b89c01a786e85e1736e10159a9f0.diff

LOG: [git-clang-format] Don't format the line preceding a deletion (#215946)

`git diff -U0` renders a pure deletion as `@@ -3,3 +2,0 @@`: no new
lines,
anchored at the preceding line. extract_lines coerces that zero count to
one, so clang-format reformats a line the deletion never touched.

Skip such hunks, matching clang-format-diff.py. start_line is 0 only for
deletions at the start of a file, so that check goes away as well.

Aided by Claude Opus 5

(cherry picked from commit fe0143b1a97484e10bc11b012ceb2c1ecd8bc38c)

Added: 
    

Modified: 
    clang/tools/clang-format/git-clang-format

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index c9319c55213a3..d9b2d501c00e3 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -480,9 +480,10 @@ def extract_lines(patch_file, whole_file):
                 line_count = 1
                 if match.group(3):
                     line_count = int(match.group(3))
+                # A hunk that adds no lines is a pure deletion. start_line
+                # refers to the preceding line (0 when the deletion is at the
+                # start of a file), which the deletion left alone.
                 if line_count == 0:
-                    line_count = 1
-                if start_line == 0:
                     continue
                 matches.setdefault(filename, []).append(Range(start_line, line_count))
     return matches


        


More information about the llvm-branch-commits mailing list