clippy: Fix comparison_* warnings (#32058)

This commit is contained in:
eri 2024-04-12 10:08:38 +02:00 committed by GitHub
parent 509b858f15
commit 88d4aff595
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 92 additions and 78 deletions

View file

@ -68,7 +68,7 @@ impl DOMTokenList {
}
}
// https://dom.spec.whatwg.org/#concept-dtl-update
/// <https://dom.spec.whatwg.org/#concept-dtl-update>
fn perform_update_steps(&self, atoms: Vec<Atom>) {
// Step 1
if !self.element.has_attribute(&self.local_name) && atoms.is_empty() {
@ -79,7 +79,7 @@ impl DOMTokenList {
.set_atomic_tokenlist_attribute(&self.local_name, atoms)
}
// https://dom.spec.whatwg.org/#concept-domtokenlist-validation
/// <https://dom.spec.whatwg.org/#concept-domtokenlist-validation>
fn validation_steps(&self, token: &str) -> Fallible<bool> {
match &self.supported_tokens {
None => Err(Error::Type(
@ -99,15 +99,15 @@ impl DOMTokenList {
}
}
// https://dom.spec.whatwg.org/#domtokenlist
/// <https://dom.spec.whatwg.org/#domtokenlist>
impl DOMTokenListMethods for DOMTokenList {
// https://dom.spec.whatwg.org/#dom-domtokenlist-length
/// <https://dom.spec.whatwg.org/#dom-domtokenlist-length>
fn Length(&self) -> u32 {
self.attribute()
.map_or(0, |attr| attr.value().as_tokens().len()) as u32
}
// https://dom.spec.whatwg.org/#dom-domtokenlist-item
/// <https://dom.spec.whatwg.org/#dom-domtokenlist-item>
fn Item(&self, index: u32) -> Option<DOMString> {
self.attribute().and_then(|attr| {
// FIXME(ajeffrey): Convert directly from Atom to DOMString
@ -118,7 +118,7 @@ impl DOMTokenListMethods for DOMTokenList {
})
}
// https://dom.spec.whatwg.org/#dom-domtokenlist-contains
/// <https://dom.spec.whatwg.org/#dom-domtokenlist-contains>
fn Contains(&self, token: DOMString) -> bool {
let token = Atom::from(token);
self.attribute().map_or(false, |attr| {
@ -129,7 +129,7 @@ impl DOMTokenListMethods for DOMTokenList {
})
}
// https://dom.spec.whatwg.org/#dom-domtokenlist-add
/// <https://dom.spec.whatwg.org/#dom-domtokenlist-add>
fn Add(&self, tokens: Vec<DOMString>) -> ErrorResult {
let mut atoms = self.element.get_tokenlist_attribute(&self.local_name);
for token in &tokens {
@ -142,7 +142,7 @@ impl DOMTokenListMethods for DOMTokenList {
Ok(())
}
// https://dom.spec.whatwg.org/#dom-domtokenlist-remove
/// <https://dom.spec.whatwg.org/#dom-domtokenlist-remove>
fn Remove(&self, tokens: Vec<DOMString>) -> ErrorResult {
let mut atoms = self.element.get_tokenlist_attribute(&self.local_name);
for token in &tokens {
@ -156,7 +156,7 @@ impl DOMTokenListMethods for DOMTokenList {
Ok(())
}
// https://dom.spec.whatwg.org/#dom-domtokenlist-toggle
/// <https://dom.spec.whatwg.org/#dom-domtokenlist-toggle>
fn Toggle(&self, token: DOMString, force: Option<bool>) -> Fallible<bool> {
let mut atoms = self.element.get_tokenlist_attribute(&self.local_name);
let token = self.check_token_exceptions(&token)?;
@ -180,18 +180,18 @@ impl DOMTokenListMethods for DOMTokenList {
}
}
// https://dom.spec.whatwg.org/#dom-domtokenlist-value
/// <https://dom.spec.whatwg.org/#dom-domtokenlist-value>
fn Value(&self) -> DOMString {
self.element.get_string_attribute(&self.local_name)
}
// https://dom.spec.whatwg.org/#dom-domtokenlist-value
/// <https://dom.spec.whatwg.org/#dom-domtokenlist-value>
fn SetValue(&self, value: DOMString) {
self.element
.set_tokenlist_attribute(&self.local_name, value);
}
// https://dom.spec.whatwg.org/#dom-domtokenlist-replace
/// <https://dom.spec.whatwg.org/#dom-domtokenlist-replace>
fn Replace(&self, token: DOMString, new_token: DOMString) -> Fallible<bool> {
if token.is_empty() || new_token.is_empty() {
// Step 1.
@ -207,23 +207,27 @@ impl DOMTokenListMethods for DOMTokenList {
let mut atoms = self.element.get_tokenlist_attribute(&self.local_name);
let mut result = false;
if let Some(pos) = atoms.iter().position(|atom| *atom == token) {
if let Some(redundant_pos) = atoms.iter().position(|atom| *atom == new_token) {
if redundant_pos > pos {
match atoms.iter().position(|atom| *atom == new_token) {
Some(redundant_pos) if redundant_pos > pos => {
// The replacement is already in the list, later,
// so we perform the replacement and remove the
// later copy.
atoms[pos] = new_token;
atoms.remove(redundant_pos);
} else if redundant_pos < pos {
},
Some(redundant_pos) if redundant_pos < pos => {
// The replacement is already in the list, earlier,
// so we remove the index where we'd be putting the
// later copy.
atoms.remove(pos);
}
// else we are replacing the token with itself, nothing to change
} else {
// The replacement is not in the list already
atoms[pos] = new_token;
},
Some(_) => {
// Else we are replacing the token with itself, nothing to change
},
None => {
// The replacement is not in the list already
atoms[pos] = new_token;
},
}
// Step 5.
@ -233,7 +237,7 @@ impl DOMTokenListMethods for DOMTokenList {
Ok(result)
}
// https://dom.spec.whatwg.org/#dom-domtokenlist-supports
/// <https://dom.spec.whatwg.org/#dom-domtokenlist-supports>
fn Supports(&self, token: DOMString) -> Fallible<bool> {
self.validation_steps(&token)
}

View file

@ -3,6 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use std::cmp::Ordering;
use dom_struct::dom_struct;
use js::jsapi::Heap;
@ -37,7 +38,7 @@ enum PushOrReplace {
Replace,
}
// https://html.spec.whatwg.org/multipage/#the-history-interface
/// <https://html.spec.whatwg.org/multipage/#the-history-interface>
#[dom_struct]
pub struct History {
reflector_: Reflector,
@ -79,8 +80,8 @@ impl History {
Ok(())
}
// https://html.spec.whatwg.org/multipage/#history-traversal
// Steps 5-16
/// <https://html.spec.whatwg.org/multipage/#history-traversal>
/// Steps 5-16
#[allow(unsafe_code)]
pub fn activate_state(&self, state_id: Option<HistoryStateId>, url: ServoUrl) {
// Steps 5
@ -165,8 +166,8 @@ impl History {
.send(CoreResourceMsg::RemoveHistoryStates(states));
}
// https://html.spec.whatwg.org/multipage/#dom-history-pushstate
// https://html.spec.whatwg.org/multipage/#dom-history-replacestate
/// <https://html.spec.whatwg.org/multipage/#dom-history-pushstate>
/// <https://html.spec.whatwg.org/multipage/#dom-history-replacestate>
fn push_or_replace_state(
&self,
cx: JSContext,
@ -285,7 +286,7 @@ impl History {
}
impl HistoryMethods for History {
// https://html.spec.whatwg.org/multipage/#dom-history-state
/// <https://html.spec.whatwg.org/multipage/#dom-history-state>
fn GetState(&self, _cx: JSContext) -> Fallible<JSVal> {
if !self.window.Document().is_fully_active() {
return Err(Error::Security);
@ -293,7 +294,7 @@ impl HistoryMethods for History {
Ok(self.state.get())
}
// https://html.spec.whatwg.org/multipage/#dom-history-length
/// <https://html.spec.whatwg.org/multipage/#dom-history-length>
fn GetLength(&self) -> Fallible<u32> {
if !self.window.Document().is_fully_active() {
return Err(Error::Security);
@ -309,30 +310,28 @@ impl HistoryMethods for History {
Ok(recv.recv().unwrap())
}
// https://html.spec.whatwg.org/multipage/#dom-history-go
/// <https://html.spec.whatwg.org/multipage/#dom-history-go>
fn Go(&self, delta: i32) -> ErrorResult {
let direction = if delta > 0 {
TraversalDirection::Forward(delta as usize)
} else if delta < 0 {
TraversalDirection::Back(-delta as usize)
} else {
return self.window.Location().Reload();
let direction = match delta.cmp(&0) {
Ordering::Greater => TraversalDirection::Forward(delta as usize),
Ordering::Less => TraversalDirection::Back(-delta as usize),
Ordering::Equal => return self.window.Location().Reload(),
};
self.traverse_history(direction)
}
// https://html.spec.whatwg.org/multipage/#dom-history-back
/// <https://html.spec.whatwg.org/multipage/#dom-history-back>
fn Back(&self) -> ErrorResult {
self.traverse_history(TraversalDirection::Back(1))
}
// https://html.spec.whatwg.org/multipage/#dom-history-forward
/// <https://html.spec.whatwg.org/multipage/#dom-history-forward>
fn Forward(&self) -> ErrorResult {
self.traverse_history(TraversalDirection::Forward(1))
}
// https://html.spec.whatwg.org/multipage/#dom-history-pushstate
/// <https://html.spec.whatwg.org/multipage/#dom-history-pushstate>
fn PushState(
&self,
cx: JSContext,
@ -343,7 +342,7 @@ impl HistoryMethods for History {
self.push_or_replace_state(cx, data, title, url, PushOrReplace::Push)
}
// https://html.spec.whatwg.org/multipage/#dom-history-replacestate
/// <https://html.spec.whatwg.org/multipage/#dom-history-replacestate>
fn ReplaceState(
&self,
cx: JSContext,

View file

@ -3,6 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use std::cmp::Ordering;
use dom_struct::dom_struct;
use html5ever::{local_name, namespace_url, ns, LocalName, QualName};
@ -24,9 +25,9 @@ pub trait CollectionFilter: JSTraceable {
fn filter<'a>(&self, elem: &'a Element, root: &'a Node) -> bool;
}
// An optional u32, using maxint to represent None.
// It would be nicer just to use Option<u32> for this, but that would produce word
// alignment issues since Option<u32> uses 33 bits.
/// An optional u32, using maxint to represent None.
/// It would be nicer just to use Option<u32> for this, but that would produce word
/// alignment issues since Option<u32> uses 33 bits.
#[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
struct OptionU32 {
bits: u32,
@ -146,7 +147,7 @@ impl HTMLCollection {
}
}
// https://dom.spec.whatwg.org/#concept-getelementsbytagname
/// <https://dom.spec.whatwg.org/#concept-getelementsbytagname>
pub fn by_qualified_name(
window: &Window,
root: &Node,
@ -317,7 +318,7 @@ impl HTMLCollection {
}
impl HTMLCollectionMethods for HTMLCollection {
// https://dom.spec.whatwg.org/#dom-htmlcollection-length
/// <https://dom.spec.whatwg.org/#dom-htmlcollection-length>
fn Length(&self) -> u32 {
self.validate_cache();
@ -332,30 +333,34 @@ impl HTMLCollectionMethods for HTMLCollection {
}
}
// https://dom.spec.whatwg.org/#dom-htmlcollection-item
/// <https://dom.spec.whatwg.org/#dom-htmlcollection-item>
fn Item(&self, index: u32) -> Option<DomRoot<Element>> {
self.validate_cache();
if let Some(element) = self.cached_cursor_element.get() {
// Cache hit, the cursor element is set
if let Some(cached_index) = self.cached_cursor_index.get().to_option() {
if cached_index == index {
// The cursor is the element we're looking for
Some(element)
} else if cached_index < index {
// The cursor is before the element we're looking for
// Iterate forwards, starting at the cursor.
let offset = index - (cached_index + 1);
let node: DomRoot<Node> = DomRoot::upcast(element);
let mut iter = self.elements_iter_after(&node);
self.set_cached_cursor(index, iter.nth(offset as usize))
} else {
// The cursor is after the element we're looking for
// Iterate backwards, starting at the cursor.
let offset = cached_index - (index + 1);
let node: DomRoot<Node> = DomRoot::upcast(element);
let mut iter = self.elements_iter_before(&node);
self.set_cached_cursor(index, iter.nth(offset as usize))
match cached_index.cmp(&index) {
Ordering::Equal => {
// The cursor is the element we're looking for
Some(element)
},
Ordering::Less => {
// The cursor is before the element we're looking for
// Iterate forwards, starting at the cursor.
let offset = index - (cached_index + 1);
let node: DomRoot<Node> = DomRoot::upcast(element);
let mut iter = self.elements_iter_after(&node);
self.set_cached_cursor(index, iter.nth(offset as usize))
},
Ordering::Greater => {
// The cursor is after the element we're looking for
// Iterate backwards, starting at the cursor.
let offset = cached_index - (index + 1);
let node: DomRoot<Node> = DomRoot::upcast(element);
let mut iter = self.elements_iter_before(&node);
self.set_cached_cursor(index, iter.nth(offset as usize))
},
}
} else {
// Cache miss
@ -369,7 +374,7 @@ impl HTMLCollectionMethods for HTMLCollection {
}
}
// https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem
/// <https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem>
fn NamedItem(&self, key: DOMString) -> Option<DomRoot<Element>> {
// Step 1.
if key.is_empty() {

View file

@ -85,7 +85,7 @@ impl HTMLMetaElement {
if name == "referrer" {
self.apply_referrer();
}
} else if &*self.HttpEquiv() != "" {
} else if !self.HttpEquiv().is_empty() {
self.declarative_refresh();
}
}

View file

@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cmp::Ordering;
use dom_struct::dom_struct;
use html5ever::local_name;
@ -120,27 +122,31 @@ impl HTMLOptionsCollectionMethods for HTMLOptionsCollection {
}
}
// https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-length
/// <https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-length>
fn Length(&self) -> u32 {
self.upcast().Length()
}
// https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-length
/// <https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-length>
fn SetLength(&self, length: u32) {
let current_length = self.upcast().Length();
let delta = length as i32 - current_length as i32;
if delta < 0 {
// new length is lower - deleting last option elements
for index in (length..current_length).rev() {
self.Remove(index as i32)
}
} else if delta > 0 {
// new length is higher - adding new option elements
self.add_new_elements(delta as u32).unwrap();
match delta.cmp(&0) {
Ordering::Less => {
// new length is lower - deleting last option elements
for index in (length..current_length).rev() {
self.Remove(index as i32)
}
},
Ordering::Greater => {
// new length is higher - adding new option elements
self.add_new_elements(delta as u32).unwrap();
},
_ => {},
}
}
// https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-add
/// <https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-add>
fn Add(
&self,
element: HTMLOptionElementOrHTMLOptGroupElement,
@ -195,14 +201,14 @@ impl HTMLOptionsCollectionMethods for HTMLOptionsCollection {
Node::pre_insert(node, &parent, reference_node.as_deref()).map(|_| ())
}
// https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-remove
/// <https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-remove>
fn Remove(&self, index: i32) {
if let Some(element) = self.upcast().IndexedGetter(index as u32) {
element.Remove();
}
}
// https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-selectedindex
/// <https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-selectedindex>
fn SelectedIndex(&self) -> i32 {
self.upcast()
.root_node()
@ -211,7 +217,7 @@ impl HTMLOptionsCollectionMethods for HTMLOptionsCollection {
.SelectedIndex()
}
// https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-selectedindex
/// <https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-selectedindex>
fn SetSelectedIndex(&self, index: i32) {
self.upcast()
.root_node()