Skip to content

Commit

Permalink
clippy: fix warnings in components/shared (#31565)
Browse files Browse the repository at this point in the history
* clippy: fix some warnings in components/shared

* fix: unit tests

* fix: review comments
  • Loading branch information
eerii committed Mar 8, 2024
1 parent 3a5ca78 commit 43f4496
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 138 deletions.
4 changes: 2 additions & 2 deletions components/shared/bluetooth/scanfilter.rs
Expand Up @@ -49,7 +49,7 @@ impl BluetoothScanfilter {
name,
name_prefix,
services: ServiceUUIDSequence::new(services),
manufacturer_data: manufacturer_data,
manufacturer_data,
service_data,
}
}
Expand Down Expand Up @@ -124,7 +124,7 @@ impl RequestDeviceoptions {
services: ServiceUUIDSequence,
) -> RequestDeviceoptions {
RequestDeviceoptions {
filters: filters,
filters,
optional_services: services,
}
}
Expand Down
51 changes: 18 additions & 33 deletions components/shared/canvas/canvas.rs
Expand Up @@ -115,11 +115,11 @@ impl LinearGradientStyle {
stops: Vec<CanvasGradientStop>,
) -> LinearGradientStyle {
LinearGradientStyle {
x0: x0,
y0: y0,
x1: x1,
y1: y1,
stops: stops,
x0,
y0,
x1,
y1,
stops,
}
}
}
Expand All @@ -146,13 +146,13 @@ impl RadialGradientStyle {
stops: Vec<CanvasGradientStop>,
) -> RadialGradientStyle {
RadialGradientStyle {
x0: x0,
y0: y0,
r0: r0,
x1: x1,
y1: y1,
r1: r1,
stops: stops,
x0,
y0,
r0,
x1,
y1,
r1,
stops,
}
}
}
Expand Down Expand Up @@ -402,8 +402,9 @@ impl FromStr for CompositionOrBlending {
}
}

#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
#[derive(Clone, Copy, Debug, Default, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub enum TextAlign {
#[default]
Start,
End,
Left,
Expand All @@ -426,17 +427,12 @@ impl FromStr for TextAlign {
}
}

impl Default for TextAlign {
fn default() -> TextAlign {
TextAlign::Start
}
}

#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
#[derive(Clone, Copy, Debug, Default, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub enum TextBaseline {
Top,
Hanging,
Middle,
#[default]
Alphabetic,
Ideographic,
Bottom,
Expand All @@ -458,16 +454,11 @@ impl FromStr for TextBaseline {
}
}

impl Default for TextBaseline {
fn default() -> TextBaseline {
TextBaseline::Alphabetic
}
}

#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
#[derive(Clone, Copy, Debug, Default, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub enum Direction {
Ltr,
Rtl,
#[default]
Inherit,
}

Expand All @@ -483,9 +474,3 @@ impl FromStr for Direction {
}
}
}

