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

View file

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

View file

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