Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ impl<'a> Checker<'a> {
kind: T,
range: TextRange,
) -> Option<DiagnosticGuard<'chk, 'a>> {
self.context
.report_diagnostic_if_enabled(kind, range, self.settings)
self.context.report_diagnostic_if_enabled(kind, range)
}

/// Adds a [`TextRange`] to the set of ranges of variable names
Expand Down Expand Up @@ -3109,15 +3108,17 @@ pub(crate) fn check_ast(
pub(crate) struct LintContext<'a> {
diagnostics: RefCell<Vec<OldDiagnostic>>,
source_file: &'a SourceFile,
settings: &'a LinterSettings,
}

impl<'a> LintContext<'a> {
/// Create a new collector with the given `source_file` and an empty collection of
/// `OldDiagnostic`s.
pub(crate) fn new(source_file: &'a SourceFile) -> Self {
pub(crate) fn new(source_file: &'a SourceFile, settings: &'a LinterSettings) -> Self {
Self {
diagnostics: RefCell::default(),
source_file,
settings,
}
}

Expand Down Expand Up @@ -3145,10 +3146,9 @@ impl<'a> LintContext<'a> {
&'chk self,
kind: T,
range: TextRange,
settings: &LinterSettings,
) -> Option<DiagnosticGuard<'chk, 'a>> {
let diagnostic = OldDiagnostic::new(kind, range, self.source_file);
if settings.rules.enabled(diagnostic.rule()) {
if self.settings.rules.enabled(diagnostic.rule()) {
Some(DiagnosticGuard {
context: self,
diagnostic: Some(diagnostic),
Expand Down
53 changes: 15 additions & 38 deletions crates/ruff_linter/src/checkers/logical_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ruff_python_parser::{TokenKind, Tokens};
use ruff_source_file::LineRanges;
use ruff_text_size::{Ranged, TextRange};

use crate::Locator;
use crate::line_width::IndentWidth;
use crate::registry::Rule;
use crate::rules::pycodestyle::rules::logical_lines::{
Expand All @@ -14,9 +15,8 @@ use crate::rules::pycodestyle::rules::logical_lines::{
whitespace_before_parameters,
};
use crate::settings::LinterSettings;
use crate::{Locator, Violation};

use super::ast::{DiagnosticGuard, LintContext};
use super::ast::LintContext;

/// Return the amount of indentation, expanding tabs to the next multiple of the settings' tab size.
pub(crate) fn expand_indent(line: &str, indent_width: IndentWidth) -> usize {
Expand All @@ -41,10 +41,8 @@ pub(crate) fn check_logical_lines(
indexer: &Indexer,
stylist: &Stylist,
settings: &LinterSettings,
lint_context: &LintContext,
context: &LintContext,
) {
let mut context = LogicalLinesContext::new(settings, lint_context);

let mut prev_line = None;
let mut prev_indent_level = None;
let indent_char = stylist.indentation().as_char();
Expand Down Expand Up @@ -104,55 +102,55 @@ pub(crate) fn check_logical_lines(
for line in &LogicalLines::from_tokens(tokens, locator) {
if line.flags().contains(TokenFlags::OPERATOR) {
if enforce_space_around_operator {
space_around_operator(&line, &mut context);
space_around_operator(&line, context);
}

if enforce_whitespace_around_named_parameter_equals {
whitespace_around_named_parameter_equals(&line, &mut context);
whitespace_around_named_parameter_equals(&line, context);
}

if enforce_missing_whitespace_around_operator {
missing_whitespace_around_operator(&line, &mut context);
missing_whitespace_around_operator(&line, context);
}

if enforce_missing_whitespace {
missing_whitespace(&line, &mut context);
missing_whitespace(&line, context);
}
}

if line.flags().contains(TokenFlags::PUNCTUATION) && enforce_space_after_comma {
space_after_comma(&line, &mut context);
space_after_comma(&line, context);
}

if line
.flags()
.intersects(TokenFlags::OPERATOR | TokenFlags::BRACKET | TokenFlags::PUNCTUATION)
&& enforce_extraneous_whitespace
{
extraneous_whitespace(&line, &mut context);
extraneous_whitespace(&line, context);
}

if line.flags().contains(TokenFlags::KEYWORD) {
if enforce_whitespace_around_keywords {
whitespace_around_keywords(&line, &mut context);
whitespace_around_keywords(&line, context);
}

if enforce_missing_whitespace_after_keyword {
missing_whitespace_after_keyword(&line, &mut context);
missing_whitespace_after_keyword(&line, context);
}
}

if line.flags().contains(TokenFlags::COMMENT) && enforce_whitespace_before_comment {
whitespace_before_comment(&line, locator, &mut context);
whitespace_before_comment(&line, locator, context);
}

if line.flags().contains(TokenFlags::BRACKET) {
if enforce_whitespace_before_parameters {
whitespace_before_parameters(&line, &mut context);
whitespace_before_parameters(&line, context);
}

if enforce_redundant_backslash {
redundant_backslash(&line, locator, indexer, &mut context);
redundant_backslash(&line, locator, indexer, context);
}
}

Expand Down Expand Up @@ -180,8 +178,7 @@ pub(crate) fn check_logical_lines(
prev_indent_level,
indent_size,
range,
lint_context,
settings,
context,
);
}

Expand All @@ -191,23 +188,3 @@ pub(crate) fn check_logical_lines(
}
}
}

pub(crate) struct LogicalLinesContext<'a, 'b> {
settings: &'a LinterSettings,
context: &'a LintContext<'b>,
}

impl<'a, 'b> LogicalLinesContext<'a, 'b> {
fn new(settings: &'a LinterSettings, context: &'a LintContext<'b>) -> Self {
Self { settings, context }
}

pub(crate) fn report_diagnostic<'chk, T: Violation>(
&'chk self,
kind: T,
range: TextRange,
) -> Option<DiagnosticGuard<'chk, 'a>> {
self.context
.report_diagnostic_if_enabled(kind, range, self.settings)
}
}
22 changes: 8 additions & 14 deletions crates/ruff_linter/src/checkers/physical_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,15 @@ mod tests {

let check_with_max_line_length = |line_length: LineLength| {
let source_file = SourceFileBuilder::new("<filename>", line).finish();
let diagnostics = LintContext::new(&source_file);
check_physical_lines(
&locator,
&stylist,
&indexer,
&[],
&LinterSettings {
pycodestyle: pycodestyle::settings::Settings {
max_line_length: line_length,
..pycodestyle::settings::Settings::default()
},
..LinterSettings::for_rule(Rule::LineTooLong)
let settings = LinterSettings {
pycodestyle: pycodestyle::settings::Settings {
max_line_length: line_length,
..pycodestyle::settings::Settings::default()
},
&diagnostics,
);
..LinterSettings::for_rule(Rule::LineTooLong)
};
let diagnostics = LintContext::new(&source_file, &settings);
check_physical_lines(&locator, &stylist, &indexer, &[], &settings, &diagnostics);
diagnostics.into_diagnostics()
};
let line_length = LineLength::try_from(8).unwrap();
Expand Down
6 changes: 1 addition & 5 deletions crates/ruff_linter/src/checkers/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ruff_python_parser::Tokens;

use crate::Locator;
use crate::directives::TodoComment;
use crate::registry::{AsRule, Rule};
use crate::registry::Rule;
use crate::rules::pycodestyle::rules::BlankLinesChecker;
use crate::rules::{
eradicate, flake8_commas, flake8_executable, flake8_fixme, flake8_implicit_str_concat,
Expand Down Expand Up @@ -170,8 +170,4 @@ pub(crate) fn check_tokens(
if settings.rules.enabled(Rule::TooManyNewlinesAtEndOfFile) {
pycodestyle::rules::too_many_newlines_at_end_of_file(context, tokens, cell_offsets);
}

context
.as_mut_vec()
.retain(|diagnostic| settings.rules.enabled(diagnostic.rule()));
}
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub fn check_path(
SourceFileBuilder::new(path.to_string_lossy().as_ref(), locator.contents()).finish();

// Aggregate all diagnostics.
let mut diagnostics = LintContext::new(&source_file);
let mut diagnostics = LintContext::new(&source_file, settings);

// Aggregate all semantic syntax errors.
let mut semantic_syntax_errors = vec![];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ pub(crate) fn commented_out_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[..]) {
context
.report_diagnostic(CommentedOutCode, range)
.set_fix(Fix::display_only_edit(Edit::range_deletion(
if let Some(mut diagnostic) =
context.report_diagnostic_if_enabled(CommentedOutCode, range)
{
diagnostic.set_fix(Fix::display_only_edit(Edit::range_deletion(
locator.full_lines_range(range),
)));
}
}
}
}
Expand Down
37 changes: 21 additions & 16 deletions crates/ruff_linter/src/rules/flake8_commas/rules/trailing_commas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,20 @@ fn check_token(
};

if comma_prohibited {
let mut diagnostic = lint_context.report_diagnostic(ProhibitedTrailingComma, prev.range());
let range = diagnostic.range();
diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion(range)));
return;
if let Some(mut diagnostic) =
lint_context.report_diagnostic_if_enabled(ProhibitedTrailingComma, prev.range())
{
let range = diagnostic.range();
diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion(range)));
return;
}
}

// Is prev a prohibited trailing comma on a bare tuple?
// Approximation: any comma followed by a statement-ending newline.
let bare_comma_prohibited = prev.ty == TokenType::Comma && token.ty == TokenType::Newline;
if bare_comma_prohibited {
lint_context.report_diagnostic(TrailingCommaOnBareTuple, prev.range());
lint_context.report_diagnostic_if_enabled(TrailingCommaOnBareTuple, prev.range());
return;
}

Expand All @@ -384,17 +387,19 @@ fn check_token(
| TokenType::OpeningCurlyBracket
);
if comma_required {
let mut diagnostic =
lint_context.report_diagnostic(MissingTrailingComma, TextRange::empty(prev_prev.end()));
// Create a replacement that includes the final bracket (or other token),
// rather than just inserting a comma at the end. This prevents the UP034 fix
// removing any brackets in the same linter pass - doing both at the same time could
// lead to a syntax error.
let contents = locator.slice(prev_prev.range());
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
format!("{contents},"),
prev_prev.range(),
)));
if let Some(mut diagnostic) = lint_context
.report_diagnostic_if_enabled(MissingTrailingComma, TextRange::empty(prev_prev.end()))
{
// Create a replacement that includes the final bracket (or other token),
// rather than just inserting a comma at the end. This prevents the UP034 fix
// removing any brackets in the same linter pass - doing both at the same time could
// lead to a syntax error.
let contents = locator.slice(prev_prev.range());
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
format!("{contents},"),
prev_prev.range(),
)));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ pub(crate) fn shebang_leading_whitespace(
}

let prefix = TextRange::up_to(range.start());
context
.report_diagnostic(ShebangLeadingWhitespace, prefix)
.set_fix(Fix::safe_edit(Edit::range_deletion(prefix)));
if let Some(mut diagnostic) =
context.report_diagnostic_if_enabled(ShebangLeadingWhitespace, prefix)
{
diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion(prefix)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(crate) fn shebang_missing_executable_file(filepath: &Path, context: &LintCon
return;
}
if let Ok(true) = is_executable(filepath) {
context.report_diagnostic(
context.report_diagnostic_if_enabled(
ShebangMissingExecutableFile,
ruff_text_size::TextRange::default(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ pub(crate) fn shebang_missing_python(
return;
}

context.report_diagnostic(ShebangMissingPython, range);
context.report_diagnostic_if_enabled(ShebangMissingPython, range);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(crate) fn shebang_not_executable(filepath: &Path, range: TextRange, context:
}

if let Ok(false) = is_executable(filepath) {
context.report_diagnostic(ShebangNotExecutable, range);
context.report_diagnostic_if_enabled(ShebangNotExecutable, range);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ pub(crate) fn shebang_not_first_line(range: TextRange, locator: &Locator, contex
return;
}

context.report_diagnostic(ShebangNotFirstLine, range);
context.report_diagnostic_if_enabled(ShebangNotFirstLine, range);
}
8 changes: 4 additions & 4 deletions crates/ruff_linter/src/rules/flake8_fixme/rules/todos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,19 @@ pub(crate) fn todos(context: &LintContext, directive_ranges: &[TodoComment]) {
match directive.kind {
// FIX001
TodoDirectiveKind::Fixme => {
context.report_diagnostic(LineContainsFixme, directive.range);
context.report_diagnostic_if_enabled(LineContainsFixme, directive.range);
}
// FIX002
TodoDirectiveKind::Hack => {
context.report_diagnostic(LineContainsHack, directive.range);
context.report_diagnostic_if_enabled(LineContainsHack, directive.range);
}
// FIX003
TodoDirectiveKind::Todo => {
context.report_diagnostic(LineContainsTodo, directive.range);
context.report_diagnostic_if_enabled(LineContainsTodo, directive.range);
}
// FIX004
TodoDirectiveKind::Xxx => {
context.report_diagnostic(LineContainsXxx, directive.range);
context.report_diagnostic_if_enabled(LineContainsXxx, directive.range);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,18 @@ pub(crate) fn implicit(
};

if locator.contains_line_break(TextRange::new(a_range.end(), b_range.start())) {
context.report_diagnostic(
context.report_diagnostic_if_enabled(
MultiLineImplicitStringConcatenation,
TextRange::new(a_range.start(), b_range.end()),
);
} else {
let mut diagnostic = context.report_diagnostic(
if let Some(mut diagnostic) = context.report_diagnostic_if_enabled(
SingleLineImplicitStringConcatenation,
TextRange::new(a_range.start(), b_range.end()),
);

if let Some(fix) = concatenate_strings(a_range, b_range, locator) {
diagnostic.set_fix(fix);
) {
if let Some(fix) = concatenate_strings(a_range, b_range, locator) {
diagnostic.set_fix(fix);
}
}
}
}
Expand Down
Loading
Loading