[pyupgrade] Make fix unsafe if it deletes comments (UP004) (#18393)

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary
https://github.com/astral-sh/ruff/issues/18387#issuecomment-2923039331
<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan
update snapshots
<!-- How was it tested? -->
This commit is contained in:
chiri 2025-06-03 16:09:33 +03:00 committed by GitHub
parent f23d2c9b9e
commit 628bb2cd1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 14 deletions

View file

@ -1,3 +1,4 @@
use ruff_diagnostics::Applicability;
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast as ast;
use ruff_text_size::Ranged;
@ -61,14 +62,23 @@ pub(crate) fn useless_object_inheritance(checker: &Checker, class_def: &ast::Stm
},
base.range(),
);
diagnostic.try_set_fix(|| {
remove_argument(
let edit = remove_argument(
base,
arguments,
Parentheses::Remove,
checker.locator().contents(),
)
.map(Fix::safe_edit)
)?;
let range = edit.range();
let applicability = if checker.comment_ranges().intersects(range) {
Applicability::Unsafe
} else {
Applicability::Safe
};
Ok(Fix::applicable_edit(edit, applicability))
});
}
}

View file

@ -51,7 +51,7 @@ UP004.py:16:5: UP004 [*] Class `A` inherits from `object`
|
= help: Remove `object` inheritance
Safe fix
Unsafe fix
12 12 | ...
13 13 |
14 14 |
@ -75,7 +75,7 @@ UP004.py:24:5: UP004 [*] Class `A` inherits from `object`
|
= help: Remove `object` inheritance
Safe fix
Unsafe fix
19 19 | ...
20 20 |
21 21 |
@ -99,7 +99,7 @@ UP004.py:31:5: UP004 [*] Class `A` inherits from `object`
|
= help: Remove `object` inheritance
Safe fix
Unsafe fix
26 26 | ...
27 27 |
28 28 |
@ -122,7 +122,7 @@ UP004.py:37:5: UP004 [*] Class `A` inherits from `object`
|
= help: Remove `object` inheritance
Safe fix
Unsafe fix
33 33 | ...
34 34 |
35 35 |
@ -146,7 +146,7 @@ UP004.py:45:5: UP004 [*] Class `A` inherits from `object`
|
= help: Remove `object` inheritance
Safe fix
Unsafe fix
40 40 | ...
41 41 |
42 42 |
@ -171,7 +171,7 @@ UP004.py:53:5: UP004 [*] Class `A` inherits from `object`
|
= help: Remove `object` inheritance
Safe fix
Unsafe fix
48 48 | ...
49 49 |
50 50 |
@ -196,7 +196,7 @@ UP004.py:61:5: UP004 [*] Class `A` inherits from `object`
|
= help: Remove `object` inheritance
Safe fix
Unsafe fix
56 56 | ...
57 57 |
58 58 |
@ -221,7 +221,7 @@ UP004.py:69:5: UP004 [*] Class `A` inherits from `object`
|
= help: Remove `object` inheritance
Safe fix
Unsafe fix
64 64 | ...
65 65 |
66 66 |
@ -320,7 +320,7 @@ UP004.py:98:5: UP004 [*] Class `B` inherits from `object`
|
= help: Remove `object` inheritance
Safe fix
Unsafe fix
95 95 |
96 96 |
97 97 | class B(
@ -381,7 +381,7 @@ UP004.py:125:5: UP004 [*] Class `A` inherits from `object`
|
= help: Remove `object` inheritance
Safe fix
Unsafe fix
121 121 | ...
122 122 |
123 123 |
@ -403,7 +403,7 @@ UP004.py:131:5: UP004 [*] Class `A` inherits from `object`
|
= help: Remove `object` inheritance
Safe fix
Unsafe fix
127 127 | ...
128 128 |
129 129 |