Skip to content

Commit

Permalink
flake8_executable: Only match shebang at beginning of line
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
andersk committed Jan 25, 2023
1 parent edd0e16 commit ce32035
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/rules/flake8_executable/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: ~

0 comments on commit ce32035

Please sign in to comment.