mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:09:22 +00:00
Avoid S108
if path is inside tempfile.*
call (#6416)
This commit is contained in:
parent
a2758513de
commit
887a47cad9
3 changed files with 48 additions and 22 deletions
|
@ -14,3 +14,19 @@ with open("/dev/shm/unit/test", "w") as f:
|
||||||
# not ok by config
|
# not ok by config
|
||||||
with open("/foo/bar", "w") as f:
|
with open("/foo/bar", "w") as f:
|
||||||
f.write("def")
|
f.write("def")
|
||||||
|
|
||||||
|
# Using `tempfile` module should be ok
|
||||||
|
import tempfile
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
|
with tempfile.NamedTemporaryFile(dir="/tmp") as f:
|
||||||
|
f.write(b"def")
|
||||||
|
|
||||||
|
with tempfile.NamedTemporaryFile(dir="/var/tmp") as f:
|
||||||
|
f.write(b"def")
|
||||||
|
|
||||||
|
with tempfile.TemporaryDirectory(dir="/dev/shm") as d:
|
||||||
|
pass
|
||||||
|
|
||||||
|
with TemporaryDirectory(dir="/tmp") as d:
|
||||||
|
pass
|
||||||
|
|
|
@ -1229,13 +1229,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if checker.enabled(Rule::HardcodedTempFile) {
|
if checker.enabled(Rule::HardcodedTempFile) {
|
||||||
if let Some(diagnostic) = flake8_bandit::rules::hardcoded_tmp_directory(
|
flake8_bandit::rules::hardcoded_tmp_directory(checker, expr, value);
|
||||||
expr,
|
|
||||||
value,
|
|
||||||
&checker.settings.flake8_bandit.hardcoded_tmp_directory,
|
|
||||||
) {
|
|
||||||
checker.diagnostics.push(diagnostic);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if checker.enabled(Rule::UnicodeKindPrefix) {
|
if checker.enabled(Rule::UnicodeKindPrefix) {
|
||||||
pyupgrade::rules::unicode_kind_prefix(checker, expr, kind.as_deref());
|
pyupgrade::rules::unicode_kind_prefix(checker, expr, kind.as_deref());
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
use ruff_python_ast::{Expr, Ranged};
|
use ruff_python_ast::{self as ast, Expr, Ranged};
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
|
||||||
|
use crate::checkers::ast::Checker;
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
/// Checks for the use of hardcoded temporary file or directory paths.
|
/// Checks for the use of hardcoded temporary file or directory paths.
|
||||||
///
|
///
|
||||||
|
@ -49,19 +51,33 @@ impl Violation for HardcodedTempFile {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// S108
|
/// S108
|
||||||
pub(crate) fn hardcoded_tmp_directory(
|
pub(crate) fn hardcoded_tmp_directory(checker: &mut Checker, expr: &Expr, value: &str) {
|
||||||
expr: &Expr,
|
if !checker
|
||||||
value: &str,
|
.settings
|
||||||
prefixes: &[String],
|
.flake8_bandit
|
||||||
) -> Option<Diagnostic> {
|
.hardcoded_tmp_directory
|
||||||
if prefixes.iter().any(|prefix| value.starts_with(prefix)) {
|
.iter()
|
||||||
Some(Diagnostic::new(
|
.any(|prefix| value.starts_with(prefix))
|
||||||
HardcodedTempFile {
|
{
|
||||||
string: value.to_string(),
|
return;
|
||||||
},
|
|
||||||
expr.range(),
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(Expr::Call(ast::ExprCall { func, .. })) =
|
||||||
|
checker.semantic().current_expression_parent()
|
||||||
|
{
|
||||||
|
if checker
|
||||||
|
.semantic()
|
||||||
|
.resolve_call_path(func)
|
||||||
|
.is_some_and(|call_path| matches!(call_path.as_slice(), ["tempfile", ..]))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
|
HardcodedTempFile {
|
||||||
|
string: value.to_string(),
|
||||||
|
},
|
||||||
|
expr.range(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue