Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Oct 28, 2024
1 parent 4fec2d0 commit 2af98ed
Show file tree
Hide file tree
Showing 46 changed files with 339 additions and 255 deletions.
4 changes: 2 additions & 2 deletions benches/cases/rewriting.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use lol_html::html_content::*;
use lol_html::*;
use lol_html::html_content::ContentType;
use lol_html::{element, Settings};

define_group!(
"Rewriting",
Expand Down
2 changes: 1 addition & 1 deletion benches/cases/selector_matching.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use lol_html::*;
use lol_html::{element, Settings};

define_group!(
"Selector matching",
Expand Down
1 change: 1 addition & 0 deletions js-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct Anchor<'r> {
}

impl<'r> Anchor<'r> {
#[inline]
pub fn new(poisoned: Rc<Cell<bool>>) -> Self {
Anchor {
poisoned,
Expand Down
1 change: 1 addition & 0 deletions src/base/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl Debug for Bytes<'_> {
impl Deref for Bytes<'_> {
type Target = [u8];

#[inline]
fn deref(&self) -> &[u8] {
&self.0
}
Expand Down
6 changes: 4 additions & 2 deletions src/base/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,14 @@ pub struct SharedEncoding {
}

impl SharedEncoding {
pub fn new(encoding: AsciiCompatibleEncoding) -> SharedEncoding {
SharedEncoding {
#[must_use]
pub fn new(encoding: AsciiCompatibleEncoding) -> Self {
Self {
encoding: Arc::new(AtomicUsize::new(encoding_to_index(encoding))),
}
}

#[must_use]
pub fn get(&self) -> &'static Encoding {
let encoding = self.encoding.load(Ordering::Relaxed);
ALL_ENCODINGS[encoding]
Expand Down
14 changes: 9 additions & 5 deletions src/html/local_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ pub struct LocalNameHash(Option<u64>);

impl LocalNameHash {
#[inline]
pub fn new() -> Self {
LocalNameHash(Some(0))
#[must_use]
pub const fn new() -> Self {
Self(Some(0))
}

#[inline]
pub fn is_empty(&self) -> bool {
#[must_use]
pub const fn is_empty(&self) -> bool {
self.0.is_none()
}

Expand Down Expand Up @@ -75,7 +77,7 @@ impl LocalNameHash {
impl From<&str> for LocalNameHash {
#[inline]
fn from(string: &str) -> Self {
let mut hash = LocalNameHash::new();
let mut hash = Self::new();

for ch in string.bytes() {
hash.update(ch);
Expand Down Expand Up @@ -106,6 +108,7 @@ pub enum LocalName<'i> {

impl<'i> LocalName<'i> {
#[inline]
#[must_use]
pub fn new(input: &'i Bytes<'i>, range: Range, hash: LocalNameHash) -> Self {
if hash.is_empty() {
LocalName::Bytes(input.slice(range))
Expand All @@ -115,6 +118,7 @@ impl<'i> LocalName<'i> {
}

#[inline]
#[must_use]
pub fn into_owned(self) -> LocalName<'static> {
match self {
LocalName::Bytes(b) => LocalName::Bytes(b.into_owned()),
Expand Down Expand Up @@ -150,7 +154,7 @@ impl PartialEq<Tag> for LocalName<'_> {
impl PartialEq<LocalName<'_>> for LocalName<'_> {
#[inline]
fn eq(&self, other: &LocalName<'_>) -> bool {
use LocalName::*;
use LocalName::{Bytes, Hash};

match (self, other) {
(Hash(s), Hash(o)) => s == o,
Expand Down
4 changes: 2 additions & 2 deletions src/html/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub enum Namespace {

impl Namespace {
#[inline]
pub fn uri(self) -> &'static str {
use Namespace::*;
pub const fn uri(self) -> &'static str {
use Namespace::{Html, MathML, Svg};

// NOTE: https://infra.spec.whatwg.org/#namespaces
match self {
Expand Down
3 changes: 2 additions & 1 deletion src/html/text_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ impl TextType {
///
/// [HTML entities]: https://developer.mozilla.org/en-US/docs/Glossary/Entity
#[inline]
#[must_use]
pub fn allows_html_entities(self) -> bool {
self == TextType::Data || self == TextType::RCData
self == Self::Data || self == Self::RCData
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
//! [Cloudflare Workers]: https://www.cloudflare.com/en-gb/products/cloudflare-workers/
//! [`HtmlRewriter`]: struct.HtmlRewriter.html
//! [`rewrite_str`]: fn.rewrite_str.html
#![allow(clippy::default_trait_access)]
#![allow(clippy::module_name_repetitions)]
#![cfg_attr(not(any(feature = "integration_test", test)), warn(missing_docs))]

#[macro_use]
Expand Down Expand Up @@ -151,8 +152,9 @@ pub mod test_utils {
}

impl Output {
#[must_use]
pub fn new(encoding: &'static Encoding) -> Self {
Output {
Self {
bytes: Vec::default(),
encoding,
finalizing_chunk_received: false,
Expand All @@ -174,7 +176,7 @@ pub mod test_utils {
}

impl From<Output> for String {
fn from(output: Output) -> String {
fn from(output: Output) -> Self {
assert!(
output.finalizing_chunk_received,
"Finalizing chunk for the output hasn't been received."
Expand Down
2 changes: 1 addition & 1 deletion src/memory/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Arena {
pub fn new(limiter: SharedMemoryLimiter, preallocated_size: usize) -> Self {
limiter.preallocate(preallocated_size);

Arena {
Self {
limiter,
data: Vec::with_capacity(preallocated_size),
}
Expand Down
4 changes: 2 additions & 2 deletions src/memory/limited_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct LimitedVec<T> {

impl<T> LimitedVec<T> {
pub fn new(limiter: SharedMemoryLimiter) -> Self {
LimitedVec {
Self {
vec: vec![],
limiter,
}
Expand Down Expand Up @@ -122,7 +122,7 @@ mod tests {
#[test]
fn max_limit() {
let limiter = SharedMemoryLimiter::new(2);
let mut vector: LimitedVec<u8> = LimitedVec::new(limiter.clone());
let mut vector: LimitedVec<u8> = LimitedVec::new(limiter);

vector.push(1).unwrap();
vector.push(2).unwrap();
Expand Down
5 changes: 3 additions & 2 deletions src/memory/limiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ pub struct SharedMemoryLimiter {
}

impl SharedMemoryLimiter {
pub fn new(max: usize) -> SharedMemoryLimiter {
SharedMemoryLimiter {
#[must_use]
pub fn new(max: usize) -> Self {
Self {
current_usage: Arc::new(AtomicUsize::new(0)),
max,
}
Expand Down
20 changes: 11 additions & 9 deletions src/parser/lexer/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;
use crate::parser::state_machine::StateMachineActions;

use NonTagContentTokenOutline::*;
use TagTokenOutline::*;
use TagTokenOutline::{EndTag, StartTag};

// NOTE: use macro instead of the function to make borrow
// checker happy with range construction inside match arm
Expand Down Expand Up @@ -233,8 +233,8 @@ impl<S: LexemeSink> StateMachineActions for Lexer<S> {
#[inline]
fn finish_tag_name(&mut self, _context: &mut ParserContext<S>, _input: &[u8]) -> ActionResult {
match self.current_tag_token {
Some(StartTag { ref mut name, .. }) | Some(EndTag { ref mut name, .. }) => {
*name = get_token_part_range!(self)
Some(StartTag { ref mut name, .. } | EndTag { ref mut name, .. }) => {
*name = get_token_part_range!(self);
}
_ => unreachable!("Tag should exist at this point"),
}
Expand All @@ -246,12 +246,14 @@ impl<S: LexemeSink> StateMachineActions for Lexer<S> {
fn update_tag_name_hash(&mut self, _context: &mut ParserContext<S>, input: &[u8]) {
if let Some(ch) = input.get(self.pos()).copied() {
match self.current_tag_token {
Some(StartTag {
ref mut name_hash, ..
})
| Some(EndTag {
ref mut name_hash, ..
}) => name_hash.update(ch),
Some(
StartTag {
ref mut name_hash, ..
}
| EndTag {
ref mut name_hash, ..
},
) => name_hash.update(ch),
_ => unreachable!("Tag should exist at this point"),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/parser/lexer/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::parser::state_machine::StateMachineConditions;

impl<S: LexemeSink> StateMachineConditions for Lexer<S> {
#[inline]
#[must_use]
fn is_appropriate_end_tag(&self) -> bool {
match self.current_tag_token {
Some(TagTokenOutline::EndTag { name_hash, .. }) => {
Expand All @@ -13,6 +14,7 @@ impl<S: LexemeSink> StateMachineConditions for Lexer<S> {
}

#[inline]
#[must_use]
fn cdata_allowed(&self) -> bool {
self.cdata_allowed
}
Expand Down
8 changes: 4 additions & 4 deletions src/parser/lexer/lexeme/token_outline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ impl Align for TagTokenOutline {
#[inline]
fn align(&mut self, offset: usize) {
match self {
TagTokenOutline::StartTag {
Self::StartTag {
name, attributes, ..
} => {
name.align(offset);
attributes.as_mut_slice().align(offset);
}
TagTokenOutline::EndTag { name, .. } => name.align(offset),
Self::EndTag { name, .. } => name.align(offset),
}
}
}
Expand All @@ -68,8 +68,8 @@ impl Align for NonTagContentTokenOutline {
#[inline]
fn align(&mut self, offset: usize) {
match self {
NonTagContentTokenOutline::Comment(text) => text.align(offset),
NonTagContentTokenOutline::Doctype {
Self::Comment(text) => text.align(offset),
Self::Doctype {
name,
public_id,
system_id,
Expand Down
23 changes: 10 additions & 13 deletions src/parser/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ pub struct Lexer<S: LexemeSink> {
}

impl<S: LexemeSink> Lexer<S> {
#[inline]
#[must_use]
pub fn new() -> Self {
Lexer {
Self {
next_pos: 0,
is_last_input: false,
lexeme_start: 0,
token_part_start: 0,
is_state_enter: true,
cdata_allowed: false,
state: Lexer::data_state,
state: Self::data_state,
current_tag_token: None,
current_non_tag_content_token: None,
current_attr: None,
Expand Down Expand Up @@ -133,8 +135,9 @@ impl<S: LexemeSink> Lexer<S> {
}

#[inline]
#[must_use]
fn create_lexeme_with_raw<'i, T>(
&mut self,
&self,
input: &'i [u8],
token: T,
raw_end: usize,
Expand All @@ -150,22 +153,16 @@ impl<S: LexemeSink> Lexer<S> {
}

#[inline]
fn create_lexeme_with_raw_inclusive<'i, T>(
&mut self,
input: &'i [u8],
token: T,
) -> Lexeme<'i, T> {
#[must_use]
fn create_lexeme_with_raw_inclusive<'i, T>(&self, input: &'i [u8], token: T) -> Lexeme<'i, T> {
let raw_end = self.pos() + 1;

self.create_lexeme_with_raw(input, token, raw_end)
}

#[inline]
fn create_lexeme_with_raw_exclusive<'i, T>(
&mut self,
input: &'i [u8],
token: T,
) -> Lexeme<'i, T> {
#[must_use]
fn create_lexeme_with_raw_exclusive<'i, T>(&self, input: &'i [u8], token: T) -> Lexeme<'i, T> {
let raw_end = self.pos();

self.create_lexeme_with_raw(input, token, raw_end)
Expand Down
4 changes: 3 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ pub struct Parser<S: ParserOutputSink> {
}

impl<S: ParserOutputSink> Parser<S> {
#[inline]
#[must_use]
pub fn new(output_sink: S, initial_directive: ParserDirective, strict: bool) -> Self {
let context = ParserContext {
output_sink,
tree_builder_simulator: TreeBuilderSimulator::new(strict),
};

Parser {
Self {
lexer: Lexer::new(),
tag_scanner: TagScanner::new(),
current_directive: initial_directive,
Expand Down
14 changes: 7 additions & 7 deletions src/parser/state_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub enum FeedbackDirective {

impl FeedbackDirective {
#[inline]
pub fn take(&mut self) -> FeedbackDirective {
mem::replace(self, FeedbackDirective::None)
pub fn take(&mut self) -> Self {
mem::replace(self, Self::None)
}
}

Expand All @@ -30,9 +30,9 @@ impl Debug for FeedbackDirective {
f,
"{}",
match self {
FeedbackDirective::ApplyUnhandledFeedback(_) => "ApplyPendingFeedback",
FeedbackDirective::Skip => "Skip",
FeedbackDirective::None => "None",
Self::ApplyUnhandledFeedback(_) => "ApplyPendingFeedback",
Self::Skip => "Skip",
Self::None => "None",
}
)
}
Expand All @@ -56,7 +56,7 @@ pub enum ActionError {
impl From<ParsingAmbiguityError> for ActionError {
#[cold]
fn from(err: ParsingAmbiguityError) -> Self {
ActionError::RewritingError(RewritingError::ParsingAmbiguity(err))
Self::RewritingError(RewritingError::ParsingAmbiguity(err))
}
}

Expand Down Expand Up @@ -218,7 +218,7 @@ pub trait StateMachine: StateMachineActions + StateMachineConditions {
let consumed_byte_count = self.get_consumed_byte_count(input);

if !self.is_last_input() {
self.adjust_for_next_input()
self.adjust_for_next_input();
}

self.set_pos(self.pos() - consumed_byte_count);
Expand Down
Loading

0 comments on commit 2af98ed

Please sign in to comment.