From 28160b2ba0b409eb7b1a3b3e2d609829ba81b37f Mon Sep 17 00:00:00 2001 From: Ottavio Hartman Date: Mon, 19 Feb 2024 19:18:11 -0500 Subject: [PATCH 1/4] 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/4] 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/4] 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 4c4b7a678ee25a74cfd692c8d477ea356ee3677b Mon Sep 17 00:00:00 2001 From: Ottavio Hartman Date: Mon, 19 Feb 2024 23:09:18 -0500 Subject: [PATCH 4/4] update snap --- ...s__eradicate__tests__ERA001_ERA001.py.snap | 124 +++++++++++------- 1 file changed, 79 insertions(+), 45 deletions(-) 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 89e12700e7220..04bf4cf9021c4 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,11 +154,8 @@ ERA001.py:32:1: ERA001 Found commented-out code 31 | 32 | # case 1: | ^^^^^^^^^ ERA001 -<<<<<<< HEAD -33 | # case "foo": print() -34 | # try: A() -======= ->>>>>>> 0d363ab23978396490e495c3bbb24b14c78e2741 +33 | # try: +34 | # try: # with comment | = help: Remove commented-out code @@ -167,18 +164,17 @@ ERA001.py:32:1: ERA001 Found commented-out code 30 30 | #import os # noqa 31 31 | 32 |-# case 1: -<<<<<<< HEAD -33 32 | # case "foo": print() -34 33 | # try: A() -35 34 | # except Exception as e: print(e) +33 32 | # try: +34 33 | # try: # with comment +35 34 | # try: print() 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) +33 | # try: + | ^^^^^^ ERA001 +34 | # try: # with comment +35 | # try: print() | = help: Remove commented-out code @@ -186,60 +182,98 @@ ERA001.py:33:1: ERA001 Found commented-out code 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") +33 |-# try: +34 33 | # try: # with comment +35 34 | # try: print() +36 35 | # except: 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") +33 | # try: +34 | # try: # with comment + | ^^^^^^^^^^^^^^^^^^^^^^ ERA001 +35 | # try: print() +36 | # except: | = 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") +33 33 | # try: +34 |-# try: # with comment +35 34 | # try: print() +36 35 | # except: +37 36 | # except Foo: 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") +33 | # try: +34 | # try: # with comment +35 | # try: print() + | ^^^^^^^^^^^^^^ ERA001 +36 | # except: +37 | # except Foo: | = 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") +33 33 | # try: +34 34 | # try: # with comment +35 |-# try: print() +36 35 | # except: +37 36 | # except Foo: +38 37 | # except Exception as e: print(e) ERA001.py:36:1: ERA001 Found commented-out code | -34 | # try: A() -35 | # except Exception as e: print(e) -36 | # finally: print("done") - | ^^^^^^^^^^^^^^^^^^^^^^^^ ERA001 +34 | # try: # with comment +35 | # try: print() +36 | # except: + | ^^^^^^^^^ ERA001 +37 | # except Foo: +38 | # except Exception as e: print(e) + | + = help: Remove commented-out code + +ℹ Display-only fix +33 33 | # try: +34 34 | # try: # with comment +35 35 | # try: print() +36 |-# except: +37 36 | # except Foo: +38 37 | # except Exception as e: print(e) + +ERA001.py:37:1: ERA001 Found commented-out code + | +35 | # try: print() +36 | # except: +37 | # except Foo: + | ^^^^^^^^^^^^^ ERA001 +38 | # except Exception as e: print(e) + | + = help: Remove commented-out code + +ℹ Display-only fix +34 34 | # try: # with comment +35 35 | # try: print() +36 36 | # except: +37 |-# except Foo: +38 37 | # except Exception as e: print(e) + +ERA001.py:38:1: ERA001 Found commented-out code + | +36 | # except: +37 | # except Foo: +38 | # except Exception as e: print(e) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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") -======= ->>>>>>> 0d363ab23978396490e495c3bbb24b14c78e2741 +35 35 | # try: print() +36 36 | # except: +37 37 | # except Foo: +38 |-# except Exception as e: print(e)