Skip to content

Commit

Permalink
Ignore TODO tags in commented-out-code (#7523)
Browse files Browse the repository at this point in the history
## 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`
  • Loading branch information
tjkuson committed Sep 28, 2023
1 parent cfbebcf commit c2a9cf8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
56 changes: 49 additions & 7 deletions crates/ruff_linter/src/rules/eradicate/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ruff_python_parser::parse_suite;

static ALLOWLIST_REGEX: Lazy<Regex> = Lazy::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()
});
static BRACKET_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[()\[\]{}\s]+$").unwrap());
Expand Down Expand Up @@ -39,6 +39,15 @@ pub(crate) fn comment_contains_code(line: &str, task_tags: &[String]) -> bool {
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").
if HASH_NUMBER.is_match(line) {
return false;
Expand All @@ -49,12 +58,6 @@ pub(crate) fn comment_contains_code(line: &str, task_tags: &[String]) -> bool {
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) {
return false;
}
Expand Down Expand Up @@ -102,6 +105,7 @@ fn multiline_case(line: &str) -> bool {
#[cfg(test)]
mod tests {
use super::comment_contains_code;
use crate::settings::TASK_TAGS;

#[test]
fn comment_contains_code_basic() {
Expand Down Expand Up @@ -279,4 +283,42 @@ mod tests {
&["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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ use super::super::detection::comment_contains_code;
/// Commented-out code is dead code, and is often included inadvertently.
/// 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
/// ```python
/// # print('foo')
/// # print("Hello, world!")
/// ```
///
/// ## Options
/// - `task-tags`
///
/// [#4845]: https://github.com/astral-sh/ruff/issues/4845
#[violation]
pub struct CommentedOutCode;

Expand Down

0 comments on commit c2a9cf8

Please sign in to comment.