From 2e3757a19edec66c0ea12005836f09a4157989d1 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 5 Nov 2023 15:13:27 -0500 Subject: [PATCH 1/2] Add warning --- crates/ruff_cli/src/commands/format.rs | 10 ++++++++++ .../resources/test/fixtures/pycodestyle/E70.py | 2 ++ .../src/rules/pycodestyle/rules/compound_statements.rs | 5 +++-- ...linter__rules__pycodestyle__tests__E703_E70.py.snap | 4 ++++ docs/formatter.md | 1 + 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/ruff_cli/src/commands/format.rs b/crates/ruff_cli/src/commands/format.rs index 2db1338d4d9b0..6aa2eabc1bba9 100644 --- a/crates/ruff_cli/src/commands/format.rs +++ b/crates/ruff_cli/src/commands/format.rs @@ -726,6 +726,16 @@ pub(super) fn warn_incompatible_formatter_settings( for setting in std::iter::once(&pyproject_config.settings) .chain(resolver.iter().flat_map(|resolver| resolver.settings())) { + // Validate rules that only conflict in preview. + if setting + .linter + .rules + .enabled(Rule::MultipleStatementsOnOneLineColon) + && setting.formatter.preview.is_enabled() + { + warn_user_once!("The formatter's preview style is incompatible with `E701`, which lints against the use of colons to put multiple statements on the same line."); + } + // Validate all rules that rely on tab styles. if setting.linter.rules.enabled(Rule::TabIndentation) && setting.formatter.indent_style.is_tab() diff --git a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E70.py b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E70.py index 861a99cb9dd91..45d1941d22cb1 100644 --- a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E70.py +++ b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E70.py @@ -69,3 +69,5 @@ class Foo: #: E701:2:3 a = \ 5; +#: +with x(y) as z: ... diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/compound_statements.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/compound_statements.rs index a8c8dd3d287b1..f1f038ae66c00 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/compound_statements.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/compound_statements.rs @@ -206,12 +206,13 @@ pub(crate) fn compound_statements( { colon = Some((range.start(), range.end())); - // Allow `class C: ...`-style definitions in stubs. - allow_ellipsis = class.is_some(); + // Allow `class C: ...`-style definitions. + allow_ellipsis = true; } } Tok::Semi => { semi = Some((range.start(), range.end())); + allow_ellipsis = false; } Tok::Comment(..) | Tok::Indent | Tok::Dedent | Tok::NonLogicalNewline => {} _ => { diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E703_E70.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E703_E70.py.snap index 798febf2364a0..3d3c378dcc314 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E703_E70.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E703_E70.py.snap @@ -92,6 +92,8 @@ E70.py:71:4: E703 [*] Statement ends with an unnecessary semicolon 70 | a = \ 71 | 5; | ^ E703 +72 | #: +73 | with x(y) as z: ... | = help: Remove unnecessary semicolon @@ -101,5 +103,7 @@ E70.py:71:4: E703 [*] Statement ends with an unnecessary semicolon 70 70 | a = \ 71 |- 5; 71 |+ 5 +72 72 | #: +73 73 | with x(y) as z: ... diff --git a/docs/formatter.md b/docs/formatter.md index caa7459e55b3d..f5debd9c5425a 100644 --- a/docs/formatter.md +++ b/docs/formatter.md @@ -202,6 +202,7 @@ When using Ruff as a formatter, we recommend avoiding the following lint rules: - [`indentation-with-invalid-multiple`](rules/indentation-with-invalid-multiple.md) (`E111`) - [`indentation-with-invalid-multiple-comment`](rules/indentation-with-invalid-multiple-comment.md) (`E114`) - [`over-indented`](rules/over-indented.md) (`E117`) +- [`multiple-statements-on-one-line-colon`](rules/multiple-statements-on-one-line-colon.md) (`E701`) - [`indent-with-spaces`](rules/indent-with-spaces.md) (`D206`) - [`triple-single-quotes`](rules/triple-single-quotes.md) (`D300`) - [`bad-quotes-inline-string`](rules/bad-quotes-inline-string.md) (`Q000`) From ed4f37a89f388555b8c34ed6cb95c4e86d7aa2e9 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 5 Nov 2023 19:22:19 -0500 Subject: [PATCH 2/2] Revert --- crates/ruff_cli/src/commands/format.rs | 10 ---------- .../src/rules/pycodestyle/rules/compound_statements.rs | 4 +++- docs/formatter.md | 1 - 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/crates/ruff_cli/src/commands/format.rs b/crates/ruff_cli/src/commands/format.rs index 6aa2eabc1bba9..2db1338d4d9b0 100644 --- a/crates/ruff_cli/src/commands/format.rs +++ b/crates/ruff_cli/src/commands/format.rs @@ -726,16 +726,6 @@ pub(super) fn warn_incompatible_formatter_settings( for setting in std::iter::once(&pyproject_config.settings) .chain(resolver.iter().flat_map(|resolver| resolver.settings())) { - // Validate rules that only conflict in preview. - if setting - .linter - .rules - .enabled(Rule::MultipleStatementsOnOneLineColon) - && setting.formatter.preview.is_enabled() - { - warn_user_once!("The formatter's preview style is incompatible with `E701`, which lints against the use of colons to put multiple statements on the same line."); - } - // Validate all rules that rely on tab styles. if setting.linter.rules.enabled(Rule::TabIndentation) && setting.formatter.indent_style.is_tab() diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/compound_statements.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/compound_statements.rs index f1f038ae66c00..6dcdd19c8b4ec 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/compound_statements.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/compound_statements.rs @@ -24,7 +24,7 @@ use ruff_source_file::Locator; /// if foo == "blah": /// do_blah_thing() /// ``` - +/// /// [PEP 8]: https://peps.python.org/pep-0008/#other-recommendations #[violation] pub struct MultipleStatementsOnOneLineColon; @@ -224,6 +224,7 @@ pub(crate) fn compound_statements( // Reset. semi = None; + allow_ellipsis = false; } if let Some((start, end)) = colon { @@ -246,6 +247,7 @@ pub(crate) fn compound_statements( try_ = None; while_ = None; with = None; + allow_ellipsis = false; } } } diff --git a/docs/formatter.md b/docs/formatter.md index f5debd9c5425a..caa7459e55b3d 100644 --- a/docs/formatter.md +++ b/docs/formatter.md @@ -202,7 +202,6 @@ When using Ruff as a formatter, we recommend avoiding the following lint rules: - [`indentation-with-invalid-multiple`](rules/indentation-with-invalid-multiple.md) (`E111`) - [`indentation-with-invalid-multiple-comment`](rules/indentation-with-invalid-multiple-comment.md) (`E114`) - [`over-indented`](rules/over-indented.md) (`E117`) -- [`multiple-statements-on-one-line-colon`](rules/multiple-statements-on-one-line-colon.md) (`E701`) - [`indent-with-spaces`](rules/indent-with-spaces.md) (`D206`) - [`triple-single-quotes`](rules/triple-single-quotes.md) (`D300`) - [`bad-quotes-inline-string`](rules/bad-quotes-inline-string.md) (`Q000`)