Ignore TODO tags in commented-out-code (#7523)

## Summary

Extend the `task-tags` checking logic to ignore TODO tags (with or
without parentheses). For example,

```python
# TODO(tjkuson): Rewrite in Rust
```

is no longer flagged as commented-out code.

Closes #7031.

I also updated the documentation to inform users that the rule is prone
to false positives like this!

EDIT: Accidentally linked to the wrong issue when first opening this PR,
now corrected.

## Test Plan

`cargo test`
This commit is contained in:
Tom Kuson 2023-09-29 00:13:11 +01:00 committed by GitHub
parent cfbebcf354
commit c2a9cf8ae5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 8 deletions

View file

@ -6,7 +6,7 @@ use ruff_python_parser::parse_suite;
static ALLOWLIST_REGEX: Lazy<Regex> = Lazy::new(|| { static ALLOWLIST_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new( Regex::new(
r"^(?i)(?:pylint|pyright|noqa|nosec|region|endregion|type:\s*ignore|fmt:\s*(on|off)|isort:\s*(on|off|skip|skip_file|split|dont-add-imports(:\s*\[.*?])?)|mypy:|SPDX-License-Identifier:)" r"^(?i)(?:pylint|pyright|noqa|nosec|region|endregion|type:\s*ignore|fmt:\s*(on|off)|isort:\s*(on|off|skip|skip_file|split|dont-add-imports(:\s*\[.*?])?)|mypy:|SPDX-License-Identifier:)",
).unwrap() ).unwrap()
}); });
static BRACKET_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[()\[\]{}\s]+$").unwrap()); static BRACKET_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[()\[\]{}\s]+$").unwrap());
@ -39,6 +39,15 @@ pub(crate) fn comment_contains_code(line: &str, task_tags: &[String]) -> bool {
return false; return false;
}; };
// Ignore task tag comments (e.g., "# TODO(tom): Refactor").
if line
.split(&[' ', ':', '('])
.next()
.is_some_and(|first| task_tags.iter().any(|tag| tag == first))
{
return false;
}
// Ignore non-comment related hashes (e.g., "# Issue #999"). // Ignore non-comment related hashes (e.g., "# Issue #999").
if HASH_NUMBER.is_match(line) { if HASH_NUMBER.is_match(line) {
return false; return false;
@ -49,12 +58,6 @@ pub(crate) fn comment_contains_code(line: &str, task_tags: &[String]) -> bool {
return false; return false;
} }
if let Some(first) = line.split(&[' ', ':']).next() {
if task_tags.iter().any(|tag| tag == first) {
return false;
}
}
if CODING_COMMENT_REGEX.is_match(line) { if CODING_COMMENT_REGEX.is_match(line) {
return false; return false;
} }
@ -102,6 +105,7 @@ fn multiline_case(line: &str) -> bool {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::comment_contains_code; use super::comment_contains_code;
use crate::settings::TASK_TAGS;
#[test] #[test]
fn comment_contains_code_basic() { fn comment_contains_code_basic() {
@ -279,4 +283,42 @@ mod tests {
&["XXX".to_string()] &["XXX".to_string()]
)); ));
} }
#[test]
fn comment_contains_todo() {
let task_tags = TASK_TAGS
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>();
assert!(!comment_contains_code(
"# TODO(tom): Rewrite in Rust",
&task_tags
));
assert!(!comment_contains_code(
"# TODO: Rewrite in Rust",
&task_tags
));
assert!(!comment_contains_code("# TODO:Rewrite in Rust", &task_tags));
assert!(!comment_contains_code(
"# FIXME(tom): Rewrite in Rust",
&task_tags
));
assert!(!comment_contains_code(
"# FIXME: Rewrite in Rust",
&task_tags
));
assert!(!comment_contains_code(
"# FIXME:Rewrite in Rust",
&task_tags
));
assert!(!comment_contains_code(
"# XXX(tom): Rewrite in Rust",
&task_tags
));
assert!(!comment_contains_code("# XXX: Rewrite in Rust", &task_tags));
assert!(!comment_contains_code("# XXX:Rewrite in Rust", &task_tags));
}
} }

View file

@ -15,13 +15,19 @@ use super::super::detection::comment_contains_code;
/// Commented-out code is dead code, and is often included inadvertently. /// Commented-out code is dead code, and is often included inadvertently.
/// It should be removed. /// It should be removed.
/// ///
/// ## Known problems
/// Prone to false positives when checking comments that resemble Python code,
/// but are not actually Python code ([#4845]).
///
/// ## Example /// ## Example
/// ```python /// ```python
/// # print('foo') /// # print("Hello, world!")
/// ``` /// ```
/// ///
/// ## Options /// ## Options
/// - `task-tags` /// - `task-tags`
///
/// [#4845]: https://github.com/astral-sh/ruff/issues/4845
#[violation] #[violation]
pub struct CommentedOutCode; pub struct CommentedOutCode;