Skip to content

Commit 46a55b2

Browse files
self-sasimeta-codesync[bot]
authored andcommitted
make --remove-unused-ignores a no-op for files with no unused ignores (#2217)
Summary: Fixes #2185 This change makes `--remove-unused-ignores` avoid rewriting files when there are no unused ignores to remove. Files that have no ignore comments, or only have used ignore comments, are now left untouched. This prevents unnecessary file writes and avoids introducing formatting-only changes such as added newlines. Pull Request resolved: #2217 Test Plan: Added tests to verify no newline characters are appended to input when there are no unused ignores to remove. Reviewed By: stroxler Differential Revision: D91474177 Pulled By: yangdanny97 fbshipit-source-id: 65651cf15219074f9edd77a086280609205e56a4
1 parent bdac377 commit 46a55b2

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

crates/pyrefly_python/src/ignore.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ impl Ignore {
401401
pub fn get(&self, line: &LineNumber) -> Option<&Vec<Suppression>> {
402402
self.ignores.get(line)
403403
}
404+
405+
/// Returns true if there are no suppressions.
406+
pub fn is_empty(&self) -> bool {
407+
self.ignores.is_empty() && self.ignore_all.is_empty()
408+
}
404409
}
405410

406411
#[cfg(test)]

pyrefly/lib/error/suppress.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ pub fn remove_unused_ignores(loads: &Errors, all: bool) -> usize {
373373
// Collect ignores with full suppression data (including comment_line)
374374
let mut all_ignores: SmallMap<&PathBuf, &Ignore> = SmallMap::new();
375375
for (module_path, ignore) in loads.collect_ignores() {
376-
if let ModulePathDetails::FileSystem(path) = module_path.details() {
376+
if let ModulePathDetails::FileSystem(path) = module_path.details()
377+
&& !ignore.is_empty()
378+
{
377379
all_ignores.insert(path, ignore);
378380
}
379381
}
@@ -490,10 +492,12 @@ pub fn remove_unused_ignores(loads: &Errors, all: bool) -> usize {
490492
buf.push_str(line);
491493
buf.push_str(line_ending);
492494
}
493-
if let Err(e) = fs_anyhow::write(path, buf) {
494-
error!("Failed to remove unused error suppressions in {} files:", e);
495-
} else if unused_ignore_count > 0 {
496-
removed_ignores.insert(path, unused_ignore_count);
495+
if unused_ignore_count > 0 {
496+
if let Err(e) = fs_anyhow::write(path, buf) {
497+
error!("Failed to remove unused error suppressions in {} files:", e);
498+
} else {
499+
removed_ignores.insert(path, unused_ignore_count);
500+
}
497501
}
498502
}
499503
}
@@ -912,10 +916,24 @@ def f() -> int:
912916
fn test_no_remove_suppression() {
913917
let input = r#"
914918
def g() -> int:
915-
return "hello" # pyrefly: ignore [bad-return]
916-
"#;
919+
return "hello" # pyrefly: ignore [bad-return]"#;
920+
// No trailing newline on purpose.
921+
// Ensures files with only used suppressions are not rewritten (no newline added).
922+
// https://github.com/facebook/pyrefly/issues/2185
917923
assert_remove_ignores(input, input, false, 0);
918924
}
925+
926+
#[test]
927+
fn test_remove_unused_ignores_no_ignores() {
928+
let input = r#"
929+
def f(x: int) -> int:
930+
return x + 1"#;
931+
// No trailing newline on purpose.
932+
// Ensures files without suppressions are not rewritten (no newline added).
933+
// https://github.com/facebook/pyrefly/issues/2185
934+
assert_remove_ignores(input, input, false, 0);
935+
}
936+
919937
#[test]
920938
fn test_remove_generic_suppression() {
921939
let before = r#"

0 commit comments

Comments
 (0)