From 5ce35faa869e37f8ec2894121ab65d18f944d4f9 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 17 Apr 2023 04:41:01 +0530 Subject: [PATCH] Do not consider nested comment as part of code (#3984) --- .../test/fixtures/eradicate/ERA001.py | 1 + crates/ruff/src/rules/eradicate/detection.rs | 4 +++- ...s__eradicate__tests__ERA001_ERA001.py.snap | 19 ++++++++++--------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/eradicate/ERA001.py b/crates/ruff/resources/test/fixtures/eradicate/ERA001.py index 4d79010d35..b12529624a 100644 --- a/crates/ruff/resources/test/fixtures/eradicate/ERA001.py +++ b/crates/ruff/resources/test/fixtures/eradicate/ERA001.py @@ -9,6 +9,7 @@ def foo(x, y, z): print(x, y, z) # This is a real comment. + # # This is a (nested) comment. #return True return False diff --git a/crates/ruff/src/rules/eradicate/detection.rs b/crates/ruff/src/rules/eradicate/detection.rs index be758b1fd0..2cff42cd64 100644 --- a/crates/ruff/src/rules/eradicate/detection.rs +++ b/crates/ruff/src/rules/eradicate/detection.rs @@ -33,7 +33,7 @@ static PRINT_RETURN_REGEX: Lazy = Lazy::new(|| Regex::new(r"^(print|retur /// Returns `true` if a comment contains Python code. pub fn comment_contains_code(line: &str, task_tags: &[String]) -> bool { let line = if let Some(line) = line.trim().strip_prefix('#') { - line.trim() + line.trim_start_matches([' ', '#']) } else { return false; }; @@ -105,6 +105,7 @@ mod tests { #[test] fn comment_contains_code_basic() { assert!(comment_contains_code("# x = 1", &[])); + assert!(comment_contains_code("# # x = 1", &[])); assert!(comment_contains_code("#from foo import eradicate", &[])); assert!(comment_contains_code("#import eradicate", &[])); assert!(comment_contains_code(r#"#"key": value,"#, &[])); @@ -117,6 +118,7 @@ mod tests { assert!(!comment_contains_code("#", &[])); 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.1", &[])); assert!(!comment_contains_code("# 1, 2, 3", &[])); diff --git a/crates/ruff/src/rules/eradicate/snapshots/ruff__rules__eradicate__tests__ERA001_ERA001.py.snap b/crates/ruff/src/rules/eradicate/snapshots/ruff__rules__eradicate__tests__ERA001_ERA001.py.snap index 0e41c4cf7c..4005ebdb4a 100644 --- a/crates/ruff/src/rules/eradicate/snapshots/ruff__rules__eradicate__tests__ERA001_ERA001.py.snap +++ b/crates/ruff/src/rules/eradicate/snapshots/ruff__rules__eradicate__tests__ERA001_ERA001.py.snap @@ -72,22 +72,23 @@ ERA001.py:5:1: ERA001 [*] Found commented-out code 7 6 | def foo(x, y, z): 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 | #return True +13 | # This is a real comment. +14 | # # This is a (nested) comment. +15 | #return True | ^^^^^^^^^^^^ ERA001 -14 | return False +16 | return False | = help: Remove commented-out code ℹ Suggested fix -9 9 | print(x, y, z) 10 10 | 11 11 | # This is a real comment. -12 |- #return True -13 12 | return False -14 13 | -15 14 | #import os # noqa: ERA001 +12 12 | # # This is a (nested) comment. +13 |- #return True +14 13 | return False +15 14 | +16 15 | #import os # noqa: ERA001