Skip to content

Commit

Permalink
Per review
Browse files Browse the repository at this point in the history
  • Loading branch information
InSyncWithFoo committed Feb 7, 2025
1 parent 4fa775a commit b91ab6b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 48 deletions.
44 changes: 15 additions & 29 deletions crates/ruff_linter/src/rules/pylint/rules/bad_str_strip_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,20 @@ use crate::settings::types::PythonVersion;
/// - [Python documentation: `str.strip`](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.strip)
#[derive(ViolationMetadata)]
pub(crate) struct BadStrStripCall {
duplicated: char,
strip: StripKind,
removal: Option<RemovalKind>,
}

impl Violation for BadStrStripCall {
#[derive_message_formats]
fn message(&self) -> String {
let Self {
duplicated,
strip,
removal,
} = self;

let Self { strip, removal } = self;
if let Some(removal) = removal {
format!(
"String `{strip}` call contains duplicate character {duplicated:#?} \
(did you mean `{removal}`?)",
"String `{strip}` call contains duplicate characters (did you mean `{removal}`?)",
)
} else {
format!("String `{strip}` call contains duplicate character {duplicated:#?}")
format!("String `{strip}` call contains duplicate characters")
}
}
}
Expand Down Expand Up @@ -143,19 +136,19 @@ impl fmt::Display for RemovalKind {
}
}

