From 765cce0d3ef02659a3b365fc2ee13d850aec7b90 Mon Sep 17 00:00:00 2001 From: Brad Solomon Date: Tue, 28 Apr 2020 14:01:59 -0400 Subject: [PATCH] Bugfix: account for CRLF in empty-line comments If require-starting-space is true, *and* either: - new-lines: disbale, or - newlines: type: dos is specified, a line with `#\r` or `#\r\n` should not raise a false positive. This commit also uses a Set for O(1) membership testing and uses the correct escape sequence for the nul byte. Closes: Issue #171. --- yamllint/rules/comments.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/yamllint/rules/comments.py b/yamllint/rules/comments.py index 0122838..1e81db5 100644 --- a/yamllint/rules/comments.py +++ b/yamllint/rules/comments.py @@ -97,7 +97,9 @@ def check(conf, comment): comment.column_no == 1 and re.match(r'^!\S', comment.buffer[text_start:])): return - elif comment.buffer[text_start] not in (' ', '\n', '\0'): + # We can test for both \r and \r\n just by checking first char + # \r itself is a valid newline on some older OS. + elif comment.buffer[text_start] not in {' ', '\n', '\r', '\x00'}: column = comment.column_no + text_start - comment.pointer yield LintProblem(comment.line_no, column,