-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refurb] - add
FURB167
/use-long-regex-flag
with autofix
- Loading branch information
1 parent
953d48b
commit 6644a9b
Showing
8 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
crates/ruff_linter/resources/test/fixtures/refurb/FURB167.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import re | ||
|
||
# BAD: | ||
if re.match("^hello", "hello world", re.I): | ||
pass | ||
|
||
|
||
# GOOD: | ||
if re.match("^hello", "hello world", re.IGNORECASE): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
crates/ruff_linter/src/rules/refurb/rules/long_regex_flag.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::Expr; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of shorthand regex flags such as `re.I`. | ||
/// | ||
/// ## Why is this bad? | ||
/// These single-character flags are not as descriptive as the full names. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// if re.match("^hello", "hello world", re.I): | ||
/// ... | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// if re.match("^hello", "hello world", re.IGNORECASE): | ||
/// ... | ||
/// ``` | ||
/// | ||
#[violation] | ||
pub struct LongRegexFlag { | ||
short: &'static str, | ||
long: &'static str, | ||
} | ||
|
||
impl AlwaysFixableViolation for LongRegexFlag { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let LongRegexFlag { short, .. } = self; | ||
format!("Use of shorthand `re.{short}`") | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
let LongRegexFlag { long, .. } = self; | ||
format!("Replace with `re.{long}`") | ||
} | ||
} | ||
|
||
/// FURB167 | ||
pub(crate) fn long_regex_flag(checker: &mut Checker, expr: &Expr) { | ||
let Some((short, long)) = checker | ||
.semantic() | ||
.resolve_call_path(expr) | ||
.and_then(|call_path| match call_path.as_slice() { | ||
["re", "A"] => Some(("A", "ASCII")), | ||
["re", "I"] => Some(("I", "IGNORECASE")), | ||
["re", "L"] => Some(("L", "LOCALE")), | ||
["re", "M"] => Some(("M", "MULTILINE")), | ||
["re", "S"] => Some(("S", "DOTALL")), | ||
["re", "T"] => Some(("T", "TEMPLATE")), | ||
["re", "U"] => Some(("U", "UNICODE")), | ||
["re", "X"] => Some(("X", "VERBOSE")), | ||
_ => None, | ||
}) | ||
else { | ||
return; | ||
}; | ||
|
||
let mut diagnostic = Diagnostic::new(LongRegexFlag { short, long }, expr.range()); | ||
|
||
diagnostic.set_fix(Fix::safe_edit(Edit::replacement( | ||
format!("re.{long}"), | ||
expr.start(), | ||
expr.end(), | ||
))); | ||
|
||
checker.diagnostics.push(diagnostic); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB167_FURB167.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/refurb/mod.rs | ||
--- | ||
FURB167.py:4:38: FURB167 [*] Use of shorthand `re.I` | ||
| | ||
3 | # BAD: | ||
4 | if re.match("^hello", "hello world", re.I): | ||
| ^^^^ FURB167 | ||
5 | pass | ||
| | ||
= help: Replace with `re.IGNORECASE` | ||
|
||
ℹ Safe fix | ||
1 1 | import re | ||
2 2 | | ||
3 3 | # BAD: | ||
4 |-if re.match("^hello", "hello world", re.I): | ||
4 |+if re.match("^hello", "hello world", re.IGNORECASE): | ||
5 5 | pass | ||
6 6 | | ||
7 7 | | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.