Skip to content

Commit

Permalink
Implement PartialEq for ConcatenatedStringLiteral
Browse files Browse the repository at this point in the history
As highlighted in the review:

> If you have two `ConcatenatedStringLiteral` values where both have
> equivalent values for `strings` but where one has `value` initialized
> and the other does not, would you expect them to compare equal?
> Semantically I think I would, since the alternative is that equality is
> dependent on whether `as_str()` has been called, which seems incidental.

#7927 (comment)
  • Loading branch information
dhruvmanila committed Nov 22, 2023
1 parent bcabcca commit 8193ff9
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion crates/ruff_python_ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ impl From<StringLiteral> for Expr {

/// An internal representation of [`StringLiteral`] that represents an
/// implicitly concatenated string.
#[derive(Clone, PartialEq)]
#[derive(Clone)]
struct ConcatenatedStringLiteral {
/// Each string literal that makes up the concatenated string.
strings: Vec<StringLiteral>,
Expand All @@ -1322,6 +1322,19 @@ impl ConcatenatedStringLiteral {
}
}

impl PartialEq for ConcatenatedStringLiteral {
fn eq(&self, other: &Self) -> bool {
if self.strings.len() != other.strings.len() {
return false;
}
// The `zip` here is safe because we have checked the length of both parts.
self.strings
.iter()
.zip(other.strings.iter())
.all(|(s1, s2)| s1 == s2)
}
}

impl Debug for ConcatenatedStringLiteral {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ConcatenatedStringLiteral")
Expand Down

0 comments on commit 8193ff9

Please sign in to comment.