mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00

Rebase of https://github.com/astral-sh/ruff/pull/5119 authored by @evanrittenhouse with additional refinements. ## Changes - Adds `--unsafe-fixes` / `--no-unsafe-fixes` flags to `ruff check` - Violations with unsafe fixes are not shown as fixable unless opted-in - Fix applicability is respected now - `Applicability::Never` fixes are no longer applied - `Applicability::Sometimes` fixes require opt-in - `Applicability::Always` fixes are unchanged - Hints for availability of `--unsafe-fixes` added to `ruff check` output ## Examples Check hints at hidden unsafe fixes ``` ❯ ruff check example.py --no-cache --select F601,W292 example.py:1:14: F601 Dictionary key literal `'a'` repeated example.py:2:15: W292 [*] No newline at end of file Found 2 errors. [*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option). ``` We could add an indicator for which violations have hidden fixes in the future. Check treats unsafe fixes as applicable with opt-in ``` ❯ ruff check example.py --no-cache --select F601,W292 --unsafe-fixes example.py:1:14: F601 [*] Dictionary key literal `'a'` repeated example.py:2:15: W292 [*] No newline at end of file Found 2 errors. [*] 2 fixable with the --fix option. ``` Also can be enabled in the config file ``` ❯ cat ruff.toml unsafe-fixes = true ``` And opted-out per invocation ``` ❯ ruff check example.py --no-cache --select F601,W292 --no-unsafe-fixes example.py:1:14: F601 Dictionary key literal `'a'` repeated example.py:2:15: W292 [*] No newline at end of file Found 2 errors. [*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option). ``` Diff does not include unsafe fixes ``` ❯ ruff check example.py --no-cache --select F601,W292 --diff --- example.py +++ example.py @@ -1,2 +1,2 @@ x = {'a': 1, 'a': 1} -print(('foo')) +print(('foo')) \ No newline at end of file Would fix 1 error. ``` Unless there is opt-in ``` ❯ ruff check example.py --no-cache --select F601,W292 --diff --unsafe-fixes --- example.py +++ example.py @@ -1,2 +1,2 @@ -x = {'a': 1} -print(('foo')) +x = {'a': 1, 'a': 1} +print(('foo')) \ No newline at end of file Would fix 2 errors. ``` https://github.com/astral-sh/ruff/pull/7790 will improve the diff messages following this pull request Similarly, `--fix` and `--fix-only` require the `--unsafe-fixes` flag to apply unsafe fixes. ## Related Replaces #5119 Closes https://github.com/astral-sh/ruff/issues/4185 Closes https://github.com/astral-sh/ruff/issues/7214 Closes https://github.com/astral-sh/ruff/issues/4845 Closes https://github.com/astral-sh/ruff/issues/3863 Addresses https://github.com/astral-sh/ruff/issues/6835 Addresses https://github.com/astral-sh/ruff/issues/7019 Needs follow-up https://github.com/astral-sh/ruff/issues/6962 Needs follow-up https://github.com/astral-sh/ruff/issues/4845 Needs follow-up https://github.com/astral-sh/ruff/issues/7436 Needs follow-up https://github.com/astral-sh/ruff/issues/7025 Needs follow-up https://github.com/astral-sh/ruff/issues/6434 Follow-up #7790 Follow-up https://github.com/astral-sh/ruff/pull/7792 --------- Co-authored-by: Evan Rittenhouse <evanrittenhouse@gmail.com>
157 lines
4 KiB
Rust
157 lines
4 KiB
Rust
//! Tests the interaction of the `lint` configuration section
|
|
|
|
#![cfg(not(target_family = "wasm"))]
|
|
|
|
use std::fs;
|
|
use std::process::Command;
|
|
use std::str;
|
|
|
|
use anyhow::Result;
|
|
use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
|
|
use tempfile::TempDir;
|
|
|
|
const BIN_NAME: &str = "ruff";
|
|
const STDIN_BASE_OPTIONS: &[&str] = &["--no-cache", "--output-format", "text"];
|
|
|
|
#[test]
|
|
fn top_level_options() -> Result<()> {
|
|
let tempdir = TempDir::new()?;
|
|
let ruff_toml = tempdir.path().join("ruff.toml");
|
|
fs::write(
|
|
&ruff_toml,
|
|
r#"
|
|
extend-select = ["B", "Q"]
|
|
|
|
[flake8-quotes]
|
|
inline-quotes = "single"
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
|
|
.args(STDIN_BASE_OPTIONS)
|
|
.arg("--config")
|
|
.arg(&ruff_toml)
|
|
.arg("-")
|
|
.pass_stdin(r#"a = "abcba".strip("aba")"#), @r###"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
-:1:5: Q000 [*] Double quotes found but single quotes preferred
|
|
-:1:5: B005 Using `.strip()` with multi-character strings is misleading
|
|
-:1:19: Q000 [*] Double quotes found but single quotes preferred
|
|
Found 3 errors.
|
|
[*] 2 fixable with the `--fix` option.
|
|
|
|
----- stderr -----
|
|
"###);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn lint_options() -> Result<()> {
|
|
let tempdir = TempDir::new()?;
|
|
let ruff_toml = tempdir.path().join("ruff.toml");
|
|
fs::write(
|
|
&ruff_toml,
|
|
r#"
|
|
[lint]
|
|
extend-select = ["B", "Q"]
|
|
|
|
[lint.flake8-quotes]
|
|
inline-quotes = "single"
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
|
|
.args(STDIN_BASE_OPTIONS)
|
|
.arg("--config")
|
|
.arg(&ruff_toml)
|
|
.arg("-")
|
|
.pass_stdin(r#"a = "abcba".strip("aba")"#), @r###"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
-:1:5: Q000 [*] Double quotes found but single quotes preferred
|
|
-:1:5: B005 Using `.strip()` with multi-character strings is misleading
|
|
-:1:19: Q000 [*] Double quotes found but single quotes preferred
|
|
Found 3 errors.
|
|
[*] 2 fixable with the `--fix` option.
|
|
|
|
----- stderr -----
|
|
"###);
|
|
Ok(())
|
|
}
|
|
|
|
/// Tests that configurations from the top-level and `lint` section are merged together.
|
|
#[test]
|
|
fn mixed_levels() -> Result<()> {
|
|
let tempdir = TempDir::new()?;
|
|
let ruff_toml = tempdir.path().join("ruff.toml");
|
|
fs::write(
|
|
&ruff_toml,
|
|
r#"
|
|
extend-select = ["B", "Q"]
|
|
|
|
[lint.flake8-quotes]
|
|
inline-quotes = "single"
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
|
|
.args(STDIN_BASE_OPTIONS)
|
|
.arg("--config")
|
|
.arg(&ruff_toml)
|
|
.arg("-")
|
|
.pass_stdin(r#"a = "abcba".strip("aba")"#), @r###"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
-:1:5: Q000 [*] Double quotes found but single quotes preferred
|
|
-:1:5: B005 Using `.strip()` with multi-character strings is misleading
|
|
-:1:19: Q000 [*] Double quotes found but single quotes preferred
|
|
Found 3 errors.
|
|
[*] 2 fixable with the `--fix` option.
|
|
|
|
----- stderr -----
|
|
"###);
|
|
Ok(())
|
|
}
|
|
|
|
/// Tests that options in the `lint` section have higher precedence than top-level options (because they are more specific).
|
|
#[test]
|
|
fn precedence() -> Result<()> {
|
|
let tempdir = TempDir::new()?;
|
|
let ruff_toml = tempdir.path().join("ruff.toml");
|
|
fs::write(
|
|
&ruff_toml,
|
|
r#"
|
|
[lint]
|
|
extend-select = ["B", "Q"]
|
|
|
|
[flake8-quotes]
|
|
inline-quotes = "double"
|
|
|
|
[lint.flake8-quotes]
|
|
inline-quotes = "single"
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
|
|
.args(STDIN_BASE_OPTIONS)
|
|
.arg("--config")
|
|
.arg(&ruff_toml)
|
|
.arg("-")
|
|
.pass_stdin(r#"a = "abcba".strip("aba")"#), @r###"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
-:1:5: Q000 [*] Double quotes found but single quotes preferred
|
|
-:1:5: B005 Using `.strip()` with multi-character strings is misleading
|
|
-:1:19: Q000 [*] Double quotes found but single quotes preferred
|
|
Found 3 errors.
|
|
[*] 2 fixable with the `--fix` option.
|
|
|
|
----- stderr -----
|
|
"###);
|
|
Ok(())
|
|
}
|