Skip to content

Commit

Permalink
Improve B005 documentation to reflect duplicate-character behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 22, 2023
1 parent 74dbd87 commit d209414
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,25 @@ use crate::checkers::ast::Checker;
/// `str.removesuffix` to remove an exact prefix or suffix from a string,
/// respectively, which should be preferred when possible.
///
/// ## Known problems
/// As a heuristic, this rule only flags multi-character strings that contain
/// duplicate characters. This allows usages like `.strip("xyz")`, which
/// removes all occurrences of the characters `x`, `y`, and `z` from the
/// leading and trailing ends of the string, but not `.strip("foo")`.
///
/// The use of unique, multi-character strings may be intentional and
/// consistent with the intent of `.strip()`, `.lstrip()`, or `.rstrip()`,
/// while the use of duplicate-character strings is very likely to be a
/// mistake.
///
/// ## Example
/// ```python
/// "abcba".strip("ab") # "c"
/// "text.txt".strip(".txt") # "ex"
/// ```
///
/// Use instead:
/// ```python
/// "abcba".removeprefix("ab").removesuffix("ba") # "c"
/// "text.txt".removesuffix(".txt") # "text"
/// ```
///
/// ## References
Expand Down Expand Up @@ -65,8 +76,7 @@ pub(crate) fn strip_with_multi_characters(
return;
};

let num_chars = value.chars().count();
if num_chars > 1 && num_chars != value.chars().unique().count() {
if value.chars().count() > 1 && !value.chars().all_unique() {
checker
.diagnostics
.push(Diagnostic::new(StripWithMultiCharacters, expr.range()));
Expand Down

0 comments on commit d209414

Please sign in to comment.