Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 2 additions & 3 deletions crates/ruff/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rayon::prelude::*;
use rustc_hash::FxHashMap;

use ruff_db::panic::catch_unwind;
use ruff_linter::Diagnostic;
use ruff_linter::OldDiagnostic;
use ruff_linter::message::Message;
use ruff_linter::package::PackageRoot;
use ruff_linter::registry::Rule;
Expand Down Expand Up @@ -131,8 +131,7 @@ pub(crate) fn check(

Diagnostics::new(
vec![Message::from_diagnostic(
Diagnostic::new(IOError { message }, TextRange::default()),
dummy,
OldDiagnostic::new(IOError { message }, TextRange::default(), &dummy),
None,
)],
FxHashMap::default(),
Expand Down
10 changes: 5 additions & 5 deletions crates/ruff/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use colored::Colorize;
use log::{debug, warn};
use rustc_hash::FxHashMap;

use ruff_linter::Diagnostic;
use ruff_linter::OldDiagnostic;
use ruff_linter::codes::Rule;
use ruff_linter::linter::{FixTable, FixerResult, LinterResult, ParseSource, lint_fix, lint_only};
use ruff_linter::message::Message;
Expand Down Expand Up @@ -64,13 +64,13 @@ impl Diagnostics {
let source_file = SourceFileBuilder::new(name, "").finish();
Self::new(
vec![Message::from_diagnostic(
Diagnostic::new(
OldDiagnostic::new(
IOError {
message: err.to_string(),
},
TextRange::default(),
&source_file,
),
source_file,
None,
)],
FxHashMap::default(),
Expand Down Expand Up @@ -235,7 +235,7 @@ pub(crate) fn lint_path(
};
let source_file =
SourceFileBuilder::new(path.to_string_lossy(), contents).finish();
lint_pyproject_toml(source_file, settings)
lint_pyproject_toml(&source_file, settings)
} else {
vec![]
};
Expand Down Expand Up @@ -396,7 +396,7 @@ pub(crate) fn lint_stdin(
}

return Ok(Diagnostics {
messages: lint_pyproject_toml(source_file, &settings.linter),
messages: lint_pyproject_toml(&source_file, &settings.linter),
fixed: FixMap::from_iter([(fs::relativize_path(path), FixTable::default())]),
notebook_indexes: FxHashMap::default(),
});
Expand Down
32 changes: 20 additions & 12 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use ruff_python_semantic::{
};
use ruff_python_stdlib::builtins::{MAGIC_GLOBALS, python_builtins};
use ruff_python_trivia::CommentRanges;
use ruff_source_file::{OneIndexed, SourceRow};
use ruff_source_file::{OneIndexed, SourceFile, SourceRow};
use ruff_text_size::{Ranged, TextRange, TextSize};

use crate::checkers::ast::annotation::AnnotationContext;
Expand All @@ -73,7 +73,7 @@ use crate::rules::pyflakes::rules::{
use crate::rules::pylint::rules::{AwaitOutsideAsync, LoadBeforeGlobalDeclaration};
use crate::rules::{flake8_pyi, flake8_type_checking, pyflakes, pyupgrade};
use crate::settings::{LinterSettings, TargetVersion, flags};
use crate::{Diagnostic, Edit, Violation};
use crate::{Edit, OldDiagnostic, Violation};
use crate::{Locator, docstrings, noqa};

mod analyze;
Expand Down Expand Up @@ -225,7 +225,7 @@ pub(crate) struct Checker<'a> {
/// A set of deferred nodes to be analyzed after the AST traversal (e.g., `for` loops).
analyze: deferred::Analyze,
/// The cumulative set of diagnostics computed across all lint rules.
diagnostics: RefCell<Vec<Diagnostic>>,
diagnostics: RefCell<Vec<OldDiagnostic>>,
/// The list of names already seen by flake8-bugbear diagnostics, to avoid duplicate violations.
flake8_bugbear_seen: RefCell<FxHashSet<TextRange>>,
/// The end offset of the last visited statement.
Expand All @@ -239,6 +239,8 @@ pub(crate) struct Checker<'a> {
semantic_checker: SemanticSyntaxChecker,
/// Errors collected by the `semantic_checker`.
semantic_errors: RefCell<Vec<SemanticSyntaxError>>,
/// The [`SourceFile`] corresponding to the file under analysis.
source_file: &'a SourceFile,
}

impl<'a> Checker<'a> {
Expand All @@ -259,6 +261,7 @@ impl<'a> Checker<'a> {
cell_offsets: Option<&'a CellOffsets>,
notebook_index: Option<&'a NotebookIndex>,
target_version: TargetVersion,
source_file: &'a SourceFile,
) -> Checker<'a> {
let semantic = SemanticModel::new(&settings.typing_modules, path, module);
Self {
Expand Down Expand Up @@ -288,6 +291,7 @@ impl<'a> Checker<'a> {
target_version,
semantic_checker: SemanticSyntaxChecker::new(),
semantic_errors: RefCell::default(),
source_file,
}
}
}
Expand Down Expand Up @@ -391,7 +395,7 @@ impl<'a> Checker<'a> {
) -> DiagnosticGuard<'chk, 'a> {
DiagnosticGuard {
checker: self,
diagnostic: Some(Diagnostic::new(kind, range)),
diagnostic: Some(OldDiagnostic::new(kind, range, self.source_file)),
}
}

Expand All @@ -405,7 +409,7 @@ impl<'a> Checker<'a> {
kind: T,
range: TextRange,
) -> Option<DiagnosticGuard<'chk, 'a>> {
let diagnostic = Diagnostic::new(kind, range);
let diagnostic = OldDiagnostic::new(kind, range, self.source_file);
if self.enabled(diagnostic.rule()) {
Some(DiagnosticGuard {
checker: self,
Expand Down Expand Up @@ -2892,11 +2896,12 @@ impl<'a> Checker<'a> {
if self.semantic.global_scope().uses_star_imports() {
if self.enabled(Rule::UndefinedLocalWithImportStarUsage) {
self.diagnostics.get_mut().push(
Diagnostic::new(
OldDiagnostic::new(
pyflakes::rules::UndefinedLocalWithImportStarUsage {
name: name.to_string(),
},
range,
self.source_file,
)
.with_parent(definition.start()),
);
Expand All @@ -2907,11 +2912,12 @@ impl<'a> Checker<'a> {
|| !self.path.ends_with("__init__.py")
{
self.diagnostics.get_mut().push(
Diagnostic::new(
OldDiagnostic::new(
pyflakes::rules::UndefinedExport {
name: name.to_string(),
},
range,
self.source_file,
)
.with_parent(definition.start()),
);
Expand Down Expand Up @@ -2975,7 +2981,8 @@ pub(crate) fn check_ast(
cell_offsets: Option<&CellOffsets>,
notebook_index: Option<&NotebookIndex>,
target_version: TargetVersion,
) -> (Vec<Diagnostic>, Vec<SemanticSyntaxError>) {
source_file: &SourceFile,
) -> (Vec<OldDiagnostic>, Vec<SemanticSyntaxError>) {
let module_path = package
.map(PackageRoot::path)
.and_then(|package| to_module_path(package, path));
Expand Down Expand Up @@ -3015,6 +3022,7 @@ pub(crate) fn check_ast(
cell_offsets,
notebook_index,
target_version,
source_file,
);
checker.bind_builtins();

Expand Down Expand Up @@ -3062,7 +3070,7 @@ pub(crate) struct DiagnosticGuard<'a, 'b> {
/// The diagnostic that we want to report.
///
/// This is always `Some` until the `Drop` (or `defuse`) call.
diagnostic: Option<Diagnostic>,
diagnostic: Option<OldDiagnostic>,
}

impl DiagnosticGuard<'_, '_> {
Expand All @@ -3076,17 +3084,17 @@ impl DiagnosticGuard<'_, '_> {
}

impl std::ops::Deref for DiagnosticGuard<'_, '_> {
type Target = Diagnostic;
type Target = OldDiagnostic;

fn deref(&self) -> &Diagnostic {
fn deref(&self) -> &OldDiagnostic {
// OK because `self.diagnostic` is only `None` within `Drop`.
self.diagnostic.as_ref().unwrap()
}
}

/// Return a mutable borrow of the diagnostic in this guard.
impl std::ops::DerefMut for DiagnosticGuard<'_, '_> {
fn deref_mut(&mut self) -> &mut Diagnostic {
fn deref_mut(&mut self) -> &mut OldDiagnostic {
// OK because `self.diagnostic` is only `None` within `Drop`.
self.diagnostic.as_mut().unwrap()
}
Expand Down
22 changes: 15 additions & 7 deletions crates/ruff_linter/src/checkers/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::path::Path;

use ruff_python_ast::PythonVersion;
use ruff_python_trivia::CommentRanges;
use ruff_source_file::SourceFile;

use crate::Diagnostic;
use crate::Locator;
use crate::OldDiagnostic;
use crate::package::PackageRoot;
use crate::preview::is_allow_nested_roots_enabled;
use crate::registry::Rule;
Expand All @@ -20,8 +21,9 @@ pub(crate) fn check_file_path(
comment_ranges: &CommentRanges,
settings: &LinterSettings,
target_version: PythonVersion,
) -> Vec<Diagnostic> {
let mut diagnostics: Vec<Diagnostic> = vec![];
source_file: &SourceFile,
) -> Vec<OldDiagnostic> {
let mut diagnostics: Vec<OldDiagnostic> = vec![];

// flake8-no-pep420
if settings.rules.enabled(Rule::ImplicitNamespacePackage) {
Expand All @@ -34,23 +36,29 @@ pub(crate) fn check_file_path(
&settings.project_root,
&settings.src,
allow_nested_roots,
source_file,
) {
diagnostics.push(diagnostic);
}
}

// pep8-naming
if settings.rules.enabled(Rule::InvalidModuleName) {
if let Some(diagnostic) =
invalid_module_name(path, package, &settings.pep8_naming.ignore_names)
{
if let Some(diagnostic) = invalid_module_name(
path,
package,
&settings.pep8_naming.ignore_names,
source_file,
) {
diagnostics.push(diagnostic);
}
}

// flake8-builtins
if settings.rules.enabled(Rule::StdlibModuleShadowing) {
if let Some(diagnostic) = stdlib_module_shadowing(path, settings, target_version) {
if let Some(diagnostic) =
stdlib_module_shadowing(path, settings, target_version, source_file)
{
diagnostics.push(diagnostic);
}
}
Expand Down
8 changes: 6 additions & 2 deletions crates/ruff_linter/src/checkers/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use ruff_python_ast::{ModModule, PySourceType, PythonVersion};
use ruff_python_codegen::Stylist;
use ruff_python_index::Indexer;
use ruff_python_parser::Parsed;
use ruff_source_file::SourceFile;

use crate::Diagnostic;
use crate::Locator;
use crate::OldDiagnostic;
use crate::directives::IsortDirectives;
use crate::package::PackageRoot;
use crate::registry::Rule;
Expand All @@ -28,7 +29,8 @@ pub(crate) fn check_imports(
source_type: PySourceType,
cell_offsets: Option<&CellOffsets>,
target_version: PythonVersion,
) -> Vec<Diagnostic> {
source_file: &SourceFile,
) -> Vec<OldDiagnostic> {
// Extract all import blocks from the AST.
let tracker = {
let mut tracker =
Expand All @@ -54,6 +56,7 @@ pub(crate) fn check_imports(
source_type,
parsed.tokens(),
target_version,
source_file,
) {
diagnostics.push(diagnostic);
}
Expand All @@ -67,6 +70,7 @@ pub(crate) fn check_imports(
stylist,
settings,
source_type,
source_file,
));
}

Expand Down
22 changes: 15 additions & 7 deletions crates/ruff_linter/src/checkers/logical_lines.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use ruff_python_codegen::Stylist;
use ruff_python_index::Indexer;
use ruff_python_parser::{TokenKind, Tokens};
use ruff_source_file::LineRanges;
use ruff_source_file::{LineRanges, SourceFile};
use ruff_text_size::{Ranged, TextRange};

use crate::Diagnostic;
use crate::Locator;
use crate::OldDiagnostic;
use crate::line_width::IndentWidth;
use crate::registry::{AsRule, Rule};
use crate::rules::pycodestyle::rules::logical_lines::{
Expand Down Expand Up @@ -40,8 +40,9 @@ pub(crate) fn check_logical_lines(
indexer: &Indexer,
stylist: &Stylist,
settings: &LinterSettings,
) -> Vec<Diagnostic> {
let mut context = LogicalLinesContext::new(settings);
source_file: &SourceFile,
) -> Vec<OldDiagnostic> {
let mut context = LogicalLinesContext::new(settings, source_file);

let mut prev_line = None;
let mut prev_indent_level = None;
Expand Down Expand Up @@ -178,6 +179,7 @@ pub(crate) fn check_logical_lines(
prev_indent_level,
indent_size,
range,
source_file,
) {
if settings.rules.enabled(diagnostic.rule()) {
context.push_diagnostic(diagnostic);
Expand All @@ -196,20 +198,26 @@ pub(crate) fn check_logical_lines(
#[derive(Debug, Clone)]
pub(crate) struct LogicalLinesContext<'a> {
settings: &'a LinterSettings,
diagnostics: Vec<Diagnostic>,
source_file: &'a SourceFile,
diagnostics: Vec<OldDiagnostic>,
}

impl<'a> LogicalLinesContext<'a> {
fn new(settings: &'a LinterSettings) -> Self {
fn new(settings: &'a LinterSettings, source_file: &'a SourceFile) -> Self {
Self {
settings,
source_file,
diagnostics: Vec::new(),
}
}

pub(crate) fn push_diagnostic(&mut self, diagnostic: Diagnostic) {
pub(crate) fn push_diagnostic(&mut self, diagnostic: OldDiagnostic) {
if self.settings.rules.enabled(diagnostic.rule()) {
self.diagnostics.push(diagnostic);
}
}

pub(crate) fn source_file(&self) -> &SourceFile {
self.source_file
}
}
Loading