From 70a5a5a5d2a3459f3f216f0aee6fc35ccf774c10 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 2 Jan 2023 14:05:02 -0500 Subject: [PATCH] Detect unpacking assignments in eradicate --- src/eradicate/detection.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/eradicate/detection.rs b/src/eradicate/detection.rs index ad1b2c2bc91d3..4d368f9800cad 100644 --- a/src/eradicate/detection.rs +++ b/src/eradicate/detection.rs @@ -24,7 +24,7 @@ static CODING_COMMENT_REGEX: Lazy = Lazy::new(|| Regex::new(r"^.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)").unwrap()); static HASH_NUMBER: Lazy = Lazy::new(|| Regex::new(r"#\d").unwrap()); static MULTILINE_ASSIGNMENT_REGEX: Lazy = - Lazy::new(|| Regex::new(r"^\s*\w+\s*=.*[(\[{]$").unwrap()); + Lazy::new(|| Regex::new(r"^\s*([(\[]\s*)?(\w+\s*,\s*)*\w+\s*([)\]]\s*)?=.*[(\[{]$").unwrap()); static PARTIAL_DICTIONARY_REGEX: Lazy = Lazy::new(|| Regex::new(r#"^\s*['"]\w+['"]\s*:.+[,{]\s*$"#).unwrap()); static PRINT_RETURN_REGEX: Lazy = Lazy::new(|| Regex::new(r"^(print|return)\b\s*").unwrap()); @@ -153,6 +153,21 @@ mod tests { assert!(!comment_contains_code("#or else:")); assert!(!comment_contains_code("#else True:")); + // Unpacking assignments + assert!(comment_contains_code( + "# user_content_type, _ = TimelineEvent.objects.using(db_alias).get_or_create(" + )); + assert!(comment_contains_code( + "# (user_content_type, _) = TimelineEvent.objects.using(db_alias).get_or_create(" + )); + assert!(comment_contains_code( + "# ( user_content_type , _ )= TimelineEvent.objects.using(db_alias).get_or_create(" + )); + assert!(comment_contains_code( + "# app_label=\"core\", model=\"user\"" + )); + assert!(comment_contains_code("# )")); + // TODO(charlie): This should be `true` under aggressive mode. assert!(!comment_contains_code("#def foo():")); }