Skip to content

Commit 17ef711

Browse files
committed
Stabilize --add-ignore (#27125)
Summary -- This makes sense to accompany `ruff: ignore` comments being stabilized. The preview gate has been pushed down to cover only the names vs codes decision. I'll flag this as a comment, but I wasn't totally sure about the LSP case. I left using `ruff: ignore` in its suppression edits preview-gated for now. Test Plan -- Existing tests, dropping one that showed the preview error with `--add-ignore`. I left `--preview` in many of the tests to continue testing the human-readable names but removed it from one.
1 parent ef912bb commit 17ef711

7 files changed

Lines changed: 28 additions & 44 deletions

File tree

‎crates/ruff/src/args.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,8 @@ pub struct CheckCommand {
468468
)]
469469
pub add_noqa: Option<String>,
470470
/// Enable automatic additions of `ruff: ignore` comments to failing lines.
471-
/// Optionally provide a reason to append after the rule names.
472-
/// Requires preview mode.
471+
/// Optionally provide a reason to append after the codes.
472+
/// In preview, add suppression comments with rule names instead.
473473
#[arg(
474474
long,
475475
value_name = "REASON",

‎crates/ruff/src/commands/add_noqa.rs‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::path::PathBuf;
22
use std::time::Instant;
33

4-
use anyhow::{Result, bail};
4+
use anyhow::Result;
55
use log::{debug, error};
66
#[cfg(not(target_family = "wasm"))]
77
use rayon::prelude::*;
88

