Skip to content

RUF072: skip formfeeds on dedent#24308

Merged
MichaReiser merged 7 commits intoastral-sh:mainfrom
second-ed:fix/ruff_24246_formfeed_indent
Apr 2, 2026
Merged

RUF072: skip formfeeds on dedent#24308
MichaReiser merged 7 commits intoastral-sh:mainfrom
second-ed:fix/ruff_24246_formfeed_indent

Conversation

@second-ed
Copy link
Copy Markdown
Contributor

Summary

fixes: #24246

Skips the formfeed on the dedent if the line starts with the formfeed and then readds it back to the start of the line.

Test Plan

Added bug report example to snapshot test cases

@astral-sh-bot astral-sh-bot bot requested a review from ntBre March 30, 2026 14:34
@MichaReiser MichaReiser changed the title [RUF072 useless-finally] skip formfeeds on dedent #24246 RUF072: skip formfeeds on dedent Mar 30, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 30, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Comment on lines +74 to +80
# Bare try finally with line starting with a formfeed
try:
1
2
finally:
pass

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll do a full review later. Can you add the new test at the end? It reduces the snapshot diff size, which makes the changes easier to review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah sure, I'll make those changes and push up, I was trying to keep the error cases and ok cases together but agree it makes the diff horrible to read

@MichaReiser MichaReiser added bug Something isn't working fixes Related to suggested fixes for violations labels Mar 30, 2026
/// Lines that are indented by _less_ than the indent of the first line
/// are left unchanged.
///
/// Lines that consist solely of whitespace are trimmed to a blank line.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's update the documentation to explain how form feeds are handled


#[test]
#[rustfmt::skip]
fn dedent_to_handles_when_multiple_leading_form_feeds_greater_than_dedent_len() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here's another test case that currently panics:

    #[test]
    #[rustfmt::skip]
    fn dedent_to_ignores_leading_form_feeds_when_checking_indentation() {
        let x = [
            "    1",
            "\x0C\x0C  2",
        ].join("\n");
        let y = [
            "1",
            "\x0C\x0C  2",
        ].join("\n");
        assert_eq!(dedent_to(&x, ""), Some(y));
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Cheers, I'll have a look at this after work and see if I can find more test cases to sure it up

" baz"
].join("\n");
let first_result = dedent_to(&x, " ").unwrap();
assert_eq!(dedent_to(&first_result, " "), Some(y));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I found two more failing test cases

#[test]
    #[rustfmt::skip]
    fn dedent_to_preserves_less_indented_later_line() {
        let x = [
            "   foo\n",
            "  bar\n",
        ].join("");
        let y = [
            "foo\n",
            "  bar\n",
        ].join("");
        assert_eq!(dedent_to(&x, ""), Some(y));
    }

    #[test]
    #[rustfmt::skip]
    fn dedent_to_preserves_less_indented_later_line_with_crlf() {
        let x = [
            "   foo\r\n",
            "  bar\r\n",
        ].join("");
        let y = [
            "foo\r\n",
            "  bar\r\n",
        ].join("");
        assert_eq!(dedent_to(&x, ""), Some(y));
    }

@second-ed second-ed marked this pull request as draft March 31, 2026 21:05
@second-ed second-ed marked this pull request as ready for review April 1, 2026 15:42
@second-ed second-ed requested a review from MichaReiser April 2, 2026 10:34
Copy link
Copy Markdown
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Thank you. This turned out to be trickier than I thought.

@MichaReiser MichaReiser force-pushed the fix/ruff_24246_formfeed_indent branch from 311bea1 to ced2ffe Compare April 2, 2026 12:21
@second-ed
Copy link
Copy Markdown
Contributor Author

cheers for your help it was a fun one 😄

@MichaReiser MichaReiser merged commit 37f5d61 into astral-sh:main Apr 2, 2026
48 checks passed
carljm added a commit that referenced this pull request Apr 2, 2026
* main:
  Add a "release-gate" step to the release workflow (#24365)
  Disallow starred expressions as values of starred expressions (#24280)
  [`pyupgrade`] Ignore strings with string-only escapes (`UP012`) (#16058)
  [ty] Improve consistency and quality of diagnostics relating to invalid type forms (#24325)
  [flake8-type-checking] Clarify import cycle wording for TC001/TC002/TC003 (#24322)
  [`flake8-errmsg`] Avoid shadowing existing `msg` in fix for `EM101` (#24363)
  `RUF072`: skip formfeeds on dedent (#24308)
  Replace unmaintained `unic-ucd-category` crate with `icu_properties` (#24344)
  [ty] Replace markdown hard line breaks in snapshot tests (#24361)
  [ty] Move snapshot for code action test with trailing whitespace to external file (#24359)
@second-ed second-ed deleted the fix/ruff_24246_formfeed_indent branch April 2, 2026 16:31
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.

Form feeds make the RUF072 fix introduce syntax errors

3 participants