diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TC006.py b/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TC006.py index 0880cc2129..cf10553c49 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TC006.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TC006.py @@ -80,3 +80,12 @@ def f(): from typing import cast, Annotated, Literal cast(list[Annotated["list['Literal[\"A\"]']", "Foo"]], ['A']) + + +def f(): + from typing import cast + + cast( + int # TC006 + , 6.0 + ) diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/rules/runtime_cast_value.rs b/crates/ruff_linter/src/rules/flake8_type_checking/rules/runtime_cast_value.rs index 27593b6a16..f2de99c612 100644 --- a/crates/ruff_linter/src/rules/flake8_type_checking/rules/runtime_cast_value.rs +++ b/crates/ruff_linter/src/rules/flake8_type_checking/rules/runtime_cast_value.rs @@ -69,10 +69,7 @@ pub(crate) fn runtime_cast_value(checker: &mut Checker, type_expr: &Expr) { checker.stylist(), checker.locator(), ); - if checker - .comment_ranges() - .has_comments(type_expr, checker.source()) - { + if checker.comment_ranges().intersects(type_expr.range()) { diagnostic.set_fix(Fix::unsafe_edit(edit)); } else { diagnostic.set_fix(Fix::safe_edit(edit)); diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/rules/type_alias_quotes.rs b/crates/ruff_linter/src/rules/flake8_type_checking/rules/type_alias_quotes.rs index 9dd36dec6f..980fb06a5d 100644 --- a/crates/ruff_linter/src/rules/flake8_type_checking/rules/type_alias_quotes.rs +++ b/crates/ruff_linter/src/rules/flake8_type_checking/rules/type_alias_quotes.rs @@ -289,10 +289,7 @@ pub(crate) fn quoted_type_alias( let range = annotation_expr.range(); let mut diagnostic = Diagnostic::new(QuotedTypeAlias, range); let edit = Edit::range_replacement(annotation_expr.value.to_string(), range); - if checker - .comment_ranges() - .has_comments(expr, checker.source()) - { + if checker.comment_ranges().intersects(range) { diagnostic.set_fix(Fix::unsafe_edit(edit)); } else { diagnostic.set_fix(Fix::safe_edit(edit)); diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-cast-value_TC006.py.snap b/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-cast-value_TC006.py.snap index ca5dc6f6c1..25348b34ab 100644 --- a/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-cast-value_TC006.py.snap +++ b/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-cast-value_TC006.py.snap @@ -190,3 +190,25 @@ TC006.py:82:10: TC006 [*] Add quotes to type expression in `typing.cast()` 81 81 | 82 |- cast(list[Annotated["list['Literal[\"A\"]']", "Foo"]], ['A']) 82 |+ cast("list[Annotated[list[Literal['A']], 'Foo']]", ['A']) +83 83 | +84 84 | +85 85 | def f(): + +TC006.py:89:9: TC006 [*] Add quotes to type expression in `typing.cast()` + | +88 | cast( +89 | int # TC006 + | ^^^ TC006 +90 | , 6.0 +91 | ) + | + = help: Add quotes + +ℹ Safe fix +86 86 | from typing import cast +87 87 | +88 88 | cast( +89 |- int # TC006 + 89 |+ "int" # TC006 +90 90 | , 6.0 +91 91 | )