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

[ruff] Parenthesize fix when argument spans multiple lines for unnecessary-round (RUF057) #15703

Merged
merged 4 commits into from
Jan 24, 2025
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
17 changes: 17 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF057.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,20 @@
round(lorem, inferred_int) # No error
round(lorem, 3 + 4) # No error
round(lorem, foo) # No error

# Fixes should preserve parentheses when argument
# contains newline.
# See https://github.com/astral-sh/ruff/issues/15598
round(-
1)
round(1
*1
)

# fix should be unsafe if comment is in call range
round(# a comment
17
)
round(
17 # a comment
)
19 changes: 12 additions & 7 deletions crates/ruff_linter/src/rules/ruff/rules/unnecessary_round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ruff_python_ast::{Arguments, Expr, ExprCall, ExprNumberLiteral, Number};
use ruff_python_semantic::analyze::type_inference::{NumberLike, PythonType, ResolvedPythonType};
use ruff_python_semantic::analyze::typing;
use ruff_python_semantic::SemanticModel;
use ruff_source_file::find_newline;
use ruff_text_size::Ranged;

/// ## What it does
Expand Down Expand Up @@ -59,7 +60,7 @@ pub(crate) fn unnecessary_round(checker: &mut Checker, call: &ExprCall) {
return;
}

let applicability = match rounded_value {
let mut applicability = match rounded_value {
// ```python
// some_int: int
//
Expand All @@ -86,6 +87,10 @@ pub(crate) fn unnecessary_round(checker: &mut Checker, call: &ExprCall) {
_ => return,
};

if checker.comment_ranges().intersects(call.range()) {
applicability = Applicability::Unsafe;
};

let edit = unwrap_round_call(call, rounded, checker.semantic(), checker.locator());
let fix = Fix::applicable_edit(edit, applicability);

Expand Down Expand Up @@ -196,13 +201,13 @@ fn unwrap_round_call(
locator: &Locator,
) -> Edit {
let rounded_expr = locator.slice(rounded.range());

let has_parent_expr = semantic.current_expression_parent().is_some();
let new_content = if has_parent_expr || rounded.is_named_expr() {
format!("({rounded_expr})")
} else {
rounded_expr.to_string()
};
let new_content =
if has_parent_expr || rounded.is_named_expr() || find_newline(rounded_expr).is_some() {
format!("({rounded_expr})")
} else {
rounded_expr.to_string()
};

Edit::range_replacement(new_content, call.range)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/ruff_linter/src/rules/ruff/mod.rs
snapshot_kind: text
---
RUF057.py:6:1: RUF057 [*] Value being rounded is already an integer
|
Expand Down Expand Up @@ -181,3 +180,94 @@ RUF057.py:44:1: RUF057 [*] Value being rounded is already an integer
45 45 | round(inferred_int, -2) # No error
46 46 | round(inferred_int, inferred_int) # No error
47 47 | round(inferred_int, 3 + 4) # No error

RUF057.py:71:1: RUF057 [*] Value being rounded is already an integer
|
69 | # contains newline.
70 | # See https://github.com/astral-sh/ruff/issues/15598
71 | / round(-
72 | | 1)
| |__^ RUF057
73 | round(1
74 | *1
|
= help: Remove unnecessary `round` call

ℹ Safe fix
68 68 | # Fixes should preserve parentheses when argument
69 69 | # contains newline.
70 70 | # See https://github.com/astral-sh/ruff/issues/15598
71 |-round(-
71 |+(-
72 72 | 1)
73 73 | round(1
74 74 | *1

RUF057.py:73:1: RUF057 [*] Value being rounded is already an integer
|
71 | round(-
72 | 1)
73 | / round(1
74 | | *1
75 | | )
| |_^ RUF057
76 |
77 | # fix should be unsafe if comment is in call range
|
= help: Remove unnecessary `round` call

ℹ Safe fix
70 70 | # See https://github.com/astral-sh/ruff/issues/15598
71 71 | round(-
72 72 | 1)
73 |-round(1
74 |-*1
75 |-)
73 |+(1
74 |+*1)
76 75 |
77 76 | # fix should be unsafe if comment is in call range
78 77 | round(# a comment

RUF057.py:78:1: RUF057 [*] Value being rounded is already an integer
|
77 | # fix should be unsafe if comment is in call range
78 | / round(# a comment
79 | | 17
80 | | )
| |_^ RUF057
81 | round(
82 | 17 # a comment
|
= help: Remove unnecessary `round` call

ℹ Unsafe fix
75 75 | )
76 76 |
77 77 | # fix should be unsafe if comment is in call range
78 |-round(# a comment
79 78 | 17
80 |-)
81 79 | round(
82 80 | 17 # a comment
83 81 | )

RUF057.py:81:1: RUF057 [*] Value being rounded is already an integer
|
79 | 17
80 | )
81 | / round(
82 | | 17 # a comment
83 | | )
| |_^ RUF057
|
= help: Remove unnecessary `round` call

ℹ Unsafe fix
78 78 | round(# a comment
79 79 | 17
80 80 | )
81 |-round(
82 |- 17 # a comment
83 |-)
81 |+17
Loading