fn find_duplicate_in_string(string: &ast::StringLiteralValue) -> Option<char> {
find_duplicate(string.chars())
fn string_has_duplicate_char(string: &ast::StringLiteralValue) -> bool {
has_duplicate(string.chars())
}

fn find_duplicate_in_bytes(bytes: &ast::BytesLiteralValue) -> Option<char> {
find_duplicate(bytes.bytes().map(char::from))
fn bytes_has_duplicate_char(bytes: &ast::BytesLiteralValue) -> bool {
has_duplicate(bytes.bytes().map(char::from))
}

/// Return the first duplicated character of a string or byte sequence, if any.
fn find_duplicate(mut chars: impl Iterator<Item = char>) -> Option<char> {
/// Return true if a string or byte sequence has a duplicate.
fn has_duplicate(mut chars: impl Iterator<Item = char>) -> bool {
let mut seen = FxHashSet::default();

chars.find(|&char| !seen.insert(char))
chars.any(|char| !seen.insert(char))
}

/// PLE1310
Expand Down Expand Up @@ -184,32 +177,25 @@ pub(crate) fn bad_str_strip_call(checker: &Checker, call: &ast::ExprCall) {

let duplicated = match arg {
Expr::StringLiteral(string) if value_kind == ValueKind::String => {
find_duplicate_in_string(&string.value)
string_has_duplicate_char(&string.value)
}
Expr::BytesLiteral(bytes) if value_kind == ValueKind::Bytes => {
find_duplicate_in_bytes(&bytes.value)
bytes_has_duplicate_char(&bytes.value)
}
_ => return,
};

let Some(duplicated) = duplicated else {
if !duplicated {
return;
};
}

let removal = if checker.settings.target_version >= PythonVersion::Py39 {
RemovalKind::for_strip(strip)
} else {
None
};

let diagnostic = Diagnostic::new(
BadStrStripCall {
duplicated,
strip,
removal,
},
arg.range(),
);
let diagnostic = Diagnostic::new(BadStrStripCall { strip, removal }, arg.range());

checker.report_diagnostic(diagnostic);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
---
bad_str_strip_call.py:2:21: PLE1310 String `strip` call contains duplicate character 'l'
bad_str_strip_call.py:2:21: PLE1310 String `strip` call contains duplicate characters
|
1 | # PLE1310
2 | "Hello World".strip("Hello")
Expand All @@ -10,7 +10,7 @@ bad_str_strip_call.py:2:21: PLE1310 String `strip` call contains duplicate chara
4 | # PLE1310
|

bad_str_strip_call.py:5:21: PLE1310 String `strip` call contains duplicate character 'l'
bad_str_strip_call.py:5:21: PLE1310 String `strip` call contains duplicate characters
|
4 | # PLE1310
5 | "Hello World".strip("Hello")
Expand All @@ -19,7 +19,7 @@ bad_str_strip_call.py:5:21: PLE1310 String `strip` call contains duplicate chara
7 | # PLE1310
|

bad_str_strip_call.py:8:21: PLE1310 String `strip` call contains duplicate character 'l'
bad_str_strip_call.py:8:21: PLE1310 String `strip` call contains duplicate characters
|
7 | # PLE1310
8 | "Hello World".strip(u"Hello")
Expand All @@ -28,7 +28,7 @@ bad_str_strip_call.py:8:21: PLE1310 String `strip` call contains duplicate chara
10 | # PLE1310
|

bad_str_strip_call.py:11:21: PLE1310 String `strip` call contains duplicate character 'l'
bad_str_strip_call.py:11:21: PLE1310 String `strip` call contains duplicate characters
|
10 | # PLE1310
11 | "Hello World".strip(r"Hello")
Expand All @@ -37,7 +37,7 @@ bad_str_strip_call.py:11:21: PLE1310 String `strip` call contains duplicate char
13 | # PLE1310
|

bad_str_strip_call.py:14:21: PLE1310 String `strip` call contains duplicate character 'l'
bad_str_strip_call.py:14:21: PLE1310 String `strip` call contains duplicate characters
|
13 | # PLE1310
14 | "Hello World".strip("Hello\t")
Expand All @@ -46,7 +46,7 @@ bad_str_strip_call.py:14:21: PLE1310 String `strip` call contains duplicate char
16 | # PLE1310
|

bad_str_strip_call.py:17:21: PLE1310 String `strip` call contains duplicate character 'l'
bad_str_strip_call.py:17:21: PLE1310 String `strip` call contains duplicate characters
|
16 | # PLE1310
17 | "Hello World".strip(r"Hello\t")
Expand All @@ -55,7 +55,7 @@ bad_str_strip_call.py:17:21: PLE1310 String `strip` call contains duplicate char
19 | # PLE1310
|

bad_str_strip_call.py:20:21: PLE1310 String `strip` call contains duplicate character 'l'
bad_str_strip_call.py:20:21: PLE1310 String `strip` call contains duplicate characters
|
19 | # PLE1310
20 | "Hello World".strip("Hello\\")
Expand All @@ -64,7 +64,7 @@ bad_str_strip_call.py:20:21: PLE1310 String `strip` call contains duplicate char
22 | # PLE1310
|

bad_str_strip_call.py:23:21: PLE1310 String `strip` call contains duplicate character 'l'
bad_str_strip_call.py:23:21: PLE1310 String `strip` call contains duplicate characters
|
22 | # PLE1310
23 | "Hello World".strip(r"Hello\\")
Expand All @@ -73,7 +73,7 @@ bad_str_strip_call.py:23:21: PLE1310 String `strip` call contains duplicate char
25 | # PLE1310
|

bad_str_strip_call.py:26:21: PLE1310 String `strip` call contains duplicate character '🤣'
bad_str_strip_call.py:26:21: PLE1310 String `strip` call contains duplicate characters
|
25 | # PLE1310
26 | "Hello World".strip("🤣🤣🤣🤣🙃👀😀")
Expand All @@ -82,7 +82,7 @@ bad_str_strip_call.py:26:21: PLE1310 String `strip` call contains duplicate char
28 | # PLE1310
|

bad_str_strip_call.py:30:5: PLE1310 String `strip` call contains duplicate character 'e'
bad_str_strip_call.py:30:5: PLE1310 String `strip` call contains duplicate characters
|
28 | # PLE1310
29 | "Hello World".strip(
Expand All @@ -93,7 +93,7 @@ bad_str_strip_call.py:30:5: PLE1310 String `strip` call contains duplicate chara
33 | )
|

bad_str_strip_call.py:36:21: PLE1310 String `strip` call contains duplicate character ' '
bad_str_strip_call.py:36:21: PLE1310 String `strip` call contains duplicate characters
|
35 | # PLE1310
36 | "Hello World".strip("can we get a long " \
Expand All @@ -105,7 +105,7 @@ bad_str_strip_call.py:36:21: PLE1310 String `strip` call contains duplicate char
40 | # PLE1310
|

bad_str_strip_call.py:42:5: PLE1310 String `strip` call contains duplicate character ' '
bad_str_strip_call.py:42:5: PLE1310 String `strip` call contains duplicate characters
|
40 | # PLE1310
41 | "Hello World".strip(
Expand All @@ -116,7 +116,7 @@ bad_str_strip_call.py:42:5: PLE1310 String `strip` call contains duplicate chara
45 | )
|

bad_str_strip_call.py:49:5: PLE1310 String `strip` call contains duplicate character ' '
bad_str_strip_call.py:49:5: PLE1310 String `strip` call contains duplicate characters
|
47 | # PLE1310
48 | "Hello World".strip(
Expand All @@ -127,7 +127,7 @@ bad_str_strip_call.py:49:5: PLE1310 String `strip` call contains duplicate chara
52 | )
|

bad_str_strip_call.py:61:11: PLE1310 String `strip` call contains duplicate character 't'
bad_str_strip_call.py:61:11: PLE1310 String `strip` call contains duplicate characters
|
60 | # PLE1310
61 | u''.strip('http://')
Expand All @@ -136,7 +136,7 @@ bad_str_strip_call.py:61:11: PLE1310 String `strip` call contains duplicate char
63 | # PLE1310
|

bad_str_strip_call.py:64:12: PLE1310 String `lstrip` call contains duplicate character 't' (did you mean `removeprefix`?)
bad_str_strip_call.py:64:12: PLE1310 String `lstrip` call contains duplicate characters (did you mean `removeprefix`?)
|
63 | # PLE1310
64 | u''.lstrip('http://')
Expand All @@ -145,7 +145,7 @@ bad_str_strip_call.py:64:12: PLE1310 String `lstrip` call contains duplicate cha
66 | # PLE1310
|

bad_str_strip_call.py:67:12: PLE1310 String `rstrip` call contains duplicate character 't' (did you mean `removesuffix`?)
bad_str_strip_call.py:67:12: PLE1310 String `rstrip` call contains duplicate characters (did you mean `removesuffix`?)
|
66 | # PLE1310
67 | b''.rstrip(b'http://')
Expand All @@ -154,7 +154,7 @@ bad_str_strip_call.py:67:12: PLE1310 String `rstrip` call contains duplicate cha
69 | # OK
|

bad_str_strip_call.py:79:10: PLE1310 String `strip` call contains duplicate character '\\'
bad_str_strip_call.py:79:10: PLE1310 String `strip` call contains duplicate characters
|
78 | # Errors: Multiple backslashes
79 | ''.strip('\\b\\x09')
Expand All @@ -163,7 +163,7 @@ bad_str_strip_call.py:79:10: PLE1310 String `strip` call contains duplicate char
81 | ''.strip('\\\x5C')
|

bad_str_strip_call.py:80:10: PLE1310 String `strip` call contains duplicate character '\\'
bad_str_strip_call.py:80:10: PLE1310 String `strip` call contains duplicate characters
|
78 | # Errors: Multiple backslashes
79 | ''.strip('\\b\\x09')
Expand All @@ -172,7 +172,7 @@ bad_str_strip_call.py:80:10: PLE1310 String `strip` call contains duplicate char
81 | ''.strip('\\\x5C')
|

bad_str_strip_call.py:81:10: PLE1310 String `strip` call contains duplicate character '\\'
bad_str_strip_call.py:81:10: PLE1310 String `strip` call contains duplicate characters
|
79 | ''.strip('\\b\\x09')
80 | ''.strip(r'\b\x09')
Expand Down

0 comments on commit b91ab6b

Please sign in to comment.