mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 22:01:47 +00:00
feat(ERA001): detect single-line code for try:, except:, etc. (#10057)
Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
This commit is contained in:
parent
ee4efdba96
commit
0f70c99c42
3 changed files with 144 additions and 1 deletions
|
@ -30,3 +30,9 @@ dictionary = {
|
||||||
#import os # noqa
|
#import os # noqa
|
||||||
|
|
||||||
# case 1:
|
# case 1:
|
||||||
|
# try:
|
||||||
|
# try: # with comment
|
||||||
|
# try: print()
|
||||||
|
# except:
|
||||||
|
# except Foo:
|
||||||
|
# except Exception as e: print(e)
|
||||||
|
|
|
@ -26,7 +26,7 @@ static HASH_NUMBER: Lazy<Regex> = Lazy::new(|| Regex::new(r"#\d").unwrap());
|
||||||
static POSITIVE_CASES: Lazy<RegexSet> = Lazy::new(|| {
|
static POSITIVE_CASES: Lazy<RegexSet> = Lazy::new(|| {
|
||||||
RegexSet::new([
|
RegexSet::new([
|
||||||
// Keywords
|
// 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
|
// Partial dictionary
|
||||||
r#"^['"]\w+['"]\s*:.+[,{]\s*(#.*)?$"#,
|
r#"^['"]\w+['"]\s*:.+[,{]\s*(#.*)?$"#,
|
||||||
// Multiline assignment
|
// Multiline assignment
|
||||||
|
@ -147,6 +147,27 @@ mod tests {
|
||||||
assert!(!comment_contains_code("#to print", &[]));
|
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]
|
#[test]
|
||||||
fn comment_contains_code_with_multiline() {
|
fn comment_contains_code_with_multiline() {
|
||||||
assert!(comment_contains_code("#else:", &[]));
|
assert!(comment_contains_code("#else:", &[]));
|
||||||
|
@ -157,6 +178,7 @@ mod tests {
|
||||||
assert!(comment_contains_code("#except Exception:", &[]));
|
assert!(comment_contains_code("#except Exception:", &[]));
|
||||||
assert!(comment_contains_code("# case 1:", &[]));
|
assert!(comment_contains_code("# case 1:", &[]));
|
||||||
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("# this is = to that :(", &[]));
|
||||||
assert!(!comment_contains_code("#else", &[]));
|
assert!(!comment_contains_code("#else", &[]));
|
||||||
|
|
|
@ -154,6 +154,8 @@ ERA001.py:32:1: ERA001 Found commented-out code
|
||||||
31 |
|
31 |
|
||||||
32 | # case 1:
|
32 | # case 1:
|
||||||
| ^^^^^^^^^ ERA001
|
| ^^^^^^^^^ ERA001
|
||||||
|
33 | # try:
|
||||||
|
34 | # try: # with comment
|
||||||
|
|
|
|
||||||
= help: Remove commented-out code
|
= help: Remove commented-out code
|
||||||
|
|
||||||
|
@ -162,3 +164,116 @@ ERA001.py:32:1: ERA001 Found commented-out code
|
||||||
30 30 | #import os # noqa
|
30 30 | #import os # noqa
|
||||||
31 31 |
|
31 31 |
|
||||||
32 |-# case 1:
|
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)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue