Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pylint] Add fix for subprocess-run-without-check (PLW1510) #6708

Merged
merged 10 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
# Errors.
subprocess.run("ls")
subprocess.run("ls", shell=True)
subprocess.run(
["ls"],
shell=False,
)

# Non-errors.
subprocess.run("ls", check=True)
Expand Down
29 changes: 23 additions & 6 deletions crates/ruff/src/rules/pylint/rules/subprocess_run_without_check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_text_size::TextSize;

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast as ast;
use ruff_python_ast::Ranged;
Expand Down Expand Up @@ -41,11 +43,15 @@ use crate::checkers::ast::Checker;
#[violation]
pub struct SubprocessRunWithoutCheck;

impl Violation for SubprocessRunWithoutCheck {
impl AlwaysAutofixableViolation for SubprocessRunWithoutCheck {
#[derive_message_formats]
fn message(&self) -> String {
format!("`subprocess.run` without explicit `check` argument")
}

fn autofix_title(&self) -> String {
"Add an explicit check=False".to_string()
}
}

/// PLW1510
Expand All @@ -56,10 +62,21 @@ pub(crate) fn subprocess_run_without_check(checker: &mut Checker, call: &ast::Ex
.is_some_and(|call_path| matches!(call_path.as_slice(), ["subprocess", "run"]))
{
if call.arguments.find_keyword("check").is_none() {
checker.diagnostics.push(Diagnostic::new(
SubprocessRunWithoutCheck,
call.func.range(),
));
let mut diagnostic = Diagnostic::new(SubprocessRunWithoutCheck, call.func.range());
let text: &str = checker.locator().slice(call.range());
let ends_with_comma = text[..text.len() - 1].trim_end().ends_with(',');
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved
diagnostic.set_fix(Fix::automatic(Edit::insertion(
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved
{
if ends_with_comma {
"check=False"
} else {
", check=False"
}
}
.to_string(),
call.range().end() - TextSize::from(1),
)));
checker.diagnostics.push(diagnostic);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,66 @@
---
source: crates/ruff/src/rules/pylint/mod.rs
---
subprocess_run_without_check.py:4:1: PLW1510 `subprocess.run` without explicit `check` argument
subprocess_run_without_check.py:4:1: PLW1510 [*] `subprocess.run` without explicit `check` argument
|
3 | # Errors.
4 | subprocess.run("ls")
| ^^^^^^^^^^^^^^ PLW1510
5 | subprocess.run("ls", shell=True)
6 | subprocess.run(
|
= help: Add an explicit check=False

subprocess_run_without_check.py:5:1: PLW1510 `subprocess.run` without explicit `check` argument
ℹ Fix
1 1 | import subprocess
2 2 |
3 3 | # Errors.
4 |-subprocess.run("ls")
4 |+subprocess.run("ls", check=False)
5 5 | subprocess.run("ls", shell=True)
6 6 | subprocess.run(
7 7 | ["ls"],

subprocess_run_without_check.py:5:1: PLW1510 [*] `subprocess.run` without explicit `check` argument
|
3 | # Errors.
4 | subprocess.run("ls")
5 | subprocess.run("ls", shell=True)
| ^^^^^^^^^^^^^^ PLW1510
6 |
7 | # Non-errors.
6 | subprocess.run(
7 | ["ls"],
|
= help: Add an explicit check=False

ℹ Fix
2 2 |
3 3 | # Errors.
4 4 | subprocess.run("ls")
5 |-subprocess.run("ls", shell=True)
5 |+subprocess.run("ls", shell=True, check=False)
6 6 | subprocess.run(
7 7 | ["ls"],
8 8 | shell=False,

subprocess_run_without_check.py:6:1: PLW1510 [*] `subprocess.run` without explicit `check` argument
|
4 | subprocess.run("ls")
5 | subprocess.run("ls", shell=True)
6 | subprocess.run(
| ^^^^^^^^^^^^^^ PLW1510
7 | ["ls"],
8 | shell=False,
|
= help: Add an explicit check=False

ℹ Fix
6 6 | subprocess.run(
7 7 | ["ls"],
8 8 | shell=False,
9 |-)
9 |+check=False)
10 10 |
11 11 | # Non-errors.
12 12 | subprocess.run("ls", check=True)


Loading