diff --git a/src/blocker.rs b/src/blocker.rs index a5761d64..7b4987fd 100644 --- a/src/blocker.rs +++ b/src/blocker.rs @@ -288,10 +288,8 @@ impl Blocker { // matching pages. return None } - } else { - if let Some(directive) = &filter.csp { - enabled_directives.insert(directive); - } + } else if let Some(directive) = &filter.csp { + enabled_directives.insert(directive); } } diff --git a/src/cosmetic_filter_cache.rs b/src/cosmetic_filter_cache.rs index 965a1b26..ab773129 100644 --- a/src/cosmetic_filter_cache.rs +++ b/src/cosmetic_filter_cache.rs @@ -180,12 +180,10 @@ impl CosmeticFilterCache { let key = key.clone(); if rule.mask.contains(CosmeticFilterMask::IS_SIMPLE) { self.simple_class_rules.insert(key); + } else if let Some(bucket) = self.complex_class_rules.get_mut(&key) { + bucket.push(rule.selector); } else { - if let Some(bucket) = self.complex_class_rules.get_mut(&key) { - bucket.push(rule.selector); - } else { - self.complex_class_rules.insert(key, vec![rule.selector]); - } + self.complex_class_rules.insert(key, vec![rule.selector]); } } } else if rule.mask.contains(CosmeticFilterMask::IS_ID_SELECTOR) { @@ -193,12 +191,10 @@ impl CosmeticFilterCache { let key = key.clone(); if rule.mask.contains(CosmeticFilterMask::IS_SIMPLE) { self.simple_id_rules.insert(key); + } else if let Some(bucket) = self.complex_id_rules.get_mut(&key) { + bucket.push(rule.selector); } else { - if let Some(bucket) = self.complex_id_rules.get_mut(&key) { - bucket.push(rule.selector); - } else { - self.complex_id_rules.insert(key, vec![rule.selector]); - } + self.complex_id_rules.insert(key, vec![rule.selector]); } } } else { @@ -439,12 +435,8 @@ impl HostnameRuleDb { } } - pub fn retrieve<'a>(&'a self, hostname: &Hash) -> Option<&'a[SpecificFilterType]> { - if let Some(bucket) = self.db.get(hostname) { - Some(&bucket) - } else { - None - } + pub fn retrieve(&self, hostname: &Hash) -> Option<&[SpecificFilterType]> { + self.db.get(hostname).map(|bucket| &bucket[..]) } } @@ -506,12 +498,10 @@ impl From<&CosmeticFilter> for SpecificFilterType { } else { SpecificFilterType::ScriptInject(rule.selector.clone()) } + } else if unhide { + SpecificFilterType::Unhide(rule.selector.clone()) } else { - if unhide { - SpecificFilterType::Unhide(rule.selector.clone()) - } else { - SpecificFilterType::Hide(rule.selector.clone()) - } + SpecificFilterType::Hide(rule.selector.clone()) } } } diff --git a/src/data_format.rs b/src/data_format.rs index f9d00618..db448e47 100644 --- a/src/data_format.rs +++ b/src/data_format.rs @@ -211,39 +211,39 @@ impl<'a> From<(&'a Blocker, &'a CosmeticFilterCache)> for SerializeFormat<'a> { } } -impl Into<(Blocker, CosmeticFilterCache)> for DeserializeFormat { - fn into(self) -> (Blocker, CosmeticFilterCache) { +impl From for (Blocker, CosmeticFilterCache) { + fn from(format: DeserializeFormat) -> Self { (Blocker { - csp: self.part1.csp, - exceptions: self.part1.exceptions, - importants: self.part1.importants, - redirects: self.part1.redirects, - filters_tagged: self.part1.filters_tagged, - filters: self.part1.filters, + csp: format.part1.csp, + exceptions: format.part1.exceptions, + importants: format.part1.importants, + redirects: format.part1.redirects, + filters_tagged: format.part1.filters_tagged, + filters: format.part1.filters, tags_enabled: Default::default(), - tagged_filters_all: self.part1.tagged_filters_all, + tagged_filters_all: format.part1.tagged_filters_all, hot_filters: Default::default(), - enable_optimizations: self.part1.enable_optimizations, + enable_optimizations: format.part1.enable_optimizations, - resources: self.part1.resources, + resources: format.part1.resources, #[cfg(feature = "object-pooling")] pool: Default::default(), - generic_hide: self.rest.generic_hide, + generic_hide: format.rest.generic_hide, }, CosmeticFilterCache { - simple_class_rules: self.rest.simple_class_rules, - simple_id_rules: self.rest.simple_id_rules, - complex_class_rules: self.rest.complex_class_rules, - complex_id_rules: self.rest.complex_id_rules, + simple_class_rules: format.rest.simple_class_rules, + simple_id_rules: format.rest.simple_id_rules, + complex_class_rules: format.rest.complex_class_rules, + complex_id_rules: format.rest.complex_id_rules, - specific_rules: self.rest.specific_rules, + specific_rules: format.rest.specific_rules, - misc_generic_selectors: self.rest.misc_generic_selectors, + misc_generic_selectors: format.rest.misc_generic_selectors, - scriptlets: self.rest.scriptlets, + scriptlets: format.rest.scriptlets, }) } } diff --git a/src/filters/network.rs b/src/filters/network.rs index 2ea43e34..dc582443 100644 --- a/src/filters/network.rs +++ b/src/filters/network.rs @@ -424,7 +424,7 @@ fn validate_options(options: &[NetworkFilterOption]) -> Result<(), NetworkFilter | NfOpt::Websocket(..) | NfOpt::Font(..) )) { - Err(NetworkFilterError::CspWithContentType)?; + return Err(NetworkFilterError::CspWithContentType); } Ok(()) @@ -1144,43 +1144,40 @@ fn check_is_regex(filter: &str) -> bool { /// filters authors rely and different assumption. We can have prefix of suffix /// matches of anchor. fn is_anchored_by_hostname(filter_hostname: &str, hostname: &str, wildcard_filter_hostname: bool) -> bool { - let filter_hostname_len = filter_hostname.len(); - // Corner-case, if `filterHostname` is empty, then it's a match - if filter_hostname_len == 0 { - return true; - } - let hostname_len = hostname.len(); - - if filter_hostname_len > hostname_len { + match filter_hostname.len() { + // Corner-case, if `filterHostname` is empty, then it's a match + 0 => true, // `filterHostname` cannot be longer than actual hostname - false - } else if filter_hostname_len == hostname_len { + _ if filter_hostname.len() > hostname.len() => false, // If they have the same len(), they should be equal - filter_hostname == hostname - } else if let Some(match_index) = twoway::find_str(hostname, filter_hostname) { // Check if `filter_hostname` appears anywhere in `hostname` - if match_index == 0 { - // `filter_hostname` is a prefix of `hostname` and needs to match full a label. - // - // Examples (filter_hostname, hostname): - // * (foo, foo.com) - // * (sub.foo, sub.foo.com) - wildcard_filter_hostname || filter_hostname.ends_with('.') || hostname[filter_hostname_len..].starts_with('.') - } else if match_index == hostname_len - filter_hostname_len { - // `filter_hostname` is a suffix of `hostname`. - // - // Examples (filter_hostname, hostname): - // * (foo.com, sub.foo.com) - // * (com, foo.com) - filter_hostname.starts_with('.') || hostname[match_index - 1..].starts_with('.') - } else { - // `filter_hostname` is infix of `hostname` and needs match full labels - (wildcard_filter_hostname || filter_hostname.ends_with('.') || hostname[filter_hostname_len..].starts_with('.')) - && (filter_hostname.starts_with('.') || hostname[match_index - 1..].starts_with('.')) - } - } - else { - // No match - false + _ if filter_hostname.len() == hostname.len() => filter_hostname == hostname, + _ => { + match twoway::find_str(hostname, filter_hostname) { // Check if `filter_hostname` appears anywhere in `hostname` + Some(0) => { + // `filter_hostname` is a prefix of `hostname` and needs to match full a label. + // + // Examples (filter_hostname, hostname): + // * (foo, foo.com) + // * (sub.foo, sub.foo.com) + wildcard_filter_hostname || filter_hostname.ends_with('.') || hostname[filter_hostname.len()..].starts_with('.') + }, + Some(match_index) if match_index == hostname.len() - filter_hostname.len() => { + // `filter_hostname` is a suffix of `hostname`. + // + // Examples (filter_hostname, hostname): + // * (foo.com, sub.foo.com) + // * (com, foo.com) + filter_hostname.starts_with('.') || hostname[match_index - 1..].starts_with('.') + } + Some(match_index) => { + // `filter_hostname` is infix of `hostname` and needs match full labels + (wildcard_filter_hostname || filter_hostname.ends_with('.') || hostname[filter_hostname.len()..].starts_with('.')) + && (filter_hostname.starts_with('.') || hostname[match_index - 1..].starts_with('.')) + } + // No match + _ => false, + } + }, } } diff --git a/src/lists.rs b/src/lists.rs index 86294540..935ca6a9 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -270,7 +270,7 @@ pub fn parse_filter( filter } else { - &filter[..] + filter }; // Take the last of at most 2 whitespace separated fields diff --git a/src/utils.rs b/src/utils.rs index 7801da44..68c7be30 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -161,10 +161,8 @@ pub fn rules_from_lists(lists: &[String]) -> Vec { pub(crate) fn is_eof_error(e: &rmp_serde::decode::Error) -> bool { if let rmp_serde::decode::Error::InvalidMarkerRead(e) = e { - if e.kind() == std::io::ErrorKind::UnexpectedEof { - if format!("{}", e) == "failed to fill whole buffer" { - return true; - } + if e.kind() == std::io::ErrorKind::UnexpectedEof && format!("{}", e) == "failed to fill whole buffer" { + return true; } } false