-
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.
Implementation for RUF035 split-of-static-string
- Loading branch information
Showing
8 changed files
with
585 additions
and
0 deletions.
There are no files selected for viewing
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,56 @@ | ||
# setup | ||
sep = "," | ||
no_sep = None | ||
|
||
# positives | ||
""" | ||
itemA | ||
itemB | ||
itemC | ||
""".split() | ||
|
||
"a,b,c,d".split(",") | ||
"a,b,c,d".split(None) | ||
"a,b,c,d".split(",", 1) | ||
"a,b,c,d".split(None, 1) | ||
"a,b,c,d".split(sep=",") | ||
"a,b,c,d".split(sep=None) | ||
"a,b,c,d".split(sep=",", maxsplit=1) | ||
"a,b,c,d".split(sep=None, maxsplit=1) | ||
"a,b,c,d".split(maxsplit=1, sep=",") | ||
"a,b,c,d".split(maxsplit=1, sep=None) | ||
"a,b,c,d".split(",", maxsplit=1) | ||
"a,b,c,d".split(None, maxsplit=1) | ||
"a,b,c,d".split(maxsplit=1) | ||
"a,b,c,d".split(maxsplit=1.0) | ||
"a,b,c,d".split(maxsplit=1) | ||
"a,b,c,d".split(maxsplit=0) | ||
"VERB AUX PRON ADP DET".split(" ") | ||
' 1 2 3 '.split() | ||
'1<>2<>3<4'.split('<>') | ||
|
||
# negatives | ||
|
||
# test | ||
"a,b,c,d".split(maxsplit="hello") | ||
|
||
# variable names not implemented | ||
"a,b,c,d".split(sep) | ||
"a,b,c,d".split(no_sep) | ||
for n in range(3): | ||
"a,b,c,d".split(",", maxsplit=n) | ||
|
||
# f-strings not yet implemented | ||
world = "world" | ||
_ = f"{world}_hello_world".split("_") | ||
|
||
hello = "hello_world" | ||
_ = f"{hello}_world".split("_") | ||
|
||
# split on bytes not yet implemented, much less frequent | ||
b"TesT.WwW.ExamplE.CoM".split(b".") | ||
|
||
# str.splitlines not yet implemented | ||
"hello\nworld".splitlines() | ||
"hello\nworld".splitlines(keepends=True) | ||
"hello\nworld".splitlines(keepends=False) |
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
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
165 changes: 165 additions & 0 deletions
165
crates/ruff_linter/src/rules/ruff/rules/split_of_static_string.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,165 @@ | ||
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{ | ||
Expr, ExprCall, ExprContext, ExprList, ExprStringLiteral, StringLiteral, StringLiteralFlags, | ||
StringLiteralValue, | ||
}; | ||
use ruff_text_size::{Ranged, TextRange}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for str.split calls that can be replaced with a `list` literal. | ||
/// | ||
/// ## Why is this bad? | ||
/// List literals are more readable and do not require the overhead of calling `str.split`. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// "a,b,c,d".split(",") | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// ["a", "b", "c", "d"] | ||
/// | ||
/// ## References | ||
/// | ||
/// - [Python documentation: `str.split`](https://docs.python.org/3/library/stdtypes.html#str.split) | ||
/// | ||
/// ``` | ||
#[violation] | ||
pub struct SplitOfStaticString; | ||
|
||
impl Violation for SplitOfStaticString { | ||
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; | ||
|
||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Consider using a list instead of string split") | ||
} | ||
|
||
fn fix_title(&self) -> Option<String> { | ||
Some(format!("Replace string split with list literal")) | ||
} | ||
} | ||
|
||
fn construct_replacement(list_items: &[&str]) -> Expr { | ||
Expr::List(ExprList { | ||
elts: list_items | ||
.iter() | ||
.map(|list_item| { | ||
Expr::StringLiteral(ExprStringLiteral { | ||
value: StringLiteralValue::single(StringLiteral { | ||
value: (*list_item).to_string().into_boxed_str(), | ||
range: TextRange::default(), | ||
flags: StringLiteralFlags::default(), | ||
}), | ||
range: TextRange::default(), | ||
}) | ||
}) | ||
.collect(), | ||
ctx: ExprContext::Load, | ||
range: TextRange::default(), | ||
}) | ||
} | ||
|
||
fn split_default(str_value: &str, max_split: usize) -> Option<Expr> { | ||
// From the Python documentation: | ||
// > If sep is not specified or is None, a different splitting algorithm is applied: runs of | ||
// > consecutive whitespace are regarded as a single separator, and the result will contain | ||
// > no empty strings at the start or end if the string has leading or trailing whitespace. | ||
// > Consequently, splitting an empty string or a string consisting of just whitespace with | ||
// > a None separator returns []. | ||
// https://docs.python.org/3/library/stdtypes.html#str.split | ||
if max_split == 0 { | ||
let list_items: Vec<&str> = str_value.split_whitespace().collect(); | ||
Some(construct_replacement(&list_items)) | ||
} else { | ||
// Autofix for maxsplit without separator not yet implemented | ||
None | ||
} | ||
} | ||
|
||
fn split_sep(str_value: &str, sep_value: &str, max_split: usize, direction_left: bool) -> Expr { | ||
let list_items: Vec<&str> = if direction_left && max_split > 0 { | ||
str_value.splitn(max_split + 1, sep_value).collect() | ||
} else if !direction_left && max_split > 0 { | ||
str_value.rsplitn(max_split + 1, sep_value).collect() | ||
} else if direction_left && max_split == 0 { | ||
str_value.split(sep_value).collect() | ||
} else { | ||
str_value.rsplit(sep_value).collect() | ||
}; | ||
construct_replacement(&list_items) | ||
} | ||
|
||
/// RUF035 | ||
pub(crate) fn split_of_static_string( | ||
checker: &mut Checker, | ||
attr: &str, | ||
call: &ExprCall, | ||
str_value: &str, | ||
) { | ||
let ExprCall { arguments, .. } = call; | ||
|
||
let sep_arg = arguments.find_argument("sep", 0); | ||
let maxsplit_arg = arguments.find_argument("maxsplit", 1); | ||
|
||
// `split` vs `rsplit` | ||
let direction_left = attr == "split"; | ||
|
||
let maxsplit_value = if let Some(maxsplit) = maxsplit_arg { | ||
match maxsplit { | ||
Expr::NumberLiteral(maxsplit_val) => { | ||
if let Some(int_value) = maxsplit_val.value.as_int() { | ||
if let Some(usize_value) = int_value.as_usize() { | ||
usize_value | ||
} else { | ||
return; | ||
} | ||
} else { | ||
return; | ||
} | ||
} | ||
// Ignore when `maxsplit` is not a numeric value | ||
_ => { | ||
return; | ||
} | ||
} | ||
} else { | ||
0 | ||
}; | ||
|
||
let split_replacement = if let Some(sep) = sep_arg { | ||
match sep { | ||
Expr::NoneLiteral(_) => split_default(str_value, maxsplit_value), | ||
Expr::StringLiteral(sep_value) => { | ||
let sep_value_str = sep_value.value.to_str(); | ||
Some(split_sep( | ||
str_value, | ||
sep_value_str, | ||
maxsplit_value, | ||
direction_left, | ||
)) | ||
} | ||
// Ignore names until type inference is available | ||
_ => { | ||
return; | ||
} | ||
} | ||
} else { | ||
split_default(str_value, maxsplit_value) | ||
}; | ||
|
||
let mut diagnostic = Diagnostic::new(SplitOfStaticString, call.range()); | ||
if let Some(ref replacement_expr) = split_replacement { | ||
// Construct replacement list | ||
let replacement = checker.generator().expr(replacement_expr); | ||
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( | ||
replacement, | ||
call.range(), | ||
))); | ||
} | ||
checker.diagnostics.push(diagnostic); | ||
} |
Oops, something went wrong.