Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore TODO tags in commented-out-code #7523

Merged
merged 8 commits into from Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 49 additions & 7 deletions crates/ruff_linter/src/rules/eradicate/detection.rs
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up removing the uppercase check here because it requires an allocation (would be expensive give how often this runs), and is probably rare.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks!

{
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));
}
}
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