Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Clippy: Fixed some clippy warnings #31818

Merged
merged 5 commits into from Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/script/dom/headers.rs
Expand Up @@ -567,5 +567,5 @@ pub fn extract_mime_type(headers: &HyperHeaders) -> Option<Vec<u8>> {
}

// Step 7, 8
return mime_type.map(|m| format!("{}", m).into_bytes());
mime_type.map(|m| format!("{}", m).into_bytes())
}
6 changes: 3 additions & 3 deletions components/script/dom/history.rs
Expand Up @@ -121,7 +121,7 @@ impl History {
};
let global_scope = self.window.upcast::<GlobalScope>();
rooted!(in(*GlobalScope::get_cx()) let mut state = UndefinedValue());
if let Err(_) = structuredclone::read(global_scope, data, state.handle_mut()) {
if structuredclone::read(global_scope, data, state.handle_mut()).is_err() {
warn!("Error reading structuredclone data");
}
self.state.set(state.get());
Expand All @@ -136,7 +136,7 @@ impl History {
if state_changed {
PopStateEvent::dispatch_jsval(
self.window.upcast::<EventTarget>(),
&*self.window,
&self.window,
unsafe { HandleValue::from_raw(self.state.handle()) },
);
}
Expand Down Expand Up @@ -270,7 +270,7 @@ impl History {
// Step 11
let global_scope = self.window.upcast::<GlobalScope>();
rooted!(in(*cx) let mut state = UndefinedValue());
if let Err(_) = structuredclone::read(global_scope, serialized_data, state.handle_mut()) {
if structuredclone::read(global_scope, serialized_data, state.handle_mut()).is_err() {
warn!("Error reading structuredclone data");
}

Expand Down
12 changes: 6 additions & 6 deletions components/script/dom/htmlanchorelement.rs
Expand Up @@ -601,13 +601,13 @@ pub fn get_element_target(subject: &Element) -> Option<DOMString> {
Some(doc) => {
let element = doc.upcast::<Element>();
if element.has_attribute(&local_name!("target")) {
return Some(element.get_string_attribute(&local_name!("target")));
Some(element.get_string_attribute(&local_name!("target")))
} else {
return None;
None
}
},
None => return None,
};
None => None,
}
}

/// <https://html.spec.whatwg.org/multipage/#get-an-element's-noopener>
Expand All @@ -625,9 +625,9 @@ pub fn get_element_noopener(subject: &Element, target_attribute_value: Option<DO
Some(rel) => rel.Value(),
None => return target_is_blank,
};
return link_types.contains("noreferrer") ||
link_types.contains("noreferrer") ||
link_types.contains("noopener") ||
(!link_types.contains("opener") && target_is_blank);
(!link_types.contains("opener") && target_is_blank)
}

/// <https://html.spec.whatwg.org/multipage/#following-hyperlinks-2>
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/htmlareaelement.rs
Expand Up @@ -222,8 +222,8 @@ impl Area {
.iter()
.enumerate()
.map(|(index, point)| match index % 2 {
0 => point + p.x as f32,
_ => point + p.y as f32,
0 => point + p.x,
_ => point + p.y,
});
Area::Polygon {
points: iter.collect::<Vec<_>>(),
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/htmlbodyelement.rs
Expand Up @@ -145,7 +145,7 @@ impl VirtualMethods for HTMLBodyElement {
}

fn bind_to_tree(&self, context: &BindContext) {
if let Some(ref s) = self.super_type() {
if let Some(s) = self.super_type() {
s.bind_to_tree(context);
}

Expand Down
12 changes: 6 additions & 6 deletions components/script/dom/htmlbuttonelement.rs
Expand Up @@ -229,8 +229,8 @@ impl VirtualMethods for HTMLButtonElement {

fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) {
self.super_type().unwrap().attribute_mutated(attr, mutation);
match attr.local_name() {
&local_name!("disabled") => {
match *attr.local_name() {
local_name!("disabled") => {
let el = self.upcast::<Element>();
match mutation {
AttributeMutation::Set(Some(_)) => {},
Expand All @@ -248,7 +248,7 @@ impl VirtualMethods for HTMLButtonElement {
self.validity_state()
.perform_validation_and_update(ValidationFlags::all());
},
&local_name!("type") => match mutation {
local_name!("type") => match mutation {
AttributeMutation::Set(_) => {
let value = match &**attr.value() {
"reset" => ButtonType::Reset,
Expand All @@ -263,7 +263,7 @@ impl VirtualMethods for HTMLButtonElement {
self.button_type.set(ButtonType::Submit);
},
},
&local_name!("form") => {
local_name!("form") => {
self.form_attribute_mutated(mutation);
self.validity_state()
.perform_validation_and_update(ValidationFlags::empty());
Expand All @@ -273,7 +273,7 @@ impl VirtualMethods for HTMLButtonElement {
}

fn bind_to_tree(&self, context: &BindContext) {
if let Some(ref s) = self.super_type() {
if let Some(s) = self.super_type() {
s.bind_to_tree(context);
}

Expand Down Expand Up @@ -306,7 +306,7 @@ impl FormControl for HTMLButtonElement {
self.form_owner.set(form);
}

fn to_element<'a>(&'a self) -> &'a Element {
fn to_element(&self) -> &Element {
self.upcast::<Element>()
}
}
Expand Down
14 changes: 7 additions & 7 deletions components/script/dom/htmlcanvaselement.rs
Expand Up @@ -179,7 +179,7 @@ impl LayoutHTMLCanvasElementHelpers for LayoutDom<'_, HTMLCanvasElement> {
#[allow(unsafe_code)]
fn get_canvas_id_for_layout(self) -> CanvasId {
unsafe {
let canvas = &*self.unsafe_get();
let canvas = self.unsafe_get();
if let &Some(CanvasContext::Context2d(ref context)) = canvas.context.borrow_for_layout()
{
context.to_layout().get_canvas_id()
Expand Down Expand Up @@ -277,7 +277,7 @@ impl HTMLCanvasElement {
/// Gets the base WebGLRenderingContext for WebGL or WebGL 2, if exists.
pub fn get_base_webgl_context(&self) -> Option<DomRoot<WebGLRenderingContext>> {
match *self.context.borrow() {
Some(CanvasContext::WebGL(ref context)) => Some(DomRoot::from_ref(&*context)),
Some(CanvasContext::WebGL(ref context)) => Some(DomRoot::from_ref(context)),
Some(CanvasContext::WebGL2(ref context)) => Some(context.base_context()),
_ => None,
}
Expand Down Expand Up @@ -439,8 +439,8 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
/// <https://w3c.github.io/mediacapture-fromelement/#dom-htmlcanvaselement-capturestream>
fn CaptureStream(&self, _frame_request_rate: Option<Finite<f64>>) -> DomRoot<MediaStream> {
let global = self.global();
let stream = MediaStream::new(&*global);
let track = MediaStreamTrack::new(&*global, MediaStreamId::new(), MediaStreamType::Video);
let stream = MediaStream::new(&global);
let track = MediaStreamTrack::new(&global, MediaStreamId::new(), MediaStreamType::Video);
stream.AddTrack(&track);
stream
}
Expand All @@ -460,9 +460,9 @@ impl VirtualMethods for HTMLCanvasElement {
}

fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
match name {
&local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
&local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
match *name {
local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
_ => self
.super_type()
.unwrap()
Expand Down
6 changes: 3 additions & 3 deletions components/script/dom/htmlcollection.rs
Expand Up @@ -195,7 +195,7 @@ impl HTMLCollection {
None => elem.local_name() == qualified_name,
Some(prefix) => {
qualified_name.starts_with(&**prefix) &&
qualified_name.find(":") == Some(prefix.len()) &&
qualified_name.find(':') == Some(prefix.len()) &&
qualified_name.ends_with(&**elem.local_name())
},
}
Expand Down Expand Up @@ -295,9 +295,9 @@ impl HTMLCollection {
.filter(move |element| self.filter.filter(element, &self.root))
}

pub fn elements_iter<'a>(&'a self) -> impl Iterator<Item = DomRoot<Element>> + 'a {
pub fn elements_iter(&self) -> impl Iterator<Item = DomRoot<Element>> + '_ {
// Iterate forwards from the root.
self.elements_iter_after(&*self.root)
self.elements_iter_after(&self.root)
}

pub fn elements_iter_before<'a>(
Expand Down
30 changes: 15 additions & 15 deletions components/script/dom/htmlelement.rs
Expand Up @@ -560,11 +560,11 @@ static DATA_PREFIX: &str = "data-";
static DATA_HYPHEN_SEPARATOR: char = '\x2d';

fn is_ascii_uppercase(c: char) -> bool {
'A' <= c && c <= 'Z'
c.is_ascii_uppercase()
}

fn is_ascii_lowercase(c: char) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is now redundant, as it is not doing any actual work itself and simply forwards the call to the standard library method. So, we can just delete this function definition from this file and simply call .is_ascii_lowercase() wherever the deleted function is used. Please refer to my previous comment for an example of what needs to be done at the call site.

Similar refactoring needs to done for is_ascii_uppercase as well.

'a' <= c && c <= 'w'
c.is_ascii_lowercase()
}

fn to_snake_case(name: DOMString) -> DOMString {
Expand Down Expand Up @@ -623,7 +623,7 @@ impl HTMLElement {
.chars()
.skip_while(|&ch| ch != '\u{2d}')
.nth(1)
.map_or(false, |ch| ch >= 'a' && ch <= 'z')
.map_or(false, |ch| ch.is_ascii_lowercase())
{
return Err(Error::Syntax);
}
Expand Down Expand Up @@ -670,16 +670,16 @@ impl HTMLElement {
// https://html.spec.whatwg.org/multipage/#category-listed
pub fn is_listed_element(&self) -> bool {
match self.upcast::<Node>().type_id() {
NodeTypeId::Element(ElementTypeId::HTMLElement(type_id)) => match type_id {
NodeTypeId::Element(ElementTypeId::HTMLElement(type_id)) => matches!(
type_id,
HTMLElementTypeId::HTMLButtonElement |
HTMLElementTypeId::HTMLFieldSetElement |
HTMLElementTypeId::HTMLInputElement |
HTMLElementTypeId::HTMLObjectElement |
HTMLElementTypeId::HTMLOutputElement |
HTMLElementTypeId::HTMLSelectElement |
HTMLElementTypeId::HTMLTextAreaElement => true,
_ => false,
},
HTMLElementTypeId::HTMLFieldSetElement |
HTMLElementTypeId::HTMLInputElement |
HTMLElementTypeId::HTMLObjectElement |
HTMLElementTypeId::HTMLOutputElement |
HTMLElementTypeId::HTMLSelectElement |
HTMLElementTypeId::HTMLTextAreaElement
),
_ => false,
}
}
Expand Down Expand Up @@ -851,9 +851,9 @@ impl VirtualMethods for HTMLElement {
}

fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
match name {
&local_name!("itemprop") => AttrValue::from_serialized_tokenlist(value.into()),
&local_name!("itemtype") => AttrValue::from_serialized_tokenlist(value.into()),
match *name {
local_name!("itemprop") => AttrValue::from_serialized_tokenlist(value.into()),
local_name!("itemtype") => AttrValue::from_serialized_tokenlist(value.into()),
_ => self
.super_type()
.unwrap()
Expand Down
8 changes: 4 additions & 4 deletions components/script/dom/htmlfieldsetelement.rs
Expand Up @@ -158,8 +158,8 @@ impl VirtualMethods for HTMLFieldSetElement {

fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) {
self.super_type().unwrap().attribute_mutated(attr, mutation);
match attr.local_name() {
&local_name!("disabled") => {
match *attr.local_name() {
local_name!("disabled") => {
let disabled_state = match mutation {
AttributeMutation::Set(None) => true,
AttributeMutation::Set(Some(_)) => {
Expand Down Expand Up @@ -219,7 +219,7 @@ impl VirtualMethods for HTMLFieldSetElement {
}
el.update_sequentially_focusable_status();
},
&local_name!("form") => {
local_name!("form") => {
self.form_attribute_mutated(mutation);
},
_ => {},
Expand All @@ -236,7 +236,7 @@ impl FormControl for HTMLFieldSetElement {
self.form_owner.set(form);
}

fn to_element<'a>(&'a self) -> &'a Element {
fn to_element(&self) -> &Element {
self.upcast::<Element>()
}
}
Expand Down
12 changes: 6 additions & 6 deletions components/script/dom/htmlfontelement.rs
Expand Up @@ -92,10 +92,10 @@ impl VirtualMethods for HTMLFontElement {
}

fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
match name {
&local_name!("face") => AttrValue::from_atomic(value.into()),
&local_name!("color") => AttrValue::from_legacy_color(value.into()),
&local_name!("size") => parse_size(&value),
match *name {
local_name!("face") => AttrValue::from_atomic(value.into()),
local_name!("color") => AttrValue::from_legacy_color(value.into()),
local_name!("size") => parse_size(&value),
_ => self
.super_type()
.unwrap()
Expand Down Expand Up @@ -174,8 +174,8 @@ fn parse_size(mut input: &str) -> AttrValue {

// Step 9
match parse_mode {
ParseMode::RelativePlus => value = 3 + value,
ParseMode::RelativeMinus => value = 3 - value,
ParseMode::RelativePlus => value += 3,
ParseMode::RelativeMinus => value -= 3,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since subtraction is not commutative, this change is not equivalent to the original code i.e value -= 3 expands to value = value - 3 whereas we want value = 3 - value

I think we can simply revert the change on line 178 alone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reverted the changes, it should be fine now.

ParseMode::Absolute => (),
}

Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/htmlformcontrolscollection.rs
Expand Up @@ -35,7 +35,7 @@ impl HTMLFormControlsCollection {
.upcast::<Node>()
.GetRootNode(&GetRootNodeOptions::empty());
HTMLFormControlsCollection {
collection: HTMLCollection::new_inherited(&*root_of_form, filter),
collection: HTMLCollection::new_inherited(&root_of_form, filter),
form: Dom::from_ref(form),
}
}
Expand Down Expand Up @@ -92,7 +92,7 @@ impl HTMLFormControlsCollectionMethods for HTMLFormControlsCollection {
// specifically HTMLFormElement::Elements(),
// and the collection filter excludes image inputs.
Some(RadioNodeListOrElement::RadioNodeList(
RadioNodeList::new_controls_except_image_inputs(window, &*self.form, &name),
RadioNodeList::new_controls_except_image_inputs(window, &self.form, &name),
))
}
// Step 3
Expand Down