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 3 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
18 changes: 17 additions & 1 deletion crates/ruff/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:|(?i)TODO(\([^\)]*\)| @\w+)?:)",
tjkuson marked this conversation as resolved.
Show resolved Hide resolved
).unwrap()
});
static BRACKET_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[()\[\]{}\s]+$").unwrap());
Expand Down Expand Up @@ -279,4 +279,20 @@ mod tests {
&["XXX".to_string()]
));
}

#[test]
fn comment_contains_todo() {
assert!(comment_contains_code("# TODO(tom)", &[]));
assert!(comment_contains_code("# todo(tom)", &[]));
assert!(comment_contains_code("# TODO()", &[]));
assert!(comment_contains_code("# todo()", &[]));

assert!(!comment_contains_code("# TODO(tom): Something", &[]));
assert!(!comment_contains_code("# todo(tom): Something", &[]));
assert!(!comment_contains_code("# TODO: Something", &[]));
assert!(!comment_contains_code("# todo: Something", &[]));
assert!(!comment_contains_code("# TODO(): Something", &[]));
assert!(!comment_contains_code("# todo(): Something", &[]));
assert!(!comment_contains_code("# TODO @tom: Something", &[]));
}
}
8 changes: 7 additions & 1 deletion crates/ruff/src/rules/eradicate/rules/commented_out_code.rs
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