mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Manuel Ebner <manuelebner@mailbox.org>
To: Andy Whitcroft <apw@canonical.com>, Joe Perches <joe@perches.com>,
	Dwaipayan Ray <dwaipayanray1@gmail.com>,
	Lukas Bulwahn <lukas.bulwahn@gmail.com>,
	linux-kernel@vger.kernel.org (open list)
Cc: Manuel Ebner <manuelebner@mailbox.org>
Subject: [RFC] checkpatch: Introduce a check for the nesting of brackets
Date: Tue, 23 Jun 2026 21:09:39 +0200	[thread overview]
Message-ID: <20260623190937.47435-3-manuelebner@mailbox.org> (raw)

I have a local script that checks files for the right nesting of brackets.
With the help of the script I managed to do a couple patches [1]. Unfortunately
there are (still) a lot of false positives [2]. I do not filter false positives
(yet).

To simplify testing the new check is enabled and warns(WARN()). The final
version would be disabled by default and would trigger CHK().

Would this addition, if cleaned up, get into checkpatch.pl?

Thanks
 Manuel

[1] Examples https://lore.kernel.org/all/?q=f%3Amanuelebner%40mailbox.org+AND+d%3A2026-06-11+AND+%28s%3A%22bracket%22+OR+s%3A%22brackets%22+%09OR+s%3A%22cleanup%22+OR+s%3A%22sysfs-firmware-dmi%22
[2] Smileys: ;-) :), :-( :(,
	Enums: 1) 2), i) ii), a) b),
	Arrows: ->, <-
	Intervall: (0, 100], [0, 100)
	Code snippets in Docs: Unclosed '{' or '('
	Scissors: 8<--- cut here ---

---
 scripts/checkpatch.pl | 52 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 0492d6afc9a1..4e8d72825b70 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -75,6 +75,17 @@ my $git_command ='export LANGUAGE=en_US.UTF-8; git';
 my $tabsize = 8;
 my ${CONFIG_} = "CONFIG_";
 
+my %brackets = (
+    ')' => '(',
+    '}' => '{',
+    ']' => '[',
+);
+my @stack_brackets;
+my $line_num = 0;
+my $has_error = 0;
+my $last_open;
+my %open_brackets = reverse %brackets;
+
 my %maybe_linker_symbol; # for externs in c exceptions, when seen in *vmlinux.lds.h
 
 sub help {
@@ -3252,6 +3263,37 @@ sub process {
 			$needs_fixes_tag = $1;
 		}
 
+# Check brackets for matching pairs
+		while ($line =~ /([\(\)\{\}\[\]])/g) {
+			my $char = $1;
+			# If it's an opening bracket, push onto stack
+			if (grep { $_ eq $char } values %brackets) {
+				push @stack_brackets, { char => $char, line => $line_num };
+			}
+			# If it's a closing bracket, validate
+			elsif (exists $brackets{$char}) {
+				my $expected = $brackets{$char};
+				if (!@stack_brackets) {
+					WARN("OPENING_BRACKET_MISSING",
+					"Missing opening bracket\n" .
+					"Found closing '$char' without an opening '$expected'\n" .
+					$herecurr);
+					$has_error = 1;
+					last;
+				}
+				$last_open = pop @stack_brackets;
+				if ($last_open->{char} ne $expected) {
+					WARN("MISMATCH_OF_BRACKETS",
+					"Mismatched brackets error\n" .
+					"Expected '$open_brackets{$last_open->{char}}' but found '$char' on" . "\n" .
+					$herecurr);
+					$has_error = 1;
+					last;
+				}
+			}
+		}
+		$line_num++;
+
 # Check Fixes: styles is correct
 		if (!$in_header_lines &&
 		    $line =~ /^\s*(fixes:?)\s*(?:commit\s*)?([0-9a-f]{5,40})(?:\s*($balanced_parens))?/i) {
@@ -7806,6 +7848,16 @@ sub process {
 		}
 	}
 
+# Check if there are any unclosed brackets remaining
+	if (!$has_error && @stack_brackets) {
+		foreach my $unclosed (reverse @stack_brackets) {
+			print "\n";
+			WARN("CLOSING_BRACKET_MISSING",
+			"closing bracket missing\n" .
+			"Unclosed '$unclosed->{char}' started on line '$unclosed->{line}'\n");
+		}
+	}
+
 	# If we have no input at all, then there is nothing to report on
 	# so just keep quiet.
 	if ($#rawlines == -1) {
-- 
2.54.0


             reply	other threads:[~2026-06-23 19:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-23 19:09 Manuel Ebner [this message]
2026-06-23 19:20 ` Joe Perches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260623190937.47435-3-manuelebner@mailbox.org \
    --to=manuelebner@mailbox.org \
    --cc=apw@canonical.com \
    --cc=dwaipayanray1@gmail.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukas.bulwahn@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®