>From 6c53e473b06ade90ceb20d52dcc0d8d989601308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= Date: Wed, 22 Feb 2023 00:12:31 +0100 Subject: [PATCH] Fix wrong argument error indenting C code in c-ts-mode * lisp/progmodes/c-ts-common.el (c-ts-common--node-is): (treesit-node-type node) can return nil for some nodes. Protect string-match-p against it. --- lisp/progmodes/c-ts-common.el | 10 ++++++---- .../progmodes/c-ts-mode-resources/indent.erts | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el index 8262e6261d4..4fa60ab71eb 100644 --- a/lisp/progmodes/c-ts-common.el +++ b/lisp/progmodes/c-ts-common.el @@ -299,16 +299,18 @@ c-ts-common--node-is (dolist (type types) (let ((regexp (alist-get type c-ts-common-indent-type-regexp-alist)) - (parent (treesit-node-parent node))) + (parent (treesit-node-parent node)) + (node-field-name (treesit-node-field-name node)) + (node-type (treesit-node-type node))) (when (and regexp (if (consp regexp) (and parent (string-match-p (car regexp) (treesit-node-type parent)) + node-field-name (string-match-p (cdr regexp) - (treesit-node-field-name - node))) - (string-match-p regexp (treesit-node-type node)))) + node-field-name)) + (string-match-p regexp node-type))) (throw 'ret t)))) nil)) diff --git a/test/lisp/progmodes/c-ts-mode-resources/indent.erts b/test/lisp/progmodes/c-ts-mode-resources/indent.erts index 36d7af4faf1..ec4173dd214 100644 --- a/test/lisp/progmodes/c-ts-mode-resources/indent.erts +++ b/test/lisp/progmodes/c-ts-mode-resources/indent.erts @@ -385,3 +385,19 @@ namespace test { }; } =-=-= + +Name: Single Line If With Comment (GNU Style) + +=-= + +int +foo () +{ + int c; + if (!CHAR_VALID_P (c)) + /* This is a comment. */ + c = 'a'; + + return c; +} +=-=-= -- 2.34.1