mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
Avoid running logical line rule logic if not enabled (#11951)
## Summary This PR updates the logical line rules entry-point function to only run the logic if any of the rules within that group is enabled. Although this shouldn't really give any performance improvements, it's better not to do additional work if we can. This is also consistent with how other rules are run. ## Test Plan `cargo insta test`
This commit is contained in:
parent
b456051be8
commit
3f884b4b34
2 changed files with 99 additions and 22 deletions
|
@ -6,7 +6,7 @@ use ruff_python_parser::{TokenKind, Tokens};
|
||||||
use ruff_source_file::Locator;
|
use ruff_source_file::Locator;
|
||||||
use ruff_text_size::{Ranged, TextRange};
|
use ruff_text_size::{Ranged, TextRange};
|
||||||
|
|
||||||
use crate::registry::AsRule;
|
use crate::registry::{AsRule, Rule};
|
||||||
use crate::rules::pycodestyle::rules::logical_lines::{
|
use crate::rules::pycodestyle::rules::logical_lines::{
|
||||||
extraneous_whitespace, indentation, missing_whitespace, missing_whitespace_after_keyword,
|
extraneous_whitespace, indentation, missing_whitespace, missing_whitespace_after_keyword,
|
||||||
missing_whitespace_around_operator, redundant_backslash, space_after_comma,
|
missing_whitespace_around_operator, redundant_backslash, space_after_comma,
|
||||||
|
@ -45,36 +45,111 @@ pub(crate) fn check_logical_lines(
|
||||||
let mut prev_indent_level = None;
|
let mut prev_indent_level = None;
|
||||||
let indent_char = stylist.indentation().as_char();
|
let indent_char = stylist.indentation().as_char();
|
||||||
|
|
||||||
|
let enforce_space_around_operator = settings.rules.any_enabled(&[
|
||||||
|
Rule::MultipleSpacesBeforeOperator,
|
||||||
|
Rule::MultipleSpacesAfterOperator,
|
||||||
|
Rule::TabBeforeOperator,
|
||||||
|
Rule::TabAfterOperator,
|
||||||
|
]);
|
||||||
|
let enforce_whitespace_around_named_parameter_equals = settings.rules.any_enabled(&[
|
||||||
|
Rule::UnexpectedSpacesAroundKeywordParameterEquals,
|
||||||
|
Rule::MissingWhitespaceAroundParameterEquals,
|
||||||
|
]);
|
||||||
|
let enforce_missing_whitespace_around_operator = settings.rules.any_enabled(&[
|
||||||
|
Rule::MissingWhitespaceAroundOperator,
|
||||||
|
Rule::MissingWhitespaceAroundArithmeticOperator,
|
||||||
|
Rule::MissingWhitespaceAroundBitwiseOrShiftOperator,
|
||||||
|
Rule::MissingWhitespaceAroundModuloOperator,
|
||||||
|
]);
|
||||||
|
let enforce_missing_whitespace = settings.rules.enabled(Rule::MissingWhitespace);
|
||||||
|
let enforce_space_after_comma = settings
|
||||||
|
.rules
|
||||||
|
.any_enabled(&[Rule::MultipleSpacesAfterComma, Rule::TabAfterComma]);
|
||||||
|
let enforce_extraneous_whitespace = settings.rules.any_enabled(&[
|
||||||
|
Rule::WhitespaceAfterOpenBracket,
|
||||||
|
Rule::WhitespaceBeforeCloseBracket,
|
||||||
|
Rule::WhitespaceBeforePunctuation,
|
||||||
|
]);
|
||||||
|
let enforce_whitespace_around_keywords = settings.rules.any_enabled(&[
|
||||||
|
Rule::MultipleSpacesAfterKeyword,
|
||||||
|
Rule::MultipleSpacesBeforeKeyword,
|
||||||
|
Rule::TabAfterKeyword,
|
||||||
|
Rule::TabBeforeKeyword,
|
||||||
|
]);
|
||||||
|
let enforce_missing_whitespace_after_keyword =
|
||||||
|
settings.rules.enabled(Rule::MissingWhitespaceAfterKeyword);
|
||||||
|
let enforce_whitespace_before_comment = settings.rules.any_enabled(&[
|
||||||
|
Rule::TooFewSpacesBeforeInlineComment,
|
||||||
|
Rule::NoSpaceAfterInlineComment,
|
||||||
|
Rule::NoSpaceAfterBlockComment,
|
||||||
|
Rule::MultipleLeadingHashesForBlockComment,
|
||||||
|
]);
|
||||||
|
let enforce_whitespace_before_parameters =
|
||||||
|
settings.rules.enabled(Rule::WhitespaceBeforeParameters);
|
||||||
|
let enforce_redundant_backslash = settings.rules.enabled(Rule::RedundantBackslash);
|
||||||
|
let enforce_indentation = settings.rules.any_enabled(&[
|
||||||
|
Rule::IndentationWithInvalidMultiple,
|
||||||
|
Rule::NoIndentedBlock,
|
||||||
|
Rule::UnexpectedIndentation,
|
||||||
|
Rule::IndentationWithInvalidMultipleComment,
|
||||||
|
Rule::NoIndentedBlockComment,
|
||||||
|
Rule::UnexpectedIndentationComment,
|
||||||
|
Rule::OverIndented,
|
||||||
|
]);
|
||||||
|
|
||||||
for line in &LogicalLines::from_tokens(tokens, locator) {
|
for line in &LogicalLines::from_tokens(tokens, locator) {
|
||||||
if line.flags().contains(TokenFlags::OPERATOR) {
|
if line.flags().contains(TokenFlags::OPERATOR) {
|
||||||
space_around_operator(&line, &mut context);
|
if enforce_space_around_operator {
|
||||||
whitespace_around_named_parameter_equals(&line, &mut context);
|
space_around_operator(&line, &mut context);
|
||||||
missing_whitespace_around_operator(&line, &mut context);
|
}
|
||||||
missing_whitespace(&line, &mut context);
|
|
||||||
|
if enforce_whitespace_around_named_parameter_equals {
|
||||||
|
whitespace_around_named_parameter_equals(&line, &mut context);
|
||||||
|
}
|
||||||
|
|
||||||
|
if enforce_missing_whitespace_around_operator {
|
||||||
|
missing_whitespace_around_operator(&line, &mut context);
|
||||||
|
}
|
||||||
|
|
||||||
|
if enforce_missing_whitespace {
|
||||||
|
missing_whitespace(&line, &mut context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if line.flags().contains(TokenFlags::PUNCTUATION) {
|
|
||||||
|
if line.flags().contains(TokenFlags::PUNCTUATION) && enforce_space_after_comma {
|
||||||
space_after_comma(&line, &mut context);
|
space_after_comma(&line, &mut context);
|
||||||
}
|
}
|
||||||
|
|
||||||
if line
|
if line
|
||||||
.flags()
|
.flags()
|
||||||
.intersects(TokenFlags::OPERATOR | TokenFlags::BRACKET | TokenFlags::PUNCTUATION)
|
.intersects(TokenFlags::OPERATOR | TokenFlags::BRACKET | TokenFlags::PUNCTUATION)
|
||||||
|
&& enforce_extraneous_whitespace
|
||||||
{
|
{
|
||||||
extraneous_whitespace(&line, &mut context);
|
extraneous_whitespace(&line, &mut context);
|
||||||
}
|
}
|
||||||
|
|
||||||
if line.flags().contains(TokenFlags::KEYWORD) {
|
if line.flags().contains(TokenFlags::KEYWORD) {
|
||||||
whitespace_around_keywords(&line, &mut context);
|
if enforce_whitespace_around_keywords {
|
||||||
missing_whitespace_after_keyword(&line, &mut context);
|
whitespace_around_keywords(&line, &mut context);
|
||||||
|
}
|
||||||
|
|
||||||
|
if enforce_missing_whitespace_after_keyword {
|
||||||
|
missing_whitespace_after_keyword(&line, &mut context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if line.flags().contains(TokenFlags::COMMENT) {
|
if line.flags().contains(TokenFlags::COMMENT) && enforce_whitespace_before_comment {
|
||||||
whitespace_before_comment(&line, locator, &mut context);
|
whitespace_before_comment(&line, locator, &mut context);
|
||||||
}
|
}
|
||||||
|
|
||||||
if line.flags().contains(TokenFlags::BRACKET) {
|
if line.flags().contains(TokenFlags::BRACKET) {
|
||||||
whitespace_before_parameters(&line, &mut context);
|
if enforce_whitespace_before_parameters {
|
||||||
redundant_backslash(&line, locator, indexer, &mut context);
|
whitespace_before_parameters(&line, &mut context);
|
||||||
|
}
|
||||||
|
|
||||||
|
if enforce_redundant_backslash {
|
||||||
|
redundant_backslash(&line, locator, indexer, &mut context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the indentation level.
|
// Extract the indentation level.
|
||||||
|
@ -92,16 +167,18 @@ pub(crate) fn check_logical_lines(
|
||||||
|
|
||||||
let indent_size = 4;
|
let indent_size = 4;
|
||||||
|
|
||||||
for kind in indentation(
|
if enforce_indentation {
|
||||||
&line,
|
for kind in indentation(
|
||||||
prev_line.as_ref(),
|
&line,
|
||||||
indent_char,
|
prev_line.as_ref(),
|
||||||
indent_level,
|
indent_char,
|
||||||
prev_indent_level,
|
indent_level,
|
||||||
indent_size,
|
prev_indent_level,
|
||||||
) {
|
indent_size,
|
||||||
if settings.rules.enabled(kind.rule()) {
|
) {
|
||||||
context.push_diagnostic(Diagnostic::new(kind, range));
|
if settings.rules.enabled(kind.rule()) {
|
||||||
|
context.push_diagnostic(Diagnostic::new(kind, range));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -250,7 +250,7 @@ impl Violation for OverIndented {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// E111, E114, E112, E113, E115, E116, E117
|
/// E111, E112, E113, E114, E115, E116, E117
|
||||||
pub(crate) fn indentation(
|
pub(crate) fn indentation(
|
||||||
logical_line: &LogicalLine,
|
logical_line: &LogicalLine,
|
||||||
prev_logical_line: Option<&LogicalLine>,
|
prev_logical_line: Option<&LogicalLine>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue