Skip to content

[flake8-implicit-str-concat] Mark fix unsafe when it creates a docstring (ISC003) - #27981

Merged
ntBre merged 3 commits into
astral-sh:mainfrom
mdiniz97:fix/isc003-docstring-safety
Sep 3, 2026
Merged

ntBre merged 3 commits into
astral-sh:mainfrom
mdiniz97:fix/isc003-docstring-safety

Conversation

@mdiniz97

Copy link
Copy Markdown
Contributor

Fixes #27979

Removing the + from the first statement of a module, function, or class body turns the concatenation into a docstring, changing __doc__ and program behavior. Detect docstring positions and emit the fix with unsafe safety level instead.

  • Concats of plain string literals only (f-strings, bytes, t-strings can never be docstrings)
  • Covers module, function, class, and method bodies
  • Safe fix behavior unchanged for non-docstring positions

Test Plan

cargo test -p ruff_linter --lib (2813 pass), clippy + fmt clean

@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre August 22, 2026 20:07

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks reasonable overall. I just had a few comments on the tests and a couple of suggestions on simplifying the implementation.

Comment thread crates/ruff_linter/src/rules/flake8_implicit_str_concat/rules/explicit.rs Outdated
Comment thread crates/ruff_linter/src/rules/flake8_implicit_str_concat/rules/explicit.rs Outdated
Comment thread crates/ruff_linter/src/rules/flake8_implicit_str_concat/rules/explicit.rs Outdated
Comment thread crates/ruff_linter/resources/test/fixtures/flake8_implicit_str_concat/ISC.py Outdated
Comment thread crates/ruff_linter/resources/test/fixtures/flake8_implicit_str_concat/ISC.py Outdated
@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Aug 28, 2026
Removing '+' from the first statement of a module, function, or class
body turns the concatenation into a docstring, changing __doc__ and
program behavior. Detect docstring positions and emit the fix with
unsafe applicability instead.

Closes astral-sh#27979
@mdiniz97

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @ntBre! All suggestions addressed:

  • explicit.rs: use current_statement() / current_statement_parent() directly instead of retrieving ids
  • explicit.rs: use Applicability + Fix::applicable_edit instead of if/else on safe/unsafe edit constructors
  • Tests: moved all fix-safety cases to a dedicated ISC003_docstring.py fixture so the module case is genuinely in a docstring position (first statement of the module body), and moved the f-string / bytes / nested-expression negatives into docstring positions so they exercise the "right position, wrong type" path

Verified locally: cargo test -p ruff_linter --lib (2814 pass), clippy and fmt clean, and the binary keeps the fix hidden under --fix for module/function/class/method docstring positions while applying it for the negative cases.

@ntBre ntBre changed the title Mark ISC003 fix as unsafe when it creates a docstring [flake8-implicit-str-concat] Mark fix unsafe when it creates a docstring (ISC003) Sep 3, 2026

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I pushed a small commit trimming down some comments, removing an extra newline, and adding fix safety docs, but this looked good to me.

@ntBre
ntBre enabled auto-merge (squash) September 3, 2026 19:00
@astral-sh-bot

astral-sh-bot Bot commented Sep 3, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre
ntBre merged commit e794f53 into astral-sh:main Sep 3, 2026
48 of 49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ISC003 fix is not safe when it creates a docstring

2 participants