clippy: fix warnings in components/shared (#31565)

* clippy: fix some warnings in components/shared

* fix: unit tests

* fix: review comments
This commit is contained in:
eri 2024-03-08 16:28:19 +01:00 committed by GitHub
parent 3a5ca785d3
commit 43f44965cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 102 additions and 139 deletions

View file

@ -14,5 +14,5 @@ pub fn get_value_from_header_list(name: &str, headers: &HeaderMap) -> Option<Vec
}
// Step 2
return Some(values.collect::<Vec<&[u8]>>().join(&[0x2C, 0x20][..]));
Some(values.collect::<Vec<&[u8]>>().join(&[0x2C, 0x20][..]))
}

View file

@ -59,12 +59,12 @@ pub fn load_from_memory(buffer: &[u8], cors_status: CorsStatus) -> Option<Image>
Ok(_) => match image::load_from_memory(buffer) {
Ok(image) => {
let mut rgba = image.into_rgba8();
pixels::rgba8_byte_swap_colors_inplace(&mut *rgba);
pixels::rgba8_byte_swap_colors_inplace(&mut rgba);
Some(Image {
width: rgba.width(),
height: rgba.height(),
format: PixelFormat::BGRA8,
bytes: IpcSharedMemory::from_bytes(&*rgba),
bytes: IpcSharedMemory::from_bytes(&rgba),
id: None,
cors_status,
})

View file

@ -42,10 +42,7 @@ pub struct ImageResponder {
impl ImageResponder {
pub fn new(sender: IpcSender<PendingImageResponse>, id: PendingImageId) -> ImageResponder {
ImageResponder {
sender: sender,
id: id,
}
ImageResponder { sender, id }
}
pub fn respond(&self, response: ImageResponse) {
@ -54,7 +51,7 @@ impl ImageResponder {
// That's not a case that's worth warning about.
// TODO(#15501): are there cases in which we should perform cleanup?
let _ = self.sender.send(PendingImageResponse {
response: response,
response,
id: self.id,
});
}

View file

@ -94,9 +94,9 @@ impl CustomResponse {
body: Vec<u8>,
) -> CustomResponse {
CustomResponse {
headers: headers,
raw_status: raw_status,
body: body,
headers,
raw_status,
body,
}
}
}
@ -567,7 +567,7 @@ pub enum ResourceTimingType {
impl ResourceFetchTiming {
pub fn new(timing_type: ResourceTimingType) -> ResourceFetchTiming {
ResourceFetchTiming {
timing_type: timing_type,
timing_type,
timing_check_passed: true,
domain_lookup_start: 0,
redirect_count: 0,
@ -587,12 +587,12 @@ impl ResourceFetchTiming {
// TODO currently this is being set with precise time ns when it should be time since
// time origin (as described in Performance::now)
pub fn set_attribute(&mut self, attribute: ResourceAttribute) {
let should_attribute_always_be_updated = match attribute {
let should_attribute_always_be_updated = matches!(
attribute,
ResourceAttribute::FetchStart |
ResourceAttribute::ResponseEnd |
ResourceAttribute::StartTime(_) => true,
_ => false,
};
ResourceAttribute::ResponseEnd |
ResourceAttribute::StartTime(_)
);
if !self.timing_check_passed && !should_attribute_always_be_updated {
return;
}
@ -782,7 +782,7 @@ impl NetworkError {
/// Normalize `slice`, as defined by
/// [the Fetch Spec](https://fetch.spec.whatwg.org/#concept-header-value-normalize).
pub fn trim_http_whitespace(mut slice: &[u8]) -> &[u8] {
const HTTP_WS_BYTES: &'static [u8] = b"\x09\x0A\x0D\x20";
const HTTP_WS_BYTES: &[u8] = b"\x09\x0A\x0D\x20";
loop {
match slice.split_first() {

View file

@ -21,7 +21,7 @@ use embedder_traits::resources::{self, Resource};
use lazy_static::lazy_static;
use servo_url::{Host, ImmutableOrigin, ServoUrl};
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct PubDomainRules {
rules: HashSet<String>,
wildcards: HashSet<String>,
@ -37,12 +37,12 @@ impl<'a> FromIterator<&'a str> for PubDomainRules {
where
T: IntoIterator<Item = &'a str>,
{
let mut result = PubDomainRules::new();
let mut result = PubDomainRules::default();
for item in iter {
if item.starts_with("!") {
result.exceptions.insert(String::from(&item[1..]));
} else if item.starts_with("*.") {
result.wildcards.insert(String::from(&item[2..]));
if let Some(stripped) = item.strip_prefix('!') {
result.exceptions.insert(String::from(stripped));
} else if let Some(stripped) = item.strip_prefix("*.") {
result.wildcards.insert(String::from(stripped));
} else {
result.rules.insert(String::from(item));
}
@ -52,13 +52,6 @@ impl<'a> FromIterator<&'a str> for PubDomainRules {
}
impl PubDomainRules {
pub fn new() -> PubDomainRules {
PubDomainRules {
rules: HashSet::new(),
wildcards: HashSet::new(),
exceptions: HashSet::new(),
}
}
pub fn parse(content: &str) -> PubDomainRules {
content
.lines()
@ -68,23 +61,21 @@ impl PubDomainRules {
.collect()
}
fn suffix_pair<'a>(&self, domain: &'a str) -> (&'a str, &'a str) {
let domain = domain.trim_start_matches(".");
let domain = domain.trim_start_matches('.');
let mut suffix = domain;
let mut prev_suffix = domain;
for (index, _) in domain.match_indices(".") {
for (index, _) in domain.match_indices('.') {
let next_suffix = &domain[index + 1..];
if self.exceptions.contains(suffix) {
return (next_suffix, suffix);
} else if self.wildcards.contains(next_suffix) {
return (suffix, prev_suffix);
} else if self.rules.contains(suffix) {
return (suffix, prev_suffix);
} else {
prev_suffix = suffix;
suffix = next_suffix;
}
if self.wildcards.contains(next_suffix) || self.rules.contains(suffix) {
return (suffix, prev_suffix);
}
prev_suffix = suffix;
suffix = next_suffix;
}
return (suffix, prev_suffix);
(suffix, prev_suffix)
}
pub fn public_suffix<'a>(&self, domain: &'a str) -> &'a str {
let (public, _) = self.suffix_pair(domain);
@ -98,8 +89,8 @@ impl PubDomainRules {
// Speeded-up version of
// domain != "" &&
// self.public_suffix(domain) == domain.
let domain = domain.trim_start_matches(".");
match domain.find(".") {
let domain = domain.trim_start_matches('.');
match domain.find('.') {
None => !domain.is_empty(),
Some(index) => {
!self.exceptions.contains(domain) && self.wildcards.contains(&domain[index + 1..]) ||
@ -111,8 +102,8 @@ impl PubDomainRules {
// Speeded-up version of
// self.public_suffix(domain) != domain &&
// self.registrable_suffix(domain) == domain.
let domain = domain.trim_start_matches(".");
match domain.find(".") {
let domain = domain.trim_start_matches('.');
match domain.find('.') {
None => false,
Some(index) => {
self.exceptions.contains(domain) ||
@ -151,7 +142,7 @@ pub fn is_reg_domain(domain: &str) -> bool {
pub fn reg_host(url: &ServoUrl) -> Option<Host> {
match url.origin() {
ImmutableOrigin::Tuple(_, Host::Domain(domain), _) => {
Some(Host::Domain(String::from(reg_suffix(&*domain))))
Some(Host::Domain(String::from(reg_suffix(&domain))))
},
ImmutableOrigin::Tuple(_, ip, _) => Some(ip),
ImmutableOrigin::Opaque(_) => None,

View file

@ -264,7 +264,7 @@ impl RequestBuilder {
pub fn new(url: ServoUrl, referrer: Referrer) -> RequestBuilder {
RequestBuilder {
method: Method::GET,
url: url,
url,
headers: HeaderMap::new(),
unsafe_request: false,
body: None,
@ -277,7 +277,7 @@ impl RequestBuilder {
credentials_mode: CredentialsMode::CredentialsSameOrigin,
use_url_credentials: false,
origin: ImmutableOrigin::new_opaque(),
referrer: referrer,
referrer,
referrer_policy: None,
pipeline_id: None,
redirect_mode: RedirectMode::Follow,
@ -524,9 +524,9 @@ impl Request {
initiator: Initiator::None,
destination: Destination::None,
origin: origin.unwrap_or(Origin::Client),
referrer: referrer,
referrer,
referrer_policy: None,
pipeline_id: pipeline_id,
pipeline_id,
synchronous: false,
mode: RequestMode::NoCors,
use_cors_preflight: false,
@ -540,7 +540,7 @@ impl Request {
redirect_count: 0,
response_tainting: ResponseTainting::Basic,
csp_list: None,
https_state: https_state,
https_state,
crash: None,
}
}

View file

@ -189,10 +189,7 @@ impl Response {
}
pub fn is_network_error(&self) -> bool {
match self.response_type {
ResponseType::Error(..) => true,
_ => false,
}
matches!(self.response_type, ResponseType::Error(..))
}
pub fn get_network_error(&self) -> Option<&NetworkError> {
@ -204,7 +201,7 @@ impl Response {
pub fn actual_response(&self) -> &Response {
if self.return_internal && self.internal_response.is_some() {
&**self.internal_response.as_ref().unwrap()
self.internal_response.as_ref().unwrap()
} else {
self
}
@ -212,7 +209,7 @@ impl Response {
pub fn actual_response_mut(&mut self) -> &mut Response {
if self.return_internal && self.internal_response.is_some() {
&mut **self.internal_response.as_mut().unwrap()
self.internal_response.as_mut().unwrap()
} else {
self
}
@ -258,10 +255,7 @@ impl Response {
ResponseType::Basic => {
let headers = old_headers.iter().filter(|(name, _)| {
match &*name.as_str().to_ascii_lowercase() {
"set-cookie" | "set-cookie2" => false,
_ => true
}
!matches!(&*name.as_str().to_ascii_lowercase(), "set-cookie" | "set-cookie2")
}).map(|(n, v)| (n.clone(), v.clone())).collect();
response.headers = headers;
},
@ -315,7 +309,7 @@ impl Response {
metadata.status = response.raw_status.clone();
metadata.https_state = response.https_state;
metadata.referrer = response.referrer.clone();
metadata.referrer_policy = response.referrer_policy.clone();
metadata.referrer_policy = response.referrer_policy;
metadata.redirected = response.actual_response().url_list.len() > 1;
metadata
}