mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:24 +00:00
Do not consider nested comment as part of code (#3984)
This commit is contained in:
parent
484b572e6b
commit
5ce35faa86
3 changed files with 14 additions and 10 deletions
|
@ -9,6 +9,7 @@ def foo(x, y, z):
|
||||||
print(x, y, z)
|
print(x, y, z)
|
||||||
|
|
||||||
# This is a real comment.
|
# This is a real comment.
|
||||||
|
# # This is a (nested) comment.
|
||||||
#return True
|
#return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ static PRINT_RETURN_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(print|retur
|
||||||
/// Returns `true` if a comment contains Python code.
|
/// Returns `true` if a comment contains Python code.
|
||||||
pub fn comment_contains_code(line: &str, task_tags: &[String]) -> bool {
|
pub fn comment_contains_code(line: &str, task_tags: &[String]) -> bool {
|
||||||
let line = if let Some(line) = line.trim().strip_prefix('#') {
|
let line = if let Some(line) = line.trim().strip_prefix('#') {
|
||||||
line.trim()
|
line.trim_start_matches([' ', '#'])
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
@ -105,6 +105,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn comment_contains_code_basic() {
|
fn comment_contains_code_basic() {
|
||||||
assert!(comment_contains_code("# x = 1", &[]));
|
assert!(comment_contains_code("# x = 1", &[]));
|
||||||
|
assert!(comment_contains_code("# # x = 1", &[]));
|
||||||
assert!(comment_contains_code("#from foo import eradicate", &[]));
|
assert!(comment_contains_code("#from foo import eradicate", &[]));
|
||||||
assert!(comment_contains_code("#import eradicate", &[]));
|
assert!(comment_contains_code("#import eradicate", &[]));
|
||||||
assert!(comment_contains_code(r#"#"key": value,"#, &[]));
|
assert!(comment_contains_code(r#"#"key": value,"#, &[]));
|
||||||
|
@ -117,6 +118,7 @@ mod tests {
|
||||||
|
|
||||||
assert!(!comment_contains_code("#", &[]));
|
assert!(!comment_contains_code("#", &[]));
|
||||||
assert!(!comment_contains_code("# This is a (real) comment.", &[]));
|
assert!(!comment_contains_code("# This is a (real) comment.", &[]));
|
||||||
|
assert!(!comment_contains_code("# # A (nested) comment.", &[]));
|
||||||
assert!(!comment_contains_code("# 123", &[]));
|
assert!(!comment_contains_code("# 123", &[]));
|
||||||
assert!(!comment_contains_code("# 123.1", &[]));
|
assert!(!comment_contains_code("# 123.1", &[]));
|
||||||
assert!(!comment_contains_code("# 1, 2, 3", &[]));
|
assert!(!comment_contains_code("# 1, 2, 3", &[]));
|
||||||
|
|
|
@ -72,22 +72,23 @@ ERA001.py:5:1: ERA001 [*] Found commented-out code
|
||||||
7 6 | def foo(x, y, z):
|
7 6 | def foo(x, y, z):
|
||||||
8 7 | content = 1 # print('hello')
|
8 7 | content = 1 # print('hello')
|
||||||
|
|
||||||
ERA001.py:12:5: ERA001 [*] Found commented-out code
|
ERA001.py:13:5: ERA001 [*] Found commented-out code
|
||||||
|
|
|
|
||||||
12 | # This is a real comment.
|
13 | # This is a real comment.
|
||||||
13 | #return True
|
14 | # # This is a (nested) comment.
|
||||||
|
15 | #return True
|
||||||
| ^^^^^^^^^^^^ ERA001
|
| ^^^^^^^^^^^^ ERA001
|
||||||
14 | return False
|
16 | return False
|
||||||
|
|
|
|
||||||
= help: Remove commented-out code
|
= help: Remove commented-out code
|
||||||
|
|
||||||
ℹ Suggested fix
|
ℹ Suggested fix
|
||||||
9 9 | print(x, y, z)
|
|
||||||
10 10 |
|
10 10 |
|
||||||
11 11 | # This is a real comment.
|
11 11 | # This is a real comment.
|
||||||
12 |- #return True
|
12 12 | # # This is a (nested) comment.
|
||||||
13 12 | return False
|
13 |- #return True
|
||||||
14 13 |
|
14 13 | return False
|
||||||
15 14 | #import os # noqa: ERA001
|
15 14 |
|
||||||
|
16 15 | #import os # noqa: ERA001
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue