Avoid EXE001 and EXE002 errors from stdin input (#3218)

This commit is contained in:
Charlie Marsh 2023-02-24 17:55:32 -05:00 committed by GitHub
parent a17b5c134a
commit 0694aee1b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 15 deletions

View file

@ -1,4 +1,6 @@
#[cfg(target_family = "unix")]
use anyhow::Result;
#[cfg(target_family = "unix")]
use std::os::unix::fs::PermissionsExt;
#[cfg(target_family = "unix")]
use std::path::Path;
@ -39,13 +41,11 @@ pub fn extract_shebang(line: &str) -> ShebangDirective {
}
#[cfg(target_family = "unix")]
pub fn is_executable(filepath: &Path) -> bool {
pub fn is_executable(filepath: &Path) -> Result<bool> {
{
let Ok(metadata) = filepath.metadata() else {
return false;
};
let metadata = filepath.metadata()?;
let permissions = metadata.permissions();
permissions.mode() & 0o111 != 0
Ok(permissions.mode() & 0o111 != 0)
}
}

View file

@ -23,12 +23,11 @@ impl Violation for ShebangMissingExecutableFile {
/// EXE002
#[cfg(target_family = "unix")]
pub fn shebang_missing(filepath: &Path) -> Option<Diagnostic> {
if is_executable(filepath) {
if let Ok(true) = is_executable(filepath) {
let diagnostic = Diagnostic::new(ShebangMissingExecutableFile, Range::default());
Some(diagnostic)
} else {
None
return Some(diagnostic);
}
None
}
#[cfg(not(target_family = "unix"))]

View file

@ -31,9 +31,7 @@ pub fn shebang_not_executable(
shebang: &ShebangDirective,
) -> Option<Diagnostic> {
if let ShebangDirective::Match(_, start, end, _) = shebang {
if is_executable(filepath) {
None
} else {
if let Ok(false) = is_executable(filepath) {
let diagnostic = Diagnostic::new(
ShebangNotExecutable,
Range::new(
@ -41,11 +39,10 @@ pub fn shebang_not_executable(
Location::new(lineno + 1, *end),
),
);
Some(diagnostic)
return Some(diagnostic);
}
} else {
None
}
None
}
#[cfg(not(target_family = "unix"))]