Skip to content

Fix clippy lints #170

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/blocker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
32 changes: 11 additions & 21 deletions src/cosmetic_filter_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,21 @@ 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) {
if let Some(key) = &rule.key {
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 {
Expand Down Expand Up @@ -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[..])
}
}

Expand Down Expand Up @@ -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())
}
}
}
Expand Down
38 changes: 19 additions & 19 deletions src/data_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeserializeFormat> 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,
})
}
}
69 changes: 33 additions & 36 deletions src/filters/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ fn validate_options(options: &[NetworkFilterOption]) -> Result<(), NetworkFilter
| NfOpt::Websocket(..)
| NfOpt::Font(..)
)) {
Err(NetworkFilterError::CspWithContentType)?;
return Err(NetworkFilterError::CspWithContentType);
}

Ok(())
Expand Down Expand Up @@ -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,
}
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub fn parse_filter(

filter
} else {
&filter[..]
filter
};

// Take the last of at most 2 whitespace separated fields
Expand Down
6 changes: 2 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,8 @@ pub fn rules_from_lists(lists: &[String]) -> Vec<String> {

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
Expand Down