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

Use full range for SIM105 fixes #7221

Merged
merged 1 commit into from
Sep 7, 2023
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
12 changes: 11 additions & 1 deletion crates/ruff/resources/test/fixtures/flake8_simplify/SIM105_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def foo():
except ValueError:
pass


# SIM105
try:
foo()
Expand Down Expand Up @@ -111,10 +112,19 @@ def with_comment():
except "not an exception":
pass


# Regression test for: https://github.com/astral-sh/ruff/issues/7123
def write_models(directory, Models):
try:
os.makedirs(model_dir);
except OSError:
pass;

try: os.makedirs(model_dir);
except OSError:
pass;

try: os.makedirs(model_dir);
except OSError:
pass; \
\
#
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use ruff_python_ast::{self as ast, Constant, ExceptHandler, Expr, Stmt};
use ruff_text_size::{TextLen, TextRange};

use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::compose_call_path;
use ruff_python_ast::helpers;
use ruff_python_ast::{self as ast, Constant, ExceptHandler, Expr, Stmt};
use ruff_text_size::Ranged;
use ruff_text_size::{TextLen, TextRange};

use crate::checkers::ast::Checker;
use crate::importer::ImportRequest;
Expand Down Expand Up @@ -134,12 +133,10 @@ pub(crate) fn suppressible_exception(
stmt.range(),
);
if checker.patch(diagnostic.kind.rule()) {
if !checker.indexer().has_comments(stmt, checker.locator())
&& !checker
.indexer()
.in_multi_statement_line(stmt, checker.locator())
{
if !checker.indexer().has_comments(stmt, checker.locator()) {
diagnostic.try_set_fix(|| {
// let range = statement_range(stmt, checker.locator(), checker.indexer());

let (import_edit, binding) = checker.importer().get_or_import_symbol(
&ImportRequest::import("contextlib", "suppress"),
stmt.start(),
Expand All @@ -149,8 +146,8 @@ pub(crate) fn suppressible_exception(
format!("with {binding}({exception})"),
TextRange::at(stmt.start(), "try".text_len()),
);
let handler_line_begin = checker.locator().line_start(range.start());
let remove_handler = Edit::deletion(handler_line_begin, range.end());
let remove_handler =
Edit::range_deletion(checker.locator().full_lines_range(*range));
Ok(Fix::suggested_edits(
import_edit,
[replace_try, remove_handler],
Expand Down
Loading
Loading