From 28160b2ba0b409eb7b1a3b3e2d609829ba81b37f Mon Sep 17 00:00:00 2001 From: Ottavio Hartman Date: Mon, 19 Feb 2024 19:18:11 -0500 Subject: [PATCH 1/5] add case: to ERA --- crates/ruff_linter/src/rules/eradicate/detection.rs | 5 ++++- .../src/rules/eradicate/rules/commented_out_code.rs | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/eradicate/detection.rs b/crates/ruff_linter/src/rules/eradicate/detection.rs index c9e3b06a99209..fa6253dc58131 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; } } From 20950cf662735f43789d8072cb112ef0027a89ca Mon Sep 17 00:00:00 2001 From: Ottavio Hartman Date: Mon, 19 Feb 2024 19:27:43 -0500 Subject: [PATCH 2/5] add to ERA001.py --- crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py | 3 +++ crates/ruff_linter/src/rules/eradicate/detection.rs | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py b/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py index 56e433f395b13..5dabe348a8bfa 100644 --- a/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py +++ b/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py @@ -28,3 +28,6 @@ class A(): } #import os # noqa + +# case 1: +# case "foo": print() diff --git a/crates/ruff_linter/src/rules/eradicate/detection.rs b/crates/ruff_linter/src/rules/eradicate/detection.rs index fa6253dc58131..85556fe1103d2 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*:|case\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 @@ -157,6 +157,7 @@ mod tests { assert!(comment_contains_code("#except Exception:", &[])); assert!(comment_contains_code("# case 1:", &[])); assert!(comment_contains_code("#case 1:", &[])); + assert!(comment_contains_code("#case 1: print()", &[])); assert!(!comment_contains_code("# this is = to that :(", &[])); assert!(!comment_contains_code("#else", &[])); From 2ec5573fb58c680bb897217ac0fb6984dd753ce4 Mon Sep 17 00:00:00 2001 From: Ottavio Hartman Date: Mon, 19 Feb 2024 19:51:55 -0500 Subject: [PATCH 3/5] add one-line tests and insta --- .../test/fixtures/eradicate/ERA001.py | 3 + .../src/rules/eradicate/detection.rs | 25 +++++- ...s__eradicate__tests__ERA001_ERA001.py.snap | 88 +++++++++++++++++++ 3 files changed, 114 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py b/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py index 5dabe348a8bfa..3abb91e6fe4b5 100644 --- a/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py +++ b/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py @@ -31,3 +31,6 @@ class A(): # case 1: # case "foo": print() +# try: A() +# except Exception as e: print(e) +# finally: print("done") diff --git a/crates/ruff_linter/src/rules/eradicate/detection.rs b/crates/ruff_linter/src/rules/eradicate/detection.rs index 85556fe1103d2..fa870287c6572 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*:|case\s+.*\s*:.*)$", + r"^(?:elif\s+.*\s*:.*|else\s*:.*|try\s*:.*|finally\s*:.*|except.*:.*|case\s+.*\s*:.*)$", // Partial dictionary r#"^['"]\w+['"]\s*:.+[,{]\s*(#.*)?$"#, // Multiline assignment @@ -147,6 +147,27 @@ mod tests { assert!(!comment_contains_code("#to print", &[])); } + #[test] + fn comment_contains_code_single_line() { + assert!(comment_contains_code("# case 1: print()", &[])); + assert!(comment_contains_code("# try: get(1, 2, 3)", &[])); + assert!(comment_contains_code("# else: print()", &[])); + assert!(comment_contains_code("# elif x == 10: print()", &[])); + assert!(comment_contains_code( + "# except Exception as e: print(e)", + &[] + )); + assert!(comment_contains_code("# except: print()", &[])); + assert!(comment_contains_code("# finally: close_handle()", &[])); + + assert!(!comment_contains_code("# try: use cache", &[])); + assert!(!comment_contains_code("# else: we should return", &[])); + assert!(!comment_contains_code( + "# call function except: without cache", + &[] + )); + } + #[test] fn comment_contains_code_with_multiline() { assert!(comment_contains_code("#else:", &[])); @@ -157,7 +178,7 @@ mod tests { assert!(comment_contains_code("#except Exception:", &[])); assert!(comment_contains_code("# case 1:", &[])); assert!(comment_contains_code("#case 1:", &[])); - assert!(comment_contains_code("#case 1: print()", &[])); + assert!(comment_contains_code("# try:", &[])); assert!(!comment_contains_code("# this is = to that :(", &[])); assert!(!comment_contains_code("#else", &[])); 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..43b4ea5201723 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,92 @@ 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 +33 | # case "foo": print() +34 | # try: A() + | + = help: Remove commented-out code + +ℹ Display-only fix +29 29 | +30 30 | #import os # noqa +31 31 | +32 |-# case 1: +33 32 | # case "foo": print() +34 33 | # try: A() +35 34 | # except Exception as e: print(e) + +ERA001.py:33:1: ERA001 Found commented-out code + | +32 | # case 1: +33 | # case "foo": print() + | ^^^^^^^^^^^^^^^^^^^^^ ERA001 +34 | # try: A() +35 | # except Exception as e: print(e) + | + = help: Remove commented-out code + +ℹ Display-only fix +30 30 | #import os # noqa +31 31 | +32 32 | # case 1: +33 |-# case "foo": print() +34 33 | # try: A() +35 34 | # except Exception as e: print(e) +36 35 | # finally: print("done") + +ERA001.py:34:1: ERA001 Found commented-out code + | +32 | # case 1: +33 | # case "foo": print() +34 | # try: A() + | ^^^^^^^^^^ ERA001 +35 | # except Exception as e: print(e) +36 | # finally: print("done") + | + = help: Remove commented-out code +ℹ Display-only fix +31 31 | +32 32 | # case 1: +33 33 | # case "foo": print() +34 |-# try: A() +35 34 | # except Exception as e: print(e) +36 35 | # finally: print("done") + +ERA001.py:35:1: ERA001 Found commented-out code + | +33 | # case "foo": print() +34 | # try: A() +35 | # except Exception as e: print(e) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ERA001 +36 | # finally: print("done") + | + = help: Remove commented-out code + +ℹ Display-only fix +32 32 | # case 1: +33 33 | # case "foo": print() +34 34 | # try: A() +35 |-# except Exception as e: print(e) +36 35 | # finally: print("done") + +ERA001.py:36:1: ERA001 Found commented-out code + | +34 | # try: A() +35 | # except Exception as e: print(e) +36 | # finally: print("done") + | ^^^^^^^^^^^^^^^^^^^^^^^^ ERA001 + | + = help: Remove commented-out code + +ℹ Display-only fix +33 33 | # case "foo": print() +34 34 | # try: A() +35 35 | # except Exception as e: print(e) +36 |-# finally: print("done") From 4e1d2bf6521d11e85c6b25d82ef4565ae755ace2 Mon Sep 17 00:00:00 2001 From: Ottavio Hartman Date: Mon, 19 Feb 2024 22:35:25 -0500 Subject: [PATCH 4/5] remove one-line --- .../test/fixtures/eradicate/ERA001.py | 4 - .../src/rules/eradicate/detection.rs | 23 +----- ...s__eradicate__tests__ERA001_ERA001.py.snap | 75 ------------------- 3 files changed, 1 insertion(+), 101 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py b/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py index 3abb91e6fe4b5..b33d14a2cfa03 100644 --- a/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py +++ b/crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py @@ -30,7 +30,3 @@ class A(): #import os # noqa # case 1: -# case "foo": print() -# try: A() -# except Exception as e: print(e) -# finally: print("done") diff --git a/crates/ruff_linter/src/rules/eradicate/detection.rs b/crates/ruff_linter/src/rules/eradicate/detection.rs index fa870287c6572..292768c5be314 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.*:.*|case\s+.*\s*:.*)$", + r"^(?:elif\s+.*\s*:|else\s*:|try\s*:|finally\s*:|except.*:|case\s+.*\s*:)$", // Partial dictionary r#"^['"]\w+['"]\s*:.+[,{]\s*(#.*)?$"#, // Multiline assignment @@ -147,27 +147,6 @@ mod tests { assert!(!comment_contains_code("#to print", &[])); } - #[test] - fn comment_contains_code_single_line() { - assert!(comment_contains_code("# case 1: print()", &[])); - assert!(comment_contains_code("# try: get(1, 2, 3)", &[])); - assert!(comment_contains_code("# else: print()", &[])); - assert!(comment_contains_code("# elif x == 10: print()", &[])); - assert!(comment_contains_code( - "# except Exception as e: print(e)", - &[] - )); - assert!(comment_contains_code("# except: print()", &[])); - assert!(comment_contains_code("# finally: close_handle()", &[])); - - assert!(!comment_contains_code("# try: use cache", &[])); - assert!(!comment_contains_code("# else: we should return", &[])); - assert!(!comment_contains_code( - "# call function except: without cache", - &[] - )); - } - #[test] fn comment_contains_code_with_multiline() { assert!(comment_contains_code("#else:", &[])); 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 43b4ea5201723..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 @@ -154,8 +154,6 @@ ERA001.py:32:1: ERA001 Found commented-out code 31 | 32 | # case 1: | ^^^^^^^^^ ERA001 -33 | # case "foo": print() -34 | # try: A() | = help: Remove commented-out code @@ -164,76 +162,3 @@ ERA001.py:32:1: ERA001 Found commented-out code 30 30 | #import os # noqa 31 31 | 32 |-# case 1: -33 32 | # case "foo": print() -34 33 | # try: A() -35 34 | # except Exception as e: print(e) - -ERA001.py:33:1: ERA001 Found commented-out code - | -32 | # case 1: -33 | # case "foo": print() - | ^^^^^^^^^^^^^^^^^^^^^ ERA001 -34 | # try: A() -35 | # except Exception as e: print(e) - | - = help: Remove commented-out code - -ℹ Display-only fix -30 30 | #import os # noqa -31 31 | -32 32 | # case 1: -33 |-# case "foo": print() -34 33 | # try: A() -35 34 | # except Exception as e: print(e) -36 35 | # finally: print("done") - -ERA001.py:34:1: ERA001 Found commented-out code - | -32 | # case 1: -33 | # case "foo": print() -34 | # try: A() - | ^^^^^^^^^^ ERA001 -35 | # except Exception as e: print(e) -36 | # finally: print("done") - | - = help: Remove commented-out code - -ℹ Display-only fix -31 31 | -32 32 | # case 1: -33 33 | # case "foo": print() -34 |-# try: A() -35 34 | # except Exception as e: print(e) -36 35 | # finally: print("done") - -ERA001.py:35:1: ERA001 Found commented-out code - | -33 | # case "foo": print() -34 | # try: A() -35 | # except Exception as e: print(e) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ERA001 -36 | # finally: print("done") - | - = help: Remove commented-out code - -ℹ Display-only fix -32 32 | # case 1: -33 33 | # case "foo": print() -34 34 | # try: A() -35 |-# except Exception as e: print(e) -36 35 | # finally: print("done") - -ERA001.py:36:1: ERA001 Found commented-out code - | -34 | # try: A() -35 | # except Exception as e: print(e) -36 | # finally: print("done") - | ^^^^^^^^^^^^^^^^^^^^^^^^ ERA001 - | - = help: Remove commented-out code - -ℹ Display-only fix -33 33 | # case "foo": print() -34 34 | # try: A() -35 35 | # except Exception as e: print(e) -36 |-# finally: print("done") From babbe5a0592c450d6a12049b0bfcbd48a0f22b98 Mon Sep 17 00:00:00 2001 From: Ottavio Hartman Date: Mon, 19 Feb 2024 22:40:37 -0500 Subject: [PATCH 5/5] fix --- crates/ruff_linter/src/rules/eradicate/detection.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/eradicate/detection.rs b/crates/ruff_linter/src/rules/eradicate/detection.rs index 292768c5be314..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.*:|case\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 @@ -157,7 +157,6 @@ mod tests { assert!(comment_contains_code("#except Exception:", &[])); assert!(comment_contains_code("# case 1:", &[])); assert!(comment_contains_code("#case 1:", &[])); - assert!(comment_contains_code("# try:", &[])); assert!(!comment_contains_code("# this is = to that :(", &[])); assert!(!comment_contains_code("#else", &[]));