Skip to content

Commit

Permalink
feat(ERA001): detect single-line code for try:, except:, etc. (#10057)
Browse files Browse the repository at this point in the history
Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
  • Loading branch information
ottaviohartman and charliermarsh committed Feb 20, 2024
1 parent ee4efdb commit 0f70c99
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
Expand Up @@ -30,3 +30,9 @@ class A():
#import os # noqa

# case 1:
# try:
# try: # with comment
# try: print()
# except:
# except Foo:
# except Exception as e: print(e)
24 changes: 23 additions & 1 deletion crates/ruff_linter/src/rules/eradicate/detection.rs
Expand Up @@ -26,7 +26,7 @@ static HASH_NUMBER: Lazy<Regex> = Lazy::new(|| Regex::new(r"#\d").unwrap());
static POSITIVE_CASES: Lazy<RegexSet> = 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
Expand Down Expand Up @@ -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:", &[]));
Expand All @@ -157,6 +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("# try:", &[]));

assert!(!comment_contains_code("# this is = to that :(", &[]));
assert!(!comment_contains_code("#else", &[]));
Expand Down
Expand Up @@ -154,6 +154,8 @@ ERA001.py:32:1: ERA001 Found commented-out code
31 |
32 | # case 1:
| ^^^^^^^^^ ERA001
33 | # try:
34 | # try: # with comment
|
= help: Remove commented-out code

Expand All @@ -162,3 +164,116 @@ ERA001.py:32:1: ERA001 Found commented-out code
30 30 | #import os # noqa
31 31 |
32 |-# case 1:
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 | # try:
| ^^^^^^ ERA001
34 | # try: # with comment
35 | # try: print()
|
= help: Remove commented-out code

Display-only fix
30 30 | #import os # noqa
31 31 |
32 32 | # case 1:
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 | # 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 | # 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 | # 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 | # 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: # 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
35 35 | # try: print()
36 36 | # except:
37 37 | # except Foo:
38 |-# except Exception as e: print(e)

0 comments on commit 0f70c99

Please sign in to comment.