Skip to content

Commit

Permalink
clippy: Fix comparison_* warnings (#32058)
Browse files Browse the repository at this point in the history
  • Loading branch information
eerii committed Apr 12, 2024
1 parent 509b858 commit 88d4aff
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 78 deletions.
46 changes: 25 additions & 21 deletions components/script/dom/domtokenlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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| {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)?;
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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)
}
Expand Down
35 changes: 17 additions & 18 deletions components/script/dom/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -285,15 +286,15 @@ 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);
}
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);
Expand All @@ -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,
Expand All @@ -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,
Expand Down
53 changes: 29 additions & 24 deletions components/script/dom/htmlcollection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();

Expand All @@ -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
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/htmlmetaelement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
Loading

0 comments on commit 88d4aff

Please sign in to comment.