Author: Fangrui Song
Date: 2026-08-23T01:08:43-07:00
New Revision: fe0143b1a97484e10bc11b012ceb2c1ecd8bc38c

URL: 
https://github.com/llvm/llvm-project/commit/fe0143b1a97484e10bc11b012ceb2c1ecd8bc38c
DIFF: 
https://github.com/llvm/llvm-project/commit/fe0143b1a97484e10bc11b012ceb2c1ecd8bc38c.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

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


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to