diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM910.py b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM910.py index e5aa9a97ed..41d8bf69c1 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM910.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM910.py @@ -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, +) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs index 58860dc56e..24429d5f12 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs @@ -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, + )); } diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM910_SIM910.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM910_SIM910.py.snap index 49b1b6a953..f48c7bfc95 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM910_SIM910.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM910_SIM910.py.snap @@ -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 diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM910_SIM910.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM910_SIM910.py.snap index 79e12c5d8b..7daebe91d0 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM910_SIM910.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM910_SIM910.py.snap @@ -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