-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Implements [`subprocess-run-check` (`W1510`)](https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/subprocess-run-check.html) as `subprocess-run-without-check` (`PLW1510`). Includes documentation. Related to #970. ## Test Plan `cargo test`
- Loading branch information
Showing
8 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
crates/ruff/resources/test/fixtures/pylint/subprocess_run_without_check.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import subprocess | ||
|
||
# Errors. | ||
subprocess.run("ls") | ||
subprocess.run("ls", shell=True) | ||
|
||
# Non-errors. | ||
subprocess.run("ls", check=True) | ||
subprocess.run("ls", check=False) | ||
subprocess.run("ls", shell=True, check=True) | ||
subprocess.run("ls", shell=True, check=False) | ||
foo.run("ls") # Not a subprocess.run call. | ||
subprocess.bar("ls") # Not a subprocess.run call. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
crates/ruff/src/rules/pylint/rules/subprocess_run_without_check.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast as ast; | ||
use ruff_python_ast::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `subprocess.run` without an explicit `check` argument. | ||
/// | ||
/// ## Why is this bad? | ||
/// By default, `subprocess.run` does not check the return code of the process | ||
/// it runs. This can lead to silent failures. | ||
/// | ||
/// Instead, consider using `check=True` to raise an exception if the process | ||
/// fails, or set `check=False` explicitly to mark the behavior as intentional. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import subprocess | ||
/// | ||
/// subprocess.run(["ls", "nonexistent"]) # No exception raised. | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// import subprocess | ||
/// | ||
/// subprocess.run(["ls", "nonexistent"], check=True) # Raises exception. | ||
/// ``` | ||
/// | ||
/// Or: | ||
/// ```python | ||
/// import subprocess | ||
/// | ||
/// subprocess.run(["ls", "nonexistent"], check=False) # Explicitly no check. | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: `subprocess.run`](https://docs.python.org/3/library/subprocess.html#subprocess.run) | ||
#[violation] | ||
pub struct SubprocessRunWithoutCheck; | ||
|
||
impl Violation for SubprocessRunWithoutCheck { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`subprocess.run` without explicit `check` argument") | ||
} | ||
} | ||
|
||
/// PLW1510 | ||
pub(crate) fn subprocess_run_without_check(checker: &mut Checker, call: &ast::ExprCall) { | ||
if checker | ||
.semantic() | ||
.resolve_call_path(&call.func) | ||
.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(), | ||
)); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...pylint/snapshots/ruff__rules__pylint__tests__PLW1510_subprocess_run_without_check.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
--- | ||
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) | ||
| | ||
|
||
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. | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.