flake8_executable: Only match shebang at beginning of line (#2183)

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 <andersk@mit.edu>

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg
2023-01-25 15:57:09 -08:00
committed by GitHub
parent edd0e16a02
commit 6036d1bbe2
2 changed files with 12 additions and 3 deletions

View File

@@ -2,7 +2,7 @@ use once_cell::sync::Lazy;
use regex::Regex;
static SHEBANG_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(?P<spaces>\s*)#!(?P<directive>.*)").unwrap());
Lazy::new(|| Regex::new(r"^(?P<spaces>\s*)#!(?P<directive>.*)").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
));
}
}

View File

@@ -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: ~