diff --git a/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py b/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py index 56e433f395b13..b33d14a2cfa03 100644 --- a/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py +++ b/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py @@ -28,3 +28,5 @@ class A(): } #import os # noqa + +# case 1: diff --git a/crates/ruff_linter/src/rules/eradicate/detection.rs b/crates/ruff_linter/src/rules/eradicate/detection.rs index c9e3b06a99209..e6f7086ebb424 100644 --- a/crates/ruff_linter/src/rules/eradicate/detection.rs +++ b/crates/ruff_linter/src/rules/eradicate/detection.rs @@ -26,7 +26,7 @@ static HASH_NUMBER: Lazy = Lazy::new(|| Regex::new(r"#\d").unwrap()); static POSITIVE_CASES: Lazy = Lazy::new(|| { RegexSet::new([ // Keywords - r"^(?:elif\s+.*\s*:|else\s*:|try\s*:|finally\s*:|except\s+.*\s*:)$", + r"^(?:elif\s+.*\s*:|else\s*:|try\s*:|finally\s*:|except\s+.*\s*|case\s+.*\s*:)$", // Partial dictionary r#"^['"]\w+['"]\s*:.+[,{]\s*(#.*)?$"#, // Multiline assignment @@ -155,11 +155,14 @@ mod tests { assert!(comment_contains_code("#elif True:", &[])); assert!(comment_contains_code("#x = foo(", &[])); assert!(comment_contains_code("#except Exception:", &[])); + assert!(comment_contains_code("# case 1:", &[])); + assert!(comment_contains_code("#case 1:", &[])); assert!(!comment_contains_code("# this is = to that :(", &[])); assert!(!comment_contains_code("#else", &[])); assert!(!comment_contains_code("#or else:", &[])); assert!(!comment_contains_code("#else True:", &[])); + assert!(!comment_contains_code("# in that case:", &[])); // Unpacking assignments assert!(comment_contains_code( diff --git a/crates/ruff_linter/src/rules/eradicate/rules/commented_out_code.rs b/crates/ruff_linter/src/rules/eradicate/rules/commented_out_code.rs index 74124b3c16950..4cc38ff256f29 100644 --- a/crates/ruff_linter/src/rules/eradicate/rules/commented_out_code.rs +++ b/crates/ruff_linter/src/rules/eradicate/rules/commented_out_code.rs @@ -47,7 +47,8 @@ fn is_standalone_comment(line: &str) -> bool { for char in line.chars() { if char == '#' { return true; - } else if !char.is_whitespace() { + } + if !char.is_whitespace() { return false; } } diff --git a/crates/ruff_linter/src/rules/eradicate/snapshots/ruff_linter__rules__eradicate__tests__ERA001_ERA001.py.snap b/crates/ruff_linter/src/rules/eradicate/snapshots/ruff_linter__rules__eradicate__tests__ERA001_ERA001.py.snap index 6864796be7573..3302831a7ac03 100644 --- a/crates/ruff_linter/src/rules/eradicate/snapshots/ruff_linter__rules__eradicate__tests__ERA001_ERA001.py.snap +++ b/crates/ruff_linter/src/rules/eradicate/snapshots/ruff_linter__rules__eradicate__tests__ERA001_ERA001.py.snap @@ -148,4 +148,17 @@ ERA001.py:27:5: ERA001 Found commented-out code 29 28 | 30 29 | #import os # noqa +ERA001.py:32:1: ERA001 Found commented-out code + | +30 | #import os # noqa +31 | +32 | # case 1: + | ^^^^^^^^^ ERA001 + | + = help: Remove commented-out code +ℹ Display-only fix +29 29 | +30 30 | #import os # noqa +31 31 | +32 |-# case 1: