From 6036d1bbe2401eaff3823d77ea4da7f3a031c323 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 25 Jan 2023 15:57:09 -0800 Subject: [PATCH] flake8_executable: Only match shebang at beginning of line (#2183) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Python implementation uses `re.match` for this, which only matches at the beginning of a line. https://github.com/xuhdev/flake8-executable/blob/v2.1.3/flake8_executable/__init__.py#L124 We could use `Regex::captures_read_at`, but that’s a more complicated API; it’s easier to anchor the regex with `^`. Signed-off-by: Anders Kaseorg Signed-off-by: Anders Kaseorg --- src/rules/flake8_executable/helpers.rs | 4 ++-- ..._rules__flake8_executable__tests__EXE004_3.py.snap | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/rules/flake8_executable/helpers.rs b/src/rules/flake8_executable/helpers.rs index fa147373ab..db47bcba38 100644 --- a/src/rules/flake8_executable/helpers.rs +++ b/src/rules/flake8_executable/helpers.rs @@ -2,7 +2,7 @@ use once_cell::sync::Lazy; use regex::Regex; static SHEBANG_REGEX: Lazy = - Lazy::new(|| Regex::new(r"(?P\s*)#!(?P.*)").unwrap()); + Lazy::new(|| Regex::new(r"^(?P\s*)#!(?P.*)").unwrap()); #[derive(Debug)] pub enum ShebangDirective<'a> { @@ -67,7 +67,7 @@ mod tests { )); assert!(matches!( extract_shebang("print('test') #!/usr/bin/python"), - ShebangDirective::Match(2, 17, 32, "/usr/bin/python") + ShebangDirective::None )); } } diff --git a/src/rules/flake8_executable/snapshots/ruff__rules__flake8_executable__tests__EXE004_3.py.snap b/src/rules/flake8_executable/snapshots/ruff__rules__flake8_executable__tests__EXE004_3.py.snap index c738409c4f..8874a32d9c 100644 --- a/src/rules/flake8_executable/snapshots/ruff__rules__flake8_executable__tests__EXE004_3.py.snap +++ b/src/rules/flake8_executable/snapshots/ruff__rules__flake8_executable__tests__EXE004_3.py.snap @@ -2,5 +2,14 @@ source: src/rules/flake8_executable/mod.rs expression: diagnostics --- -[] +- kind: + ShebangMissingExecutableFile: ~ + location: + row: 1 + column: 0 + end_location: + row: 1 + column: 0 + fix: ~ + parent: ~