impl Default for Direction {
fn default() -> Direction {
Direction::Inherit
}
}
29 changes: 13 additions & 16 deletions components/shared/canvas/webgl.rs
Expand Up @@ -140,10 +140,7 @@ pub struct WebGLMsgSender {

impl WebGLMsgSender {
pub fn new(id: WebGLContextId, sender: WebGLChan) -> Self {
WebGLMsgSender {
ctx_id: id,
sender: sender,
}
WebGLMsgSender { ctx_id: id, sender }
}

/// Returns the WebGLContextId associated to this sender
Expand Down Expand Up @@ -922,7 +919,7 @@ mod gl_ext_constants {
pub const COMPRESSED_RGBA_S3TC_DXT5_EXT: GLenum = 0x83F3;
pub const COMPRESSED_RGB_ETC1_WEBGL: GLenum = 0x8D64;

pub static COMPRESSIONS: &'static [GLenum] = &[
pub static COMPRESSIONS: &[GLenum] = &[
COMPRESSED_RGB_S3TC_DXT1_EXT,
COMPRESSED_RGBA_S3TC_DXT1_EXT,
COMPRESSED_RGBA_S3TC_DXT3_EXT,
Expand Down Expand Up @@ -1061,18 +1058,18 @@ impl TexFormat {

/// Returns whether this format is a known sized or unsized format.
pub fn is_sized(&self) -> bool {
match self {
!matches!(
self,
TexFormat::DepthComponent |
TexFormat::DepthStencil |
TexFormat::Alpha |
TexFormat::Red |
TexFormat::RG |
TexFormat::RGB |
TexFormat::RGBA |
TexFormat::Luminance |
TexFormat::LuminanceAlpha => false,
_ => true,
}
TexFormat::DepthStencil |
TexFormat::Alpha |
TexFormat::Red |
TexFormat::RG |
TexFormat::RGB |
TexFormat::RGBA |
TexFormat::Luminance |
TexFormat::LuminanceAlpha
)
}

pub fn to_unsized(self) -> TexFormat {
Expand Down
8 changes: 4 additions & 4 deletions components/shared/gfx/lib.rs
Expand Up @@ -25,9 +25,9 @@ impl Epoch {
}
}

impl Into<WebRenderEpoch> for Epoch {
fn into(self) -> WebRenderEpoch {
WebRenderEpoch(self.0)
impl From<Epoch> for WebRenderEpoch {
fn from(val: Epoch) -> Self {
WebRenderEpoch(val.0)
}
}

Expand Down Expand Up @@ -114,7 +114,7 @@ pub fn combine_id_with_fragment_type(id: usize, fragment_type: FragmentType) ->

pub fn node_id_from_scroll_id(id: usize) -> Option<usize> {
if (id & !SPECIAL_SCROLL_ROOT_ID_MASK) != 0 {
return Some((id & !3) as usize);
return Some(id & !3);
}
None
}
Expand Down
10 changes: 5 additions & 5 deletions components/shared/gfx/print_tree.rs
Expand Up @@ -28,17 +28,17 @@ impl PrintTree {

self.print_level_prefix();

let items: Vec<&str> = queued_title.split("\n").collect();
let items: Vec<&str> = queued_title.split('\n').collect();
println!("\u{251C}\u{2500} {}", items[0]);
for i in 1..items.len() {
self.print_level_child_indentation();
print!("{}", items[i]);
if i < items.len() {
print!("\n");
println!();
}
}

self.level = self.level + 1;
self.level += 1;
}

/// Ascend one level in the tree.
Expand Down Expand Up @@ -69,13 +69,13 @@ impl PrintTree {
fn flush_queued_item(&mut self, prefix: &str) {
if let Some(queued_item) = self.queued_item.take() {
self.print_level_prefix();
let items: Vec<&str> = queued_item.split("\n").collect();
let items: Vec<&str> = queued_item.split('\n').collect();
println!("{} {}", prefix, items[0]);
for i in 1..items.len() {
self.print_level_child_indentation();
print!("{}", items[i]);
if i < items.len() {
print!("\n");
println!();
}
}
}
Expand Down
23 changes: 14 additions & 9 deletions components/shared/msg/constellation_msg.rs
Expand Up @@ -5,6 +5,8 @@
//! The high-level interface from script to constellation. Using this abstract interface helps
//! reduce coupling between these two components.

#![allow(clippy::new_without_default)]

use std::cell::Cell;
use std::num::NonZeroU32;
use std::sync::Arc;
Expand Down Expand Up @@ -80,17 +82,19 @@ pub struct PipelineNamespaceInstaller {
namespace_receiver: IpcReceiver<PipelineNamespaceId>,
}

impl PipelineNamespaceInstaller {
pub fn new() -> Self {
impl Default for PipelineNamespaceInstaller {
fn default() -> Self {
let (namespace_sender, namespace_receiver) =
ipc::channel().expect("PipelineNamespaceInstaller ipc channel failure");
PipelineNamespaceInstaller {
Self {
request_sender: None,
namespace_sender: namespace_sender,
namespace_receiver: namespace_receiver,
namespace_sender,
namespace_receiver,
}
}
}

impl PipelineNamespaceInstaller {
/// Provide a request sender to send requests to the constellation.
pub fn set_sender(&mut self, sender: IpcSender<PipelineNamespaceRequest>) {
self.request_sender = Some(sender);
Expand Down Expand Up @@ -121,7 +125,7 @@ lazy_static! {
///
/// Use PipelineNamespace::fetch_install to install a unique pipeline-namespace from the calling thread.
static ref PIPELINE_NAMESPACE_INSTALLER: Arc<Mutex<PipelineNamespaceInstaller>> =
Arc::new(Mutex::new(PipelineNamespaceInstaller::new()));
Arc::new(Mutex::new(PipelineNamespaceInstaller::default()));
}

/// Each pipeline ID needs to be unique. However, it also needs to be possible to
Expand Down Expand Up @@ -247,7 +251,7 @@ size_of_test!(BrowsingContextId, 8);
size_of_test!(Option<BrowsingContextId>, 8);

impl BrowsingContextId {
pub fn new() -> BrowsingContextId {
pub fn new() -> Self {
PIPELINE_NAMESPACE.with(|tls| {
let mut namespace = tls.get().expect("No namespace set for this thread!");
let new_browsing_context_id = namespace.next_browsing_context_id();
Expand Down Expand Up @@ -292,6 +296,7 @@ impl TopLevelBrowsingContextId {
pub fn new() -> TopLevelBrowsingContextId {
TopLevelBrowsingContextId(BrowsingContextId::new())
}

/// Each script and layout thread should have the top-level browsing context id installed,
/// since it is used by crash reporting.
pub fn install(id: TopLevelBrowsingContextId) {
Expand Down Expand Up @@ -544,15 +549,15 @@ impl fmt::Debug for HangAlert {
"\n The following component is experiencing a transient hang: \n {:?}",
component_id
)?;
(annotation.clone(), None)
(*annotation, None)
},
HangAlert::Permanent(component_id, annotation, profile) => {
write!(
fmt,
"\n The following component is experiencing a permanent hang: \n {:?}",
component_id
)?;
(annotation.clone(), profile.clone())
(*annotation, profile.clone())
},
};

Expand Down
2 changes: 1 addition & 1 deletion components/shared/net/fetch/headers.rs
Expand Up @@ -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][..]))
}
4 changes: 2 additions & 2 deletions components/shared/net/image/base.rs
Expand Up @@ -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,
})
Expand Down
7 changes: 2 additions & 5 deletions components/shared/net/image_cache.rs
Expand Up @@ -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) {
Expand All @@ -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,
});
}
Expand Down
20 changes: 10 additions & 10 deletions components/shared/net/lib.rs
Expand Up @@ -94,9 +94,9 @@ impl CustomResponse {
body: Vec<u8>,
) -> CustomResponse {
CustomResponse {
headers: headers,
raw_status: raw_status,
body: body,
headers,
raw_status,
body,
}
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 43f4496

Please sign in to comment.