Skip to content

Commit

Permalink
Only run executable rules when they are enabled (#13298)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Sep 10, 2024
1 parent 5ef6979 commit 7c872e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
8 changes: 7 additions & 1 deletion crates/ruff_linter/src/checkers/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@ pub(crate) fn check_tokens(
Rule::ShebangNotFirstLine,
Rule::ShebangMissingPython,
]) {
flake8_executable::rules::from_tokens(&mut diagnostics, path, locator, comment_ranges);
flake8_executable::rules::from_tokens(
&mut diagnostics,
path,
locator,
comment_ranges,
settings,
);
}

if settings.rules.any_enabled(&[
Expand Down
18 changes: 12 additions & 6 deletions crates/ruff_linter/src/rules/flake8_executable/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::path::Path;

use crate::codes::Rule;
use crate::comments::shebang::ShebangDirective;
use crate::settings::LinterSettings;
use ruff_diagnostics::Diagnostic;
use ruff_python_trivia::CommentRanges;
use ruff_source_file::Locator;
Expand All @@ -9,8 +12,6 @@ pub(crate) use shebang_missing_python::*;
pub(crate) use shebang_not_executable::*;
pub(crate) use shebang_not_first_line::*;

use crate::comments::shebang::ShebangDirective;

mod shebang_leading_whitespace;
mod shebang_missing_executable_file;
mod shebang_missing_python;
Expand All @@ -22,6 +23,7 @@ pub(crate) fn from_tokens(
path: &Path,
locator: &Locator,
comment_ranges: &CommentRanges,
settings: &LinterSettings,
) {
let mut has_any_shebang = false;
for range in comment_ranges {
Expand All @@ -33,8 +35,10 @@ pub(crate) fn from_tokens(
diagnostics.push(diagnostic);
}

if let Some(diagnostic) = shebang_not_executable(path, range) {
diagnostics.push(diagnostic);
if settings.rules.enabled(Rule::ShebangNotExecutable) {
if let Some(diagnostic) = shebang_not_executable(path, range) {
diagnostics.push(diagnostic);
}
}

if let Some(diagnostic) = shebang_leading_whitespace(range, locator) {
Expand All @@ -48,8 +52,10 @@ pub(crate) fn from_tokens(
}

if !has_any_shebang {
if let Some(diagnostic) = shebang_missing_executable_file(path) {
diagnostics.push(diagnostic);
if settings.rules.enabled(Rule::ShebangMissingExecutableFile) {
if let Some(diagnostic) = shebang_missing_executable_file(path) {
diagnostics.push(diagnostic);
}
}
}
}

0 comments on commit 7c872e6

Please sign in to comment.