mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
chore: Do not read the same file twice (#206)
This commit is contained in:
parent
17b3109a8b
commit
0091a3ae5f
1 changed files with 22 additions and 10 deletions
|
@ -13,10 +13,12 @@ use crate::message::Message;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
use crate::{cache, fs};
|
use crate::{cache, fs};
|
||||||
|
|
||||||
fn check_path(path: &Path, settings: &Settings, autofix: &fixer::Mode) -> Result<Vec<Check>> {
|
fn check_path(
|
||||||
// Read the file from disk.
|
path: &Path,
|
||||||
let contents = fs::read_file(path)?;
|
contents: &str,
|
||||||
|
settings: &Settings,
|
||||||
|
autofix: &fixer::Mode,
|
||||||
|
) -> Result<Vec<Check>> {
|
||||||
// Aggregate all checks.
|
// Aggregate all checks.
|
||||||
let mut checks: Vec<Check> = vec![];
|
let mut checks: Vec<Check> = vec![];
|
||||||
|
|
||||||
|
@ -26,12 +28,12 @@ fn check_path(path: &Path, settings: &Settings, autofix: &fixer::Mode) -> Result
|
||||||
.iter()
|
.iter()
|
||||||
.any(|check_code| matches!(check_code.lint_source(), LintSource::AST))
|
.any(|check_code| matches!(check_code.lint_source(), LintSource::AST))
|
||||||
{
|
{
|
||||||
let python_ast = parser::parse_program(&contents, "<filename>")?;
|
let python_ast = parser::parse_program(contents, "<filename>")?;
|
||||||
checks.extend(check_ast(&python_ast, &contents, settings, autofix, path));
|
checks.extend(check_ast(&python_ast, contents, settings, autofix, path));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the lines-based checks.
|
// Run the lines-based checks.
|
||||||
check_lines(&mut checks, &contents, settings);
|
check_lines(&mut checks, contents, settings);
|
||||||
|
|
||||||
Ok(checks)
|
Ok(checks)
|
||||||
}
|
}
|
||||||
|
@ -54,7 +56,7 @@ pub fn lint_path(
|
||||||
let contents = fs::read_file(path)?;
|
let contents = fs::read_file(path)?;
|
||||||
|
|
||||||
// Generate checks.
|
// Generate checks.
|
||||||
let mut checks = check_path(path, settings, autofix)?;
|
let mut checks = check_path(path, &contents, settings, autofix)?;
|
||||||
|
|
||||||
// Apply autofix.
|
// Apply autofix.
|
||||||
if matches!(autofix, fixer::Mode::Apply) {
|
if matches!(autofix, fixer::Mode::Apply) {
|
||||||
|
@ -84,10 +86,20 @@ mod tests {
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
use crate::autofix::fixer;
|
use crate::autofix::fixer;
|
||||||
use crate::checks::CheckCode;
|
use crate::checks::{Check, CheckCode};
|
||||||
use crate::linter::check_path;
|
use crate::fs;
|
||||||
|
use crate::linter;
|
||||||
use crate::settings;
|
use crate::settings;
|
||||||
|
|
||||||
|
fn check_path(
|
||||||
|
path: &Path,
|
||||||
|
settings: &settings::Settings,
|
||||||
|
autofix: &fixer::Mode,
|
||||||
|
) -> Result<Vec<Check>> {
|
||||||
|
let contents = fs::read_file(path)?;
|
||||||
|
linter::check_path(path, &contents, settings, autofix)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn e402() -> Result<()> {
|
fn e402() -> Result<()> {
|
||||||
let mut checks = check_path(
|
let mut checks = check_path(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue