[flake8-simplify] Make fix unsafe if it deletes comments (SIM910) (#22662)

<!--
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

<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan

<!-- How was it tested? -->
This commit is contained in:
chiri
2026-01-20 19:53:08 +03:00
committed by GitHub
parent 8b5fbc240a
commit a6752942ad
4 changed files with 71 additions and 4 deletions

View File

@@ -82,3 +82,10 @@ def get_key():
ages = {"Tom": 23, "Maria": 23, "Dog": 11}
age = ages.get(get_key(), None)
age = ages.get(
"Cat",
# text
None,
)

View File

@@ -1,3 +1,4 @@
use ruff_diagnostics::Applicability;
use ruff_python_ast::{self as ast, Arguments, Expr, str_prefix::StringLiteralPrefix};
use ruff_text_size::{Ranged, TextRange};
@@ -89,6 +90,9 @@ impl Violation for UncapitalizedEnvironmentVariables {
/// age = ages.get("Cat")
/// ```
///
/// ## Fix safety
/// This rule's fix is marked as safe, unless the expression contains comments.
///
/// ## References
/// - [Python documentation: `dict.get`](https://docs.python.org/3/library/stdtypes.html#dict.get)
#[derive(ViolationMetadata)]
@@ -310,8 +314,15 @@ pub(crate) fn dict_get_with_none_default(checker: &Checker, expr: &Expr) {
},
expr.range(),
);
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
expected,
expr.range(),
)));
let applicability = if checker.comment_ranges().intersects(expr.range()) {
Applicability::Unsafe
} else {
Applicability::Safe
};
diagnostic.set_fix(Fix::applicable_edit(
Edit::range_replacement(expected, expr.range()),
applicability,
));
}

View File

@@ -170,3 +170,26 @@ help: Replace `dict.get("Cat", None)` with `dict.get("Cat")`
58 |
59 |
60 | # https://github.com/astral-sh/ruff/issues/20341
SIM910 [*] Use `dict.get()` without default value
--> SIM910.py:87:7
|
87 | age = ages.get(
| _______^
88 | | "Cat",
89 | | # text
90 | | None,
91 | | )
| |_^
|
help: Remove default value
84 | age = ages.get(get_key(), None)
85 |
86 |
- age = ages.get(
- "Cat",
- # text
- None,
- )
87 + age = ages.get("Cat")
note: This is an unsafe fix and may change runtime behavior

View File

@@ -244,3 +244,29 @@ help: Replace `ages.get(get_key(), None)` with `ages.get(get_key())`
83 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}
- age = ages.get(get_key(), None)
84 + age = ages.get(get_key())
85 |
86 |
87 | age = ages.get(
SIM910 [*] Use `dict.get()` without default value
--> SIM910.py:87:7
|
87 | age = ages.get(
| _______^
88 | | "Cat",
89 | | # text
90 | | None,
91 | | )
| |_^
|
help: Remove default value
84 | age = ages.get(get_key(), None)
85 |
86 |
- age = ages.get(
- "Cat",
- # text
- None,
- )
87 + age = ages.get("Cat")
note: This is an unsafe fix and may change runtime behavior