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:
@@ -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
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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: ~
|
||||
|
||||
|
||||
Reference in New Issue
Block a user