Only run pyproject.toml lint rules when enabled (#5578)

## Summary

I was testing some changes on Airflow, and I realized that we _always_
run the `pyproject.toml` validation rules, even if they're not enabled.
This PR gates them behind the appropriate enablement flags.

## Test Plan

- Ran: `cargo run -p ruff_cli -- check ../airflow -n`. Verified that no
RUF200 violations were raised.
- Run: `cargo run -p ruff_cli -- check ../airflow -n --select RUF200`.
Verified that two RUF200 violations were raised.
This commit is contained in:
Charlie Marsh 2023-07-08 11:05:05 -04:00 committed by GitHub
parent d0dae7e576
commit a1c559eaa4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 25 deletions

View file

@ -127,11 +127,20 @@ pub(crate) fn lint_path(
debug!("Checking: {}", path.display());
// We have to special case this here since the python tokenizer doesn't work with toml
// We have to special case this here since the Python tokenizer doesn't work with TOML.
if is_project_toml(path) {
let contents = std::fs::read_to_string(path)?;
let source_file = SourceFileBuilder::new(path.to_string_lossy(), contents).finish();
let messages = lint_pyproject_toml(source_file)?;
let messages = if settings
.lib
.rules
.iter_enabled()
.any(|rule_code| rule_code.lint_source().is_pyproject_toml())
{
let contents = std::fs::read_to_string(path)?;
let source_file = SourceFileBuilder::new(path.to_string_lossy(), contents).finish();
lint_pyproject_toml(source_file, &settings.lib)?
} else {
vec![]
};
return Ok(Diagnostics {
messages,
..Diagnostics::default()