Skip to content

Commit 3f884b4

Browse files
authored
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`
1 parent b456051 commit 3f884b4

File tree

2 files changed

+99
-22
lines changed

2 files changed

+99
-22
lines changed

Diff for: crates/ruff_linter/src/checkers/logical_lines.rs

+98-21
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ruff_python_parser::{TokenKind, Tokens};
66
use ruff_source_file::Locator;
77
use ruff_text_size::{Ranged, TextRange};
88

9-
use crate::registry::AsRule;
9+
use crate::registry::{AsRule, Rule};
1010
use crate::rules::pycodestyle::rules::logical_lines::{
1111
extraneous_whitespace, indentation, missing_whitespace, missing_whitespace_after_keyword,
1212
missing_whitespace_around_operator, redundant_backslash, space_after_comma,
@@ -45,36 +45,111 @@ pub(crate) fn check_logical_lines(
4545
let mut prev_indent_level = None;
4646
let indent_char = stylist.indentation().as_char();
4747

48+
let enforce_space_around_operator = settings.rules.any_enabled(&[
49+
Rule::MultipleSpacesBeforeOperator,
50+
Rule::MultipleSpacesAfterOperator,
51+
Rule::TabBeforeOperator,
52+
Rule::TabAfterOperator,
53+
]);
54+
let enforce_whitespace_around_named_parameter_equals = settings.rules.any_enabled(&[
55+
Rule::UnexpectedSpacesAroundKeywordParameterEquals,
56+
Rule::MissingWhitespaceAroundParameterEquals,
57+
]);
58+
let enforce_missing_whitespace_around_operator = settings.rules.any_enabled(&[
59+
Rule::MissingWhitespaceAroundOperator,
60+
Rule::MissingWhitespaceAroundArithmeticOperator,
61+
Rule::MissingWhitespaceAroundBitwiseOrShiftOperator,
62+
Rule::MissingWhitespaceAroundModuloOperator,
63+
]);
64+
let enforce_missing_whitespace = settings.rules.enabled(Rule::MissingWhitespace);
65+
let enforce_space_after_comma = settings
66+
.rules
67+
.any_enabled(&[Rule::MultipleSpacesAfterComma, Rule::TabAfterComma]);
68+
let enforce_extraneous_whitespace = settings.rules.any_enabled(&[
69+
Rule::WhitespaceAfterOpenBracket,
70+
Rule::WhitespaceBeforeCloseBracket,
71+
Rule::WhitespaceBeforePunctuation,
72+
]);
73+
let enforce_whitespace_around_keywords = settings.rules.any_enabled(&[
74+
Rule::MultipleSpacesAfterKeyword,
75+
Rule::MultipleSpacesBeforeKeyword,
76+
Rule::TabAfterKeyword,
77+
Rule::TabBeforeKeyword,
78+
]);
79+
let enforce_missing_whitespace_after_keyword =
80+
settings.rules.enabled(Rule::MissingWhitespaceAfterKeyword);
81+
let enforce_whitespace_before_comment = settings.rules.any_enabled(&[
82+
Rule::TooFewSpacesBeforeInlineComment,
83+
Rule::NoSpaceAfterInlineComment,
84+
Rule::NoSpaceAfterBlockComment,
85+
Rule::MultipleLeadingHashesForBlockComment,
86+
]);
87+
let enforce_whitespace_before_parameters =
88+
settings.rules.enabled(Rule::WhitespaceBeforeParameters);
89+
let enforce_redundant_backslash = settings.rules.enabled(Rule::RedundantBackslash);
90+
let enforce_indentation = settings.rules.any_enabled(&[
91+
Rule::IndentationWithInvalidMultiple,
92+
Rule::NoIndentedBlock,
93+
Rule::UnexpectedIndentation,
94+
Rule::IndentationWithInvalidMultipleComment,
95+
Rule::NoIndentedBlockComment,
96+
Rule::UnexpectedIndentationComment,
97+
Rule::OverIndented,
98+
]);
99+
48100
for line in &LogicalLines::from_tokens(tokens, locator) {
49101
if line.flags().contains(TokenFlags::OPERATOR) {
50-
space_around_operator(&line, &mut context);
51-
whitespace_around_named_parameter_equals(&line, &mut context);
52-
missing_whitespace_around_operator(&line, &mut context);
53-
missing_whitespace(&line, &mut context);
102+
if enforce_space_around_operator {
103+
space_around_operator(&line, &mut context);
104+
}
105+
106+
if enforce_whitespace_around_named_parameter_equals {
107+
whitespace_around_named_parameter_equals(&line, &mut context);
108+
}
109+
110+
if enforce_missing_whitespace_around_operator {
111+
missing_whitespace_around_operator(&line, &mut context);
112+
}
113+
114+
if enforce_missing_whitespace {
115+
missing_whitespace(&line, &mut context);
116+
}
54117
}
55-
if line.flags().contains(TokenFlags::PUNCTUATION) {
118+
119+
if line.flags().contains(TokenFlags::PUNCTUATION) && enforce_space_after_comma {
56120
space_after_comma(&line, &mut context);
57121
}
58122

59123
if line
60124
.flags()
61125
.intersects(TokenFlags::OPERATOR | TokenFlags::BRACKET | TokenFlags::PUNCTUATION)
126+
&& enforce_extraneous_whitespace
62127
{
63128
extraneous_whitespace(&line, &mut context);
64129
}
65130

66131
if line.flags().contains(TokenFlags::KEYWORD) {
67-
whitespace_around_keywords(&line, &mut context);
68-
missing_whitespace_after_keyword(&line, &mut context);
132+
if enforce_whitespace_around_keywords {
133+
whitespace_around_keywords(&line, &mut context);
134+
}
135+
136+
if enforce_missing_whitespace_after_keyword {
137+
missing_whitespace_after_keyword(&line, &mut context);
138+
}
69139
}
70140

71-
if line.flags().contains(TokenFlags::COMMENT) {
141+
if line.flags().contains(TokenFlags::COMMENT) && enforce_whitespace_before_comment {
72142
whitespace_before_comment(&line, locator, &mut context);
73143
}
74144

75145
if line.flags().contains(TokenFlags::BRACKET) {
76-
whitespace_before_parameters(&line, &mut context);
77-
redundant_backslash(&line, locator, indexer, &mut context);
146+
if enforce_whitespace_before_parameters {
147+
whitespace_before_parameters(&line, &mut context);
148+
}
149+
150+
if enforce_redundant_backslash {
151+
redundant_backslash(&line, locator, indexer, &mut context);
152+
}
78153
}
79154

80155
// Extract the indentation level.
@@ -92,16 +167,18 @@ pub(crate) fn check_logical_lines(
92167

93168
let indent_size = 4;
94169

95-
for kind in indentation(
96-
&line,
97-
prev_line.as_ref(),
98-
indent_char,
99-
indent_level,
100-
prev_indent_level,
101-
indent_size,
102-
) {
103-
if settings.rules.enabled(kind.rule()) {
104-
context.push_diagnostic(Diagnostic::new(kind, range));
170+
if enforce_indentation {
171+
for kind in indentation(
172+
&line,
173+
prev_line.as_ref(),
174+
indent_char,
175+
indent_level,
176+
prev_indent_level,
177+
indent_size,
178+
) {
179+
if settings.rules.enabled(kind.rule()) {
180+
context.push_diagnostic(Diagnostic::new(kind, range));
181+
}
105182
}
106183
}
107184

Diff for: crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/indentation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl Violation for OverIndented {
250250
}
251251
}
252252

253-
/// E111, E114, E112, E113, E115, E116, E117
253+
/// E111, E112, E113, E114, E115, E116, E117
254254
pub(crate) fn indentation(
255255
logical_line: &LogicalLine,
256256
prev_logical_line: Option<&LogicalLine>,

0 commit comments

Comments
 (0)