mirror of https://github.com/astral-sh/ruff
Add `LinterContext::settings` to avoid passing separate settings (#19608)
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
This commit is contained in:
parent
e0f4f25d28
commit
19569bf838
|
|
@ -3216,6 +3216,11 @@ impl<'a> LintContext<'a> {
|
||||||
pub(crate) fn iter(&mut self) -> impl Iterator<Item = &Diagnostic> {
|
pub(crate) fn iter(&mut self) -> impl Iterator<Item = &Diagnostic> {
|
||||||
self.diagnostics.get_mut().iter()
|
self.diagnostics.get_mut().iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The [`LinterSettings`] for the current analysis, including the enabled rules.
|
||||||
|
pub(crate) const fn settings(&self) -> &LinterSettings {
|
||||||
|
self.settings
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An abstraction for mutating a diagnostic.
|
/// An abstraction for mutating a diagnostic.
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ use crate::rules::{
|
||||||
eradicate, flake8_commas, flake8_executable, flake8_fixme, flake8_implicit_str_concat,
|
eradicate, flake8_commas, flake8_executable, flake8_fixme, flake8_implicit_str_concat,
|
||||||
flake8_pyi, flake8_todos, pycodestyle, pygrep_hooks, pylint, pyupgrade, ruff,
|
flake8_pyi, flake8_todos, pycodestyle, pygrep_hooks, pylint, pyupgrade, ruff,
|
||||||
};
|
};
|
||||||
use crate::settings::LinterSettings;
|
|
||||||
|
|
||||||
use super::ast::LintContext;
|
use super::ast::LintContext;
|
||||||
|
|
||||||
|
|
@ -27,7 +26,6 @@ pub(crate) fn check_tokens(
|
||||||
locator: &Locator,
|
locator: &Locator,
|
||||||
indexer: &Indexer,
|
indexer: &Indexer,
|
||||||
stylist: &Stylist,
|
stylist: &Stylist,
|
||||||
settings: &LinterSettings,
|
|
||||||
source_type: PySourceType,
|
source_type: PySourceType,
|
||||||
cell_offsets: Option<&CellOffsets>,
|
cell_offsets: Option<&CellOffsets>,
|
||||||
context: &mut LintContext,
|
context: &mut LintContext,
|
||||||
|
|
@ -42,15 +40,8 @@ pub(crate) fn check_tokens(
|
||||||
Rule::BlankLinesAfterFunctionOrClass,
|
Rule::BlankLinesAfterFunctionOrClass,
|
||||||
Rule::BlankLinesBeforeNestedDefinition,
|
Rule::BlankLinesBeforeNestedDefinition,
|
||||||
]) {
|
]) {
|
||||||
BlankLinesChecker::new(
|
BlankLinesChecker::new(locator, stylist, source_type, cell_offsets, context)
|
||||||
locator,
|
.check_lines(tokens);
|
||||||
stylist,
|
|
||||||
settings,
|
|
||||||
source_type,
|
|
||||||
cell_offsets,
|
|
||||||
context,
|
|
||||||
)
|
|
||||||
.check_lines(tokens);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if context.is_rule_enabled(Rule::BlanketTypeIgnore) {
|
if context.is_rule_enabled(Rule::BlanketTypeIgnore) {
|
||||||
|
|
@ -63,12 +54,12 @@ pub(crate) fn check_tokens(
|
||||||
|
|
||||||
if context.is_rule_enabled(Rule::AmbiguousUnicodeCharacterComment) {
|
if context.is_rule_enabled(Rule::AmbiguousUnicodeCharacterComment) {
|
||||||
for range in comment_ranges {
|
for range in comment_ranges {
|
||||||
ruff::rules::ambiguous_unicode_character_comment(context, locator, range, settings);
|
ruff::rules::ambiguous_unicode_character_comment(context, locator, range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if context.is_rule_enabled(Rule::CommentedOutCode) {
|
if context.is_rule_enabled(Rule::CommentedOutCode) {
|
||||||
eradicate::rules::commented_out_code(context, locator, comment_ranges, settings);
|
eradicate::rules::commented_out_code(context, locator, comment_ranges);
|
||||||
}
|
}
|
||||||
|
|
||||||
if context.is_rule_enabled(Rule::UTF8EncodingDeclaration) {
|
if context.is_rule_enabled(Rule::UTF8EncodingDeclaration) {
|
||||||
|
|
@ -110,7 +101,7 @@ pub(crate) fn check_tokens(
|
||||||
Rule::SingleLineImplicitStringConcatenation,
|
Rule::SingleLineImplicitStringConcatenation,
|
||||||
Rule::MultiLineImplicitStringConcatenation,
|
Rule::MultiLineImplicitStringConcatenation,
|
||||||
]) {
|
]) {
|
||||||
flake8_implicit_str_concat::rules::implicit(context, tokens, locator, indexer, settings);
|
flake8_implicit_str_concat::rules::implicit(context, tokens, locator, indexer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if context.any_rule_enabled(&[
|
if context.any_rule_enabled(&[
|
||||||
|
|
@ -118,7 +109,7 @@ pub(crate) fn check_tokens(
|
||||||
Rule::TrailingCommaOnBareTuple,
|
Rule::TrailingCommaOnBareTuple,
|
||||||
Rule::ProhibitedTrailingComma,
|
Rule::ProhibitedTrailingComma,
|
||||||
]) {
|
]) {
|
||||||
flake8_commas::rules::trailing_commas(context, tokens, locator, indexer, settings);
|
flake8_commas::rules::trailing_commas(context, tokens, locator, indexer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if context.is_rule_enabled(Rule::ExtraneousParentheses) {
|
if context.is_rule_enabled(Rule::ExtraneousParentheses) {
|
||||||
|
|
|
||||||
|
|
@ -188,7 +188,6 @@ pub fn check_path(
|
||||||
locator,
|
locator,
|
||||||
indexer,
|
indexer,
|
||||||
stylist,
|
stylist,
|
||||||
settings,
|
|
||||||
source_type,
|
source_type,
|
||||||
source_kind.as_ipy_notebook().map(Notebook::cell_offsets),
|
source_kind.as_ipy_notebook().map(Notebook::cell_offsets),
|
||||||
&mut context,
|
&mut context,
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ use ruff_text_size::TextRange;
|
||||||
|
|
||||||
use crate::Locator;
|
use crate::Locator;
|
||||||
use crate::checkers::ast::LintContext;
|
use crate::checkers::ast::LintContext;
|
||||||
use crate::settings::LinterSettings;
|
|
||||||
use crate::{Edit, Fix, FixAvailability, Violation};
|
use crate::{Edit, Fix, FixAvailability, Violation};
|
||||||
|
|
||||||
use crate::rules::eradicate::detection::comment_contains_code;
|
use crate::rules::eradicate::detection::comment_contains_code;
|
||||||
|
|
@ -51,7 +50,6 @@ pub(crate) fn commented_out_code(
|
||||||
context: &LintContext,
|
context: &LintContext,
|
||||||
locator: &Locator,
|
locator: &Locator,
|
||||||
comment_ranges: &CommentRanges,
|
comment_ranges: &CommentRanges,
|
||||||
settings: &LinterSettings,
|
|
||||||
) {
|
) {
|
||||||
let mut comments = comment_ranges.into_iter().peekable();
|
let mut comments = comment_ranges.into_iter().peekable();
|
||||||
// Iterate over all comments in the document.
|
// Iterate over all comments in the document.
|
||||||
|
|
@ -65,7 +63,9 @@ pub(crate) fn commented_out_code(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify that the comment is on its own line, and that it contains code.
|
// Verify that the comment is on its own line, and that it contains code.
|
||||||
if is_own_line_comment(line) && comment_contains_code(line, &settings.task_tags[..]) {
|
if is_own_line_comment(line)
|
||||||
|
&& comment_contains_code(line, &context.settings().task_tags[..])
|
||||||
|
{
|
||||||
if let Some(mut diagnostic) =
|
if let Some(mut diagnostic) =
|
||||||
context.report_diagnostic_if_enabled(CommentedOutCode, range)
|
context.report_diagnostic_if_enabled(CommentedOutCode, range)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -251,7 +251,6 @@ pub(crate) fn trailing_commas(
|
||||||
tokens: &Tokens,
|
tokens: &Tokens,
|
||||||
locator: &Locator,
|
locator: &Locator,
|
||||||
indexer: &Indexer,
|
indexer: &Indexer,
|
||||||
settings: &LinterSettings,
|
|
||||||
) {
|
) {
|
||||||
let mut fstrings = 0u32;
|
let mut fstrings = 0u32;
|
||||||
let simple_tokens = tokens.iter().filter_map(|token| {
|
let simple_tokens = tokens.iter().filter_map(|token| {
|
||||||
|
|
@ -299,7 +298,7 @@ pub(crate) fn trailing_commas(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the comma context stack.
|
// Update the comma context stack.
|
||||||
let context = update_context(token, prev, prev_prev, &mut stack, settings);
|
let context = update_context(token, prev, prev_prev, &mut stack, lint_context.settings());
|
||||||
|
|
||||||
check_token(token, prev, prev_prev, context, locator, lint_context);
|
check_token(token, prev, prev_prev, context, locator, lint_context);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ use ruff_text_size::{Ranged, TextRange};
|
||||||
|
|
||||||
use crate::Locator;
|
use crate::Locator;
|
||||||
use crate::checkers::ast::LintContext;
|
use crate::checkers::ast::LintContext;
|
||||||
use crate::settings::LinterSettings;
|
|
||||||
use crate::{Edit, Fix, FixAvailability, Violation};
|
use crate::{Edit, Fix, FixAvailability, Violation};
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
|
|
@ -108,13 +107,15 @@ pub(crate) fn implicit(
|
||||||
tokens: &Tokens,
|
tokens: &Tokens,
|
||||||
locator: &Locator,
|
locator: &Locator,
|
||||||
indexer: &Indexer,
|
indexer: &Indexer,
|
||||||
settings: &LinterSettings,
|
|
||||||
) {
|
) {
|
||||||
for (a_token, b_token) in tokens
|
for (a_token, b_token) in tokens
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|token| {
|
.filter(|token| {
|
||||||
token.kind() != TokenKind::Comment
|
token.kind() != TokenKind::Comment
|
||||||
&& (settings.flake8_implicit_str_concat.allow_multiline
|
&& (context
|
||||||
|
.settings()
|
||||||
|
.flake8_implicit_str_concat
|
||||||
|
.allow_multiline
|
||||||
|| token.kind() != TokenKind::NonLogicalNewline)
|
|| token.kind() != TokenKind::NonLogicalNewline)
|
||||||
})
|
})
|
||||||
.tuple_windows()
|
.tuple_windows()
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ use crate::checkers::ast::{DiagnosticGuard, LintContext};
|
||||||
use crate::checkers::logical_lines::expand_indent;
|
use crate::checkers::logical_lines::expand_indent;
|
||||||
use crate::line_width::IndentWidth;
|
use crate::line_width::IndentWidth;
|
||||||
use crate::rules::pycodestyle::helpers::is_non_logical_token;
|
use crate::rules::pycodestyle::helpers::is_non_logical_token;
|
||||||
use crate::settings::LinterSettings;
|
|
||||||
use crate::{AlwaysFixableViolation, Edit, Fix, Locator, Violation};
|
use crate::{AlwaysFixableViolation, Edit, Fix, Locator, Violation};
|
||||||
|
|
||||||
/// Number of blank lines around top level classes and functions.
|
/// Number of blank lines around top level classes and functions.
|
||||||
|
|
@ -694,14 +693,12 @@ pub(crate) struct BlankLinesChecker<'a, 'b> {
|
||||||
source_type: PySourceType,
|
source_type: PySourceType,
|
||||||
cell_offsets: Option<&'a CellOffsets>,
|
cell_offsets: Option<&'a CellOffsets>,
|
||||||
context: &'a LintContext<'b>,
|
context: &'a LintContext<'b>,
|
||||||
settings: &'a LinterSettings,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> BlankLinesChecker<'a, 'b> {
|
impl<'a, 'b> BlankLinesChecker<'a, 'b> {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
locator: &'a Locator<'a>,
|
locator: &'a Locator<'a>,
|
||||||
stylist: &'a Stylist<'a>,
|
stylist: &'a Stylist<'a>,
|
||||||
settings: &'a LinterSettings,
|
|
||||||
source_type: PySourceType,
|
source_type: PySourceType,
|
||||||
cell_offsets: Option<&'a CellOffsets>,
|
cell_offsets: Option<&'a CellOffsets>,
|
||||||
context: &'a LintContext<'b>,
|
context: &'a LintContext<'b>,
|
||||||
|
|
@ -712,7 +709,6 @@ impl<'a, 'b> BlankLinesChecker<'a, 'b> {
|
||||||
source_type,
|
source_type,
|
||||||
cell_offsets,
|
cell_offsets,
|
||||||
context,
|
context,
|
||||||
settings,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -733,7 +729,7 @@ impl<'a, 'b> BlankLinesChecker<'a, 'b> {
|
||||||
let line_preprocessor = LinePreprocessor::new(
|
let line_preprocessor = LinePreprocessor::new(
|
||||||
tokens,
|
tokens,
|
||||||
self.locator,
|
self.locator,
|
||||||
self.settings.tab_size,
|
self.context.settings().tab_size,
|
||||||
self.cell_offsets,
|
self.cell_offsets,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -879,7 +875,8 @@ impl<'a, 'b> BlankLinesChecker<'a, 'b> {
|
||||||
// `isort` defaults to 2 if before a class or function definition (except in stubs where it is one) and 1 otherwise.
|
// `isort` defaults to 2 if before a class or function definition (except in stubs where it is one) and 1 otherwise.
|
||||||
// Defaulting to 2 (or 1 in stubs) here is correct because the variable is only used when testing the
|
// Defaulting to 2 (or 1 in stubs) here is correct because the variable is only used when testing the
|
||||||
// blank lines before a class or function definition.
|
// blank lines before a class or function definition.
|
||||||
u32::try_from(self.settings.isort.lines_after_imports).unwrap_or(max_lines_level)
|
u32::try_from(self.context.settings().isort.lines_after_imports)
|
||||||
|
.unwrap_or(max_lines_level)
|
||||||
} else {
|
} else {
|
||||||
max_lines_level
|
max_lines_level
|
||||||
}
|
}
|
||||||
|
|
@ -941,8 +938,10 @@ impl<'a, 'b> BlankLinesChecker<'a, 'b> {
|
||||||
(LogicalLineKind::Import, Follows::FromImport)
|
(LogicalLineKind::Import, Follows::FromImport)
|
||||||
| (LogicalLineKind::FromImport, Follows::Import)
|
| (LogicalLineKind::FromImport, Follows::Import)
|
||||||
) {
|
) {
|
||||||
max_lines_level
|
max_lines_level.max(
|
||||||
.max(u32::try_from(self.settings.isort.lines_between_types).unwrap_or(u32::MAX))
|
u32::try_from(self.context.settings().isort.lines_between_types)
|
||||||
|
.unwrap_or(u32::MAX),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
expected_blank_lines_before_definition
|
expected_blank_lines_before_definition
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -178,11 +178,10 @@ pub(crate) fn ambiguous_unicode_character_comment(
|
||||||
context: &LintContext,
|
context: &LintContext,
|
||||||
locator: &Locator,
|
locator: &Locator,
|
||||||
range: TextRange,
|
range: TextRange,
|
||||||
settings: &LinterSettings,
|
|
||||||
) {
|
) {
|
||||||
let text = locator.slice(range);
|
let text = locator.slice(range);
|
||||||
for candidate in ambiguous_unicode_character(text, range, settings) {
|
for candidate in ambiguous_unicode_character(text, range, context.settings()) {
|
||||||
candidate.into_diagnostic(Context::Comment, settings, context);
|
candidate.into_diagnostic(Context::Comment, context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -342,13 +341,12 @@ impl Candidate {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn into_diagnostic(
|
fn into_diagnostic(self, context: Context, lint_context: &LintContext) {
|
||||||
self,
|
if !lint_context
|
||||||
context: Context,
|
.settings()
|
||||||
settings: &LinterSettings,
|
.allowed_confusables
|
||||||
lint_context: &LintContext,
|
.contains(&self.confusable)
|
||||||
) {
|
{
|
||||||
if !settings.allowed_confusables.contains(&self.confusable) {
|
|
||||||
let char_range = TextRange::at(self.offset, self.confusable.text_len());
|
let char_range = TextRange::at(self.offset, self.confusable.text_len());
|
||||||
match context {
|
match context {
|
||||||
Context::String => lint_context.report_diagnostic_if_enabled(
|
Context::String => lint_context.report_diagnostic_if_enabled(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue