[red knot] Use memmem::find instead of custom version (#13750)

This is a follow-up on #13746:

- Use `memmem::find` instead of rolling our own inferior version.
- Avoid `x.as_ref()` calls using `&**x`
This commit is contained in:
David Peter 2024-10-14 15:17:19 +02:00 committed by GitHub
parent 6048f331d9
commit 04b636cba2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 10 additions and 14 deletions

1
Cargo.lock generated
View file

@ -2083,6 +2083,7 @@ dependencies = [
"hashbrown 0.15.0", "hashbrown 0.15.0",
"insta", "insta",
"itertools 0.13.0", "itertools 0.13.0",
"memchr",
"ordermap", "ordermap",
"red_knot_test", "red_knot_test",
"red_knot_vendored", "red_knot_vendored",

View file

@ -34,6 +34,7 @@ hashbrown = { workspace = true }
smallvec = { workspace = true } smallvec = { workspace = true }
static_assertions = { workspace = true } static_assertions = { workspace = true }
test-case = { workspace = true } test-case = { workspace = true }
memchr = { workspace = true }
[dev-dependencies] [dev-dependencies]
ruff_db = { workspace = true, features = ["os", "testing"] } ruff_db = { workspace = true, features = ["os", "testing"] }

View file

@ -2661,18 +2661,8 @@ impl<'db> TypeInferenceBuilder<'db> {
} }
(Type::BytesLiteral(salsa_b1), Type::BytesLiteral(salsa_b2)) => { (Type::BytesLiteral(salsa_b1), Type::BytesLiteral(salsa_b2)) => {
let contains_subsequence = |needle: &[u8], haystack: &[u8]| { let b1 = &**salsa_b1.value(self.db);
if needle.is_empty() { let b2 = &**salsa_b2.value(self.db);
true
} else {
haystack
.windows(needle.len())
.any(|window| window == needle)
}
};
let b1 = salsa_b1.value(self.db).as_ref();
let b2 = salsa_b2.value(self.db).as_ref();
match op { match op {
ast::CmpOp::Eq => Some(Type::BooleanLiteral(b1 == b2)), ast::CmpOp::Eq => Some(Type::BooleanLiteral(b1 == b2)),
ast::CmpOp::NotEq => Some(Type::BooleanLiteral(b1 != b2)), ast::CmpOp::NotEq => Some(Type::BooleanLiteral(b1 != b2)),
@ -2680,8 +2670,12 @@ impl<'db> TypeInferenceBuilder<'db> {
ast::CmpOp::LtE => Some(Type::BooleanLiteral(b1 <= b2)), ast::CmpOp::LtE => Some(Type::BooleanLiteral(b1 <= b2)),
ast::CmpOp::Gt => Some(Type::BooleanLiteral(b1 > b2)), ast::CmpOp::Gt => Some(Type::BooleanLiteral(b1 > b2)),
ast::CmpOp::GtE => Some(Type::BooleanLiteral(b1 >= b2)), ast::CmpOp::GtE => Some(Type::BooleanLiteral(b1 >= b2)),
ast::CmpOp::In => Some(Type::BooleanLiteral(contains_subsequence(b1, b2))), ast::CmpOp::In => {
ast::CmpOp::NotIn => Some(Type::BooleanLiteral(!contains_subsequence(b1, b2))), Some(Type::BooleanLiteral(memchr::memmem::find(b2, b1).is_some()))
}
ast::CmpOp::NotIn => {
Some(Type::BooleanLiteral(memchr::memmem::find(b2, b1).is_none()))
}
ast::CmpOp::Is => { ast::CmpOp::Is => {
if b1 == b2 { if b1 == b2 {
Some(KnownClass::Bool.to_instance(self.db)) Some(KnownClass::Bool.to_instance(self.db))