[clang] [git-clang-format] Don't format the line preceding a deletion (PR #215946)
Fangrui Song via cfe-commits
cfe-commits at lists.llvm.org
Sat Aug 22 14:52:13 PDT 2026
https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/215946
>From 5b84c14a8243b2ccb01e201356fc0cb56b070cd9 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Wed, 12 Aug 2026 21:18:16 -0700
Subject: [PATCH] [git-clang-format] Don't format the line preceding a deletion
`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
---
clang/tools/clang-format/git-clang-format | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index c9319c55213a3..125692fcbc6b5 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -480,9 +480,9 @@ 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, and start_line
+ # refers to the preceding line, 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 cfe-commits
mailing list