99
use ruff_linter::SuppressionKind;
1010
use ruff_linter::linter::add_suppressions_to_path;
11-
use ruff_linter::preview::is_human_readable_names_enabled;
1211
use ruff_linter::source_kind::SourceKind;
1312
use ruff_linter::warn_user_once;
1413
use ruff_python_ast::{PySourceType, SourceType};
@@ -80,14 +79,6 @@ pub(crate) fn add_noqa(
8079
{
8180
return Ok(0);
8281
}
83-
if matches!(suppression_kind, SuppressionKind::Ignore)
84-
&& !is_human_readable_names_enabled(settings.linter.preview)
85-
{
86-
bail!(
87-
"`--add-ignore` requires preview mode, but preview is disabled for `{}`",
88-
path.display()
89-
);
90-
}
9182
let source_kind = match SourceKind::from_path(path, source_type) {
9283
Ok(Some(source_kind)) => source_kind,
9384
Ok(None) => return Ok(0),

‎crates/ruff/tests/cli/lint.rs‎

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,7 +2614,6 @@ fn add_ignore() -> Result<()> {
26142614
fixture
26152615
.check_command()
26162616
.arg("--select=RUF015")
2617-
.arg("--preview")
26182617
.arg("--add-ignore"),
26192618
@"
26202619
success: true
@@ -2633,40 +2632,13 @@ fn add_ignore() -> Result<()> {
26332632
@"
26342633
26352634
def first_square():
2636-
return [x * x for x in range(20)][0] # ruff: ignore[unnecessary-iterable-allocation-for-first-element]
2635+
return [x * x for x in range(20)][0] # ruff: ignore[RUF015]
26372636
",
26382637
);
26392638

26402639
Ok(())
26412640
}
26422641

2643-
#[test]
2644-
fn add_ignore_requires_preview() -> Result<()> {
2645-
let fixture = CliTest::new()?;
2646-
fixture.write_file("noqa.py", "import os\n")?;
2647-
2648-
assert_cmd_snapshot!(
2649-
fixture
2650-
.check_command()
2651-
.arg("--select=F401")
2652-
.arg("--add-ignore"),
2653-
@"
2654-
success: false
2655-
exit_code: 2
2656-
----- stdout -----
2657-
2658-
----- stderr -----
2659-
ruff failed
2660-
Cause: `--add-ignore` requires preview mode, but preview is disabled for `[TMP]/noqa.py`
2661-
",
2662-
);
2663-
2664-
let test_code = fixture.read_file("noqa.py")?;
2665-
insta::assert_snapshot!(test_code, @"import os");
2666-
2667-
Ok(())
2668-
}
2669-
26702642
#[test]
26712643
fn add_noqa_existing_ignore() -> Result<()> {
26722644
let fixture = CliTest::new()?;

‎crates/ruff_linter/src/linter.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ pub fn add_suppressions_to_path(
436436
reason,
437437
&suppressions,
438438
suppression_kind,
439+
settings.preview,
439440
)
440441
}
441442

‎crates/ruff_linter/src/noqa.rs‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ use rustc_hash::FxHashSet;
1919
use crate::Edit;
2020
use crate::Locator;
2121
use crate::fs::relativize_path;
22+
use crate::preview::is_human_readable_names_enabled;
2223
use crate::registry::Rule;
2324
use crate::rule_redirects::get_redirect_target;
25+
use crate::settings::types::PreviewMode;
2426
use crate::suppression::{self, Suppressions};
2527

2628
/// Generates an array of edits that matches the length of `diagnostics`.
@@ -39,6 +41,7 @@ pub fn generate_suppression_edits(
3941
line_ending: LineEnding,
4042
suppressions: &Suppressions,
4143
suppression_kind: SuppressionKind,
44+
preview: PreviewMode,
4245
) -> Vec<Option<Edit>> {
4346
let file_directives = FileNoqaDirectives::extract(locator, comment_ranges, external, path);
4447
let exemption = FileExemption::from(&file_directives);
@@ -51,6 +54,7 @@ pub fn generate_suppression_edits(
5154
noqa_line_for,
5255
suppressions,
5356
suppression_kind,
57+
preview,
5458
);
5559
build_suppression_edits_by_diagnostic(comments, locator, line_ending, None, suppression_kind)
5660
}
@@ -780,6 +784,7 @@ pub(crate) fn add_suppression(
780784
reason: Option<&str>,
781785
suppressions: &Suppressions,
782786
suppression_kind: SuppressionKind,
787+
preview: PreviewMode,
783788
) -> Result<usize> {
784789
let (count, output) = add_suppression_inner(
785790
path,
@@ -792,6 +797,7 @@ pub(crate) fn add_suppression(
792797
reason,
793798
suppressions,
794799
suppression_kind,
800+
preview,
795801
);
796802

797803
fs::write(path, output)?;
@@ -810,6 +816,7 @@ fn add_suppression_inner(
810816
reason: Option<&str>,
811817
suppressions: &Suppressions,
812818
suppression_kind: SuppressionKind,
819+
preview: PreviewMode,
813820
) -> (usize, String) {
814821
let mut count = 0;
815822

@@ -827,6 +834,7 @@ fn add_suppression_inner(
827834
noqa_line_for,
828835
suppressions,
829836
suppression_kind,
837+
preview,
830838
);
831839

832840
let edits =
@@ -938,6 +946,7 @@ impl Ranged for ExistingDirective<'_> {
938946
}
939947
}
940948

949+
#[expect(clippy::too_many_arguments)]
941950
fn find_suppression_comments<'a>(
942951
diagnostics: &'a [Diagnostic],
943952
locator: &'a Locator,
@@ -946,6 +955,7 @@ fn find_suppression_comments<'a>(
946955
noqa_line_for: &NoqaMapping,
947956
suppressions: &'a Suppressions,
948957
suppression_kind: SuppressionKind,
958+
preview: PreviewMode,
949959
) -> Vec<Option<SuppressionComment<'a>>> {
950960
// List of suppression comments, ordered to match up with `messages`
951961
let mut comments_by_line: Vec<Option<SuppressionComment<'a>>> = vec![];
@@ -1023,7 +1033,8 @@ fn find_suppression_comments<'a>(
10231033

10241034
let identifier = match suppression_kind {
10251035
SuppressionKind::Noqa => code.as_str(),
1026-
SuppressionKind::Ignore => message.name(),
1036+
SuppressionKind::Ignore if is_human_readable_names_enabled(preview) => message.name(),
1037+
SuppressionKind::Ignore => code.as_str(),
10271038
};
10281039

10291040
comments_by_line.push(Some(SuppressionComment {
@@ -1368,6 +1379,7 @@ mod tests {
13681379
use crate::rules::pycodestyle::rules::{AmbiguousVariableName, UselessSemicolon};
13691380
use crate::rules::pyflakes::rules::UnusedVariable;
13701381
use crate::rules::pyupgrade::rules::PrintfStringFormatting;
1382+
use crate::settings::types::PreviewMode;
13711383
use crate::settings::{LinterSettings, flags};
13721384
use crate::source_kind::SourceKind;
13731385
use crate::suppression::Suppressions;
@@ -1423,6 +1435,7 @@ mod tests {
14231435
None,
14241436
&suppressions,
14251437
suppression_kind,
1438+
settings.preview,
14261439
)
14271440
}
14281441

@@ -3328,6 +3341,7 @@ mod tests {
33283341
None,
33293342
&Suppressions::default(),
33303343
SuppressionKind::Noqa,
3344+
PreviewMode::Disabled,
33313345
);
33323346
assert_eq!(count, 0);
33333347
assert_eq!(output, format!("{contents}"));
@@ -3354,6 +3368,7 @@ mod tests {
33543368
None,
33553369
&Suppressions::default(),
33563370
SuppressionKind::Noqa,
3371+
PreviewMode::Disabled,
33573372
);
33583373
assert_eq!(count, 1);
33593374
assert_eq!(output, "x = 1 # noqa: F841\n");
@@ -3387,6 +3402,7 @@ mod tests {
33873402
None,
33883403
&Suppressions::default(),
33893404
SuppressionKind::Noqa,
3405+
PreviewMode::Disabled,
33903406
);
33913407
assert_eq!(count, 1);
33923408
assert_eq!(output, "x = 1 # noqa: E741, F841\n");
@@ -3420,6 +3436,7 @@ mod tests {
34203436
None,
34213437
&Suppressions::default(),
34223438
SuppressionKind::Noqa,
3439+
PreviewMode::Disabled,
34233440
);
34243441
assert_eq!(count, 0);
34253442
assert_eq!(output, "x = 1 # noqa");
@@ -3453,6 +3470,7 @@ print(
34533470
LineEnding::Lf,
34543471
&suppressions,
34553472
SuppressionKind::Noqa,
3473+
PreviewMode::Disabled,
34563474
);
34573475
assert_eq!(
34583476
edits,
@@ -3487,6 +3505,7 @@ bar =
34873505
LineEnding::Lf,
34883506
&suppressions,
34893507
SuppressionKind::Noqa,
3508+
PreviewMode::Disabled,
34903509
);
34913510
assert_eq!(
34923511
edits,

‎crates/ruff_server/src/lint.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ pub(crate) fn check(
162162
} else {
163163
SuppressionKind::Noqa
164164
},
165+
settings.linter.preview,
165166
);
166167
let context = LspDiagnosticContext {
167168
source_kind: &source_kind,

‎docs/configuration.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,8 @@ Options:
645645
Optionally provide a reason to append after the codes
646646
--add-ignore[=<REASON>]
647647
Enable automatic additions of `ruff: ignore` comments to failing
648-
lines. Optionally provide a reason to append after the rule names.
649-
Requires preview mode
648+
lines. Optionally provide a reason to append after the codes. In
649+
preview, add suppression comments with rule names instead
650650
--show-files
651651
See the files Ruff will be run against with the current settings
652652
--show-settings

0 commit comments

Comments
 (0)