Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion crates/ruff_linter/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ pub(crate) const fn is_check_file_level_directives_enabled(settings: &LinterSett
}

// https://github.com/astral-sh/ruff/pull/17644
pub(crate) const fn is_readlines_in_for_fix_safe(settings: &LinterSettings) -> bool {
pub(crate) const fn is_readlines_in_for_fix_safe_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}

pub(crate) const fn multiple_with_statements_fix_safe_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/flake8_simplify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ mod tests {
}

#[test_case(Rule::IfElseBlockInsteadOfIfExp, Path::new("SIM108.py"))]
#[test_case(Rule::MultipleWithStatements, Path::new("SIM117.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"preview__{}_{}",
Expand Down
17 changes: 14 additions & 3 deletions crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use ruff_python_ast::{self as ast, Stmt, WithItem};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange};

use super::fix_with;
use crate::checkers::ast::Checker;
use crate::fix::edits::fits;

use super::fix_with;
use crate::preview::multiple_with_statements_fix_safe_enabled;

/// ## What it does
/// Checks for the unnecessary nesting of multiple consecutive context
Expand Down Expand Up @@ -45,8 +45,15 @@ use super::fix_with;
/// pass
/// ```
///
/// # Fix safety
///
/// This fix is marked as always unsafe unless [preview] mode is enabled, in which case it is always
/// marked as safe. Note that the fix is unavailable if it would remove comments (in either case).
///
/// ## References
/// - [Python documentation: The `with` statement](https://docs.python.org/3/reference/compound_stmts.html#the-with-statement)
///
/// [preview]: https://docs.astral.sh/ruff/preview/
#[derive(ViolationMetadata)]
pub(crate) struct MultipleWithStatements;

Expand Down Expand Up @@ -188,7 +195,11 @@ pub(crate) fn multiple_with_statements(
checker.settings.tab_size,
)
}) {
Ok(Some(Fix::unsafe_edit(edit)))
if multiple_with_statements_fix_safe_enabled(checker.settings) {
Ok(Some(Fix::safe_edit(edit)))
} else {
Ok(Some(Fix::unsafe_edit(edit)))
}
} else {
Ok(None)
}
Expand Down
Loading
Loading