Skip to content

Commit 01361d6

Browse files
committed
Add LinterContext::settings to avoid passing separate settings
Summary -- I noticed while reviewing #19390 that in `check_tokens` we were still passing around an extra `LinterSettings`, despite all of the same functions also receiving a `LintContext` with its own settings. This PR adds the `LintContext::settings` method and calls that instead of using the separate `LinterSettings`. Test Plan -- Existing tests
1 parent c6a1232 commit 01361d6

File tree

8 files changed

+34
-42
lines changed

8 files changed

+34
-42
lines changed

crates/ruff_linter/src/checkers/ast/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,6 +3216,11 @@ impl<'a> LintContext<'a> {
32163216
pub(crate) fn iter(&mut self) -> impl Iterator<Item = &Diagnostic> {
32173217
self.diagnostics.get_mut().iter()
32183218
}
3219+
3220+
/// The [`LinterSettings`] for the current analysis, including the enabled rules.
3221+
pub(crate) const fn settings(&self) -> &LinterSettings {
3222+
self.settings
3223+
}
32193224
}
32203225

32213226
/// An abstraction for mutating a diagnostic.

crates/ruff_linter/src/checkers/tokens.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use crate::rules::{
1616
eradicate, flake8_commas, flake8_executable, flake8_fixme, flake8_implicit_str_concat,
1717
flake8_pyi, flake8_todos, pycodestyle, pygrep_hooks, pylint, pyupgrade, ruff,
1818
};
19-
use crate::settings::LinterSettings;
2019

2120
use super::ast::LintContext;
2221

@@ -27,7 +26,6 @@ pub(crate) fn check_tokens(
2726
locator: &Locator,
2827
indexer: &Indexer,
2928
stylist: &Stylist,
30-
settings: &LinterSettings,
3129
source_type: PySourceType,
3230
cell_offsets: Option<&CellOffsets>,
3331
context: &mut LintContext,
@@ -42,15 +40,8 @@ pub(crate) fn check_tokens(
4240
Rule::BlankLinesAfterFunctionOrClass,
4341
Rule::BlankLinesBeforeNestedDefinition,
4442
]) {
45-
BlankLinesChecker::new(
46-
locator,
47-
stylist,
48-
settings,
49-
source_type,
50-
cell_offsets,
51-
context,
52-
)
53-
.check_lines(tokens);
43+
BlankLinesChecker::new(locator, stylist, source_type, cell_offsets, context)
44+
.check_lines(tokens);
5445
}
5546

5647
if context.is_rule_enabled(Rule::BlanketTypeIgnore) {
@@ -63,12 +54,12 @@ pub(crate) fn check_tokens(
6354

6455
if context.is_rule_enabled(Rule::AmbiguousUnicodeCharacterComment) {
6556
for range in comment_ranges {
66-
ruff::rules::ambiguous_unicode_character_comment(context, locator, range, settings);
57+
ruff::rules::ambiguous_unicode_character_comment(context, locator, range);
6758
}
6859
}
6960

7061
if context.is_rule_enabled(Rule::CommentedOutCode) {
71-
eradicate::rules::commented_out_code(context, locator, comment_ranges, settings);
62+
eradicate::rules::commented_out_code(context, locator, comment_ranges);
7263
}
7364

7465
if context.is_rule_enabled(Rule::UTF8EncodingDeclaration) {
@@ -110,15 +101,15 @@ pub(crate) fn check_tokens(
110101
Rule::SingleLineImplicitStringConcatenation,
111102
Rule::MultiLineImplicitStringConcatenation,
112103
]) {
113-
flake8_implicit_str_concat::rules::implicit(context, tokens, locator, indexer, settings);
104+
flake8_implicit_str_concat::rules::implicit(context, tokens, locator, indexer);
114105
}
115106

116107
if context.any_rule_enabled(&[
117108
Rule::MissingTrailingComma,
118109
Rule::TrailingCommaOnBareTuple,
119110
Rule::ProhibitedTrailingComma,
120111
]) {
121-
flake8_commas::rules::trailing_commas(context, tokens, locator, indexer, settings);
112+
flake8_commas::rules::trailing_commas(context, tokens, locator, indexer);
122113
}
123114

124115
if context.is_rule_enabled(Rule::ExtraneousParentheses) {

crates/ruff_linter/src/linter.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ pub fn check_path(
188188
locator,
189189
indexer,
190190
stylist,
191-
settings,
192191
source_type,
193192
source_kind.as_ipy_notebook().map(Notebook::cell_offsets),
194193
&mut context,

crates/ruff_linter/src/rules/eradicate/rules/commented_out_code.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use ruff_text_size::TextRange;
55

66
use crate::Locator;
77
use crate::checkers::ast::LintContext;
8-
use crate::settings::LinterSettings;
98
use crate::{Edit, Fix, FixAvailability, Violation};
109

1110
use crate::rules::eradicate::detection::comment_contains_code;
@@ -51,7 +50,6 @@ pub(crate) fn commented_out_code(
5150
context: &LintContext,
5251
locator: &Locator,
5352
comment_ranges: &CommentRanges,
54-
settings: &LinterSettings,
5553
) {
5654
let mut comments = comment_ranges.into_iter().peekable();
5755
// Iterate over all comments in the document.
@@ -65,7 +63,9 @@ pub(crate) fn commented_out_code(
6563
}
6664

6765
// Verify that the comment is on its own line, and that it contains code.
68-
if is_own_line_comment(line) && comment_contains_code(line, &settings.task_tags[..]) {
66+
if is_own_line_comment(line)
67+
&& comment_contains_code(line, &context.settings().task_tags[..])
68+
{
6969
if let Some(mut diagnostic) =
7070
context.report_diagnostic_if_enabled(CommentedOutCode, range)
7171
{

crates/ruff_linter/src/rules/flake8_commas/rules/trailing_commas.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ pub(crate) fn trailing_commas(
251251
tokens: &Tokens,
252252
locator: &Locator,
253253
indexer: &Indexer,
254-
settings: &LinterSettings,
255254
) {
256255
let mut fstrings = 0u32;
257256
let simple_tokens = tokens.iter().filter_map(|token| {
@@ -299,7 +298,7 @@ pub(crate) fn trailing_commas(
299298
}
300299

301300
// Update the comma context stack.
302-
let context = update_context(token, prev, prev_prev, &mut stack, settings);
301+
let context = update_context(token, prev, prev_prev, &mut stack, lint_context.settings());
303302

304303
check_token(token, prev, prev_prev, context, locator, lint_context);
305304

crates/ruff_linter/src/rules/flake8_implicit_str_concat/rules/implicit.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use ruff_text_size::{Ranged, TextRange};
1111

1212
use crate::Locator;
1313
use crate::checkers::ast::LintContext;
14-
use crate::settings::LinterSettings;
1514
use crate::{Edit, Fix, FixAvailability, Violation};
1615

1716
/// ## What it does
@@ -108,13 +107,15 @@ pub(crate) fn implicit(
108107
tokens: &Tokens,
109108
locator: &Locator,
110109
indexer: &Indexer,
111-
settings: &LinterSettings,
112110
) {
113111
for (a_token, b_token) in tokens
114112
.iter()
115113
.filter(|token| {
116114
token.kind() != TokenKind::Comment
117-
&& (settings.flake8_implicit_str_concat.allow_multiline
115+
&& (context
116+
.settings()
117+
.flake8_implicit_str_concat
118+
.allow_multiline
118119
|| token.kind() != TokenKind::NonLogicalNewline)
119120
})
120121
.tuple_windows()

crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::checkers::ast::{DiagnosticGuard, LintContext};
2121
use crate::checkers::logical_lines::expand_indent;
2222
use crate::line_width::IndentWidth;
2323
use crate::rules::pycodestyle::helpers::is_non_logical_token;
24-
use crate::settings::LinterSettings;
2524
use crate::{AlwaysFixableViolation, Edit, Fix, Locator, Violation};
2625

2726
/// Number of blank lines around top level classes and functions.
@@ -694,14 +693,12 @@ pub(crate) struct BlankLinesChecker<'a, 'b> {
694693
source_type: PySourceType,
695694
cell_offsets: Option<&'a CellOffsets>,
696695
context: &'a LintContext<'b>,
697-
settings: &'a LinterSettings,
698696
}
699697

700698
impl<'a, 'b> BlankLinesChecker<'a, 'b> {
701699
pub(crate) fn new(
702700
locator: &'a Locator<'a>,
703701
stylist: &'a Stylist<'a>,
704-
settings: &'a LinterSettings,
705702
source_type: PySourceType,
706703
cell_offsets: Option<&'a CellOffsets>,
707704
context: &'a LintContext<'b>,
@@ -712,7 +709,6 @@ impl<'a, 'b> BlankLinesChecker<'a, 'b> {
712709
source_type,
713710
cell_offsets,
714711
context,
715-
settings,
716712
}
717713
}
718714

@@ -733,7 +729,7 @@ impl<'a, 'b> BlankLinesChecker<'a, 'b> {
733729
let line_preprocessor = LinePreprocessor::new(
734730
tokens,
735731
self.locator,
736-
self.settings.tab_size,
732+
self.context.settings().tab_size,
737733
self.cell_offsets,
738734
);
739735

@@ -879,7 +875,8 @@ impl<'a, 'b> BlankLinesChecker<'a, 'b> {
879875
// `isort` defaults to 2 if before a class or function definition (except in stubs where it is one) and 1 otherwise.
880876
// Defaulting to 2 (or 1 in stubs) here is correct because the variable is only used when testing the
881877
// blank lines before a class or function definition.
882-
u32::try_from(self.settings.isort.lines_after_imports).unwrap_or(max_lines_level)
878+
u32::try_from(self.context.settings().isort.lines_after_imports)
879+
.unwrap_or(max_lines_level)
883880
} else {
884881
max_lines_level
885882
}
@@ -941,8 +938,10 @@ impl<'a, 'b> BlankLinesChecker<'a, 'b> {
941938
(LogicalLineKind::Import, Follows::FromImport)
942939
| (LogicalLineKind::FromImport, Follows::Import)
943940
) {
944-
max_lines_level
945-
.max(u32::try_from(self.settings.isort.lines_between_types).unwrap_or(u32::MAX))
941+
max_lines_level.max(
942+
u32::try_from(self.context.settings().isort.lines_between_types)
943+
.unwrap_or(u32::MAX),
944+
)
946945
} else {
947946
expected_blank_lines_before_definition
948947
};

crates/ruff_linter/src/rules/ruff/rules/ambiguous_unicode_character.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,10 @@ pub(crate) fn ambiguous_unicode_character_comment(
178178
context: &LintContext,
179179
locator: &Locator,
180180
range: TextRange,
181-
settings: &LinterSettings,
182181
) {
183182
let text = locator.slice(range);
184-
for candidate in ambiguous_unicode_character(text, range, settings) {
185-
candidate.into_diagnostic(Context::Comment, settings, context);
183+
for candidate in ambiguous_unicode_character(text, range, context.settings()) {
184+
candidate.into_diagnostic(Context::Comment, context);
186185
}
187186
}
188187

@@ -342,13 +341,12 @@ impl Candidate {
342341
}
343342
}
344343

345-
fn into_diagnostic(
346-
self,
347-
context: Context,
348-
settings: &LinterSettings,
349-
lint_context: &LintContext,
350-
) {
351-
if !settings.allowed_confusables.contains(&self.confusable) {
344+
fn into_diagnostic(self, context: Context, lint_context: &LintContext) {
345+
if !lint_context
346+
.settings()
347+
.allowed_confusables
348+
.contains(&self.confusable)
349+
{
352350
let char_range = TextRange::at(self.offset, self.confusable.text_len());
353351
match context {
354352
Context::String => lint_context.report_diagnostic_if_enabled(

0 commit comments

Comments
 (0)