Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Mar 31, 2024
1 parent 4de4170 commit a7b8acd
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 60 deletions.
18 changes: 9 additions & 9 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,15 @@ pub fn is_whitespace_str(s: &str) -> bool {
/// [1]: http://www.w3.org/TR/2006/REC-xml11-20060816/#sec-common-syn
#[must_use]
pub fn is_name_start_char(c: char) -> bool {
match c {
matches!(c,
':' | 'A'..='Z' | '_' | 'a'..='z' |
'\u{C0}'..='\u{D6}' | '\u{D8}'..='\u{F6}' | '\u{F8}'..='\u{2FF}' |
'\u{370}'..='\u{37D}' | '\u{37F}'..='\u{1FFF}' |
'\u{200C}'..='\u{200D}' | '\u{2070}'..='\u{218F}' |
'\u{2C00}'..='\u{2FEF}' | '\u{3001}'..='\u{D7FF}' |
'\u{F900}'..='\u{FDCF}' | '\u{FDF0}'..='\u{FFFD}' |
'\u{10000}'..='\u{EFFFF}' => true,
_ => false
}
'\u{10000}'..='\u{EFFFF}'
)
}

/// Checks whether the given character is a name character (`NameChar`)
Expand All @@ -151,10 +150,11 @@ pub fn is_name_start_char(c: char) -> bool {
/// [1]: http://www.w3.org/TR/2006/REC-xml11-20060816/#sec-common-syn
#[must_use]
pub fn is_name_char(c: char) -> bool {
match c {
_ if is_name_start_char(c) => true,
'-' | '.' | '0'..='9' | '\u{B7}' |
'\u{300}'..='\u{36F}' | '\u{203F}'..='\u{2040}' => true,
_ => false
if is_name_start_char(c) {
return true;
}
matches!(c,
'-' | '.' | '0'..='9' | '\u{B7}' |
'\u{300}'..='\u{36F}' | '\u{203F}'..='\u{2040}'
)
}
17 changes: 11 additions & 6 deletions src/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ impl Namespace {
// a shortcut for a namespace which is definitely not empty
if self.0.len() > 3 { return false; }

self.0.iter().all(|(k, v)| match (&**k, &**v) {
(NS_NO_PREFIX, NS_EMPTY_URI) => true,
(NS_XMLNS_PREFIX, NS_XMLNS_URI) => true,
(NS_XML_PREFIX, NS_XML_URI) => true,
_ => false
})
self.0.iter().all(|(k, v)| matches!((&**k, &**v),
(NS_NO_PREFIX, NS_EMPTY_URI) |
(NS_XMLNS_PREFIX, NS_XMLNS_URI) |
(NS_XML_PREFIX, NS_XML_URI))
)
}

/// Checks whether this namespace mapping contains the given prefix.
Expand Down Expand Up @@ -172,6 +171,11 @@ impl Namespace {
pub fn borrow(&self) -> Cow<'_, Self> {
Cow::Borrowed(self)
}

/// Namespace mappings contained in a namespace.
pub fn iter(&self) -> NamespaceMappings<'_> {
self.into_iter()
}
}

/// An alias for iterator type for namespace mappings contained in a namespace.
Expand Down Expand Up @@ -215,6 +219,7 @@ impl NamespaceStack {
/// * `xmlns` → `http://www.w3.org/2000/xmlns/`.
#[inline]
#[must_use]
#[allow(clippy::should_implement_trait)]
pub fn default() -> NamespaceStack {
let mut nst = NamespaceStack::empty();
nst.push_empty();
Expand Down
2 changes: 1 addition & 1 deletion src/reader/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub struct ParserConfig2 {
impl Default for ParserConfig2 {
fn default() -> Self {
ParserConfig2 {
c: Default::default(),
c: ParserConfig::default(),
override_encoding: None,
ignore_invalid_encoding_declarations: false,
allow_multiple_root_elements: true,
Expand Down
76 changes: 40 additions & 36 deletions src/reader/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) enum Token {
ProcessingInstructionStart,
/// `?>`
ProcessingInstructionEnd,
/// `<!DOCTYPE
/// `<!DOCTYPE…`
DoctypeStart,
/// `<`
OpeningTagStart,
Expand Down Expand Up @@ -80,7 +80,10 @@ impl fmt::Display for Token {
Token::SingleQuote => "'",
Token::DoubleQuote => "\"",
Token::MarkupDeclarationStart => "<!",
_ => unreachable!()
Token::Character(_) => {
debug_assert!(false);
""
},
}.fmt(f),
}
}
Expand Down Expand Up @@ -349,14 +352,15 @@ impl Lexer {
Ok(Some(Token::Character(']'))),
State::InvalidCDataClosing(ClosingSubstate::Second) => {
self.eof_handled = false;
Ok(self.move_to_with_unread(State::Normal, &[']'], Token::Character(']')))
Ok(Some(self.move_to_with_unread(State::Normal, &[']'], Token::Character(']'))))
},
State::Normal =>
Ok(None),
}
}

#[cold]
#[allow(clippy::needless_pass_by_value)]
fn error(&self, e: SyntaxError) -> Error {
Error {
pos: self.position(),
Expand All @@ -370,21 +374,21 @@ impl Lexer {
match self.st {
State::Normal => Ok(self.normal(c)),
State::TagStarted => self.tag_opened(c),
State::EmptyTagClosing => Ok(self.empty_element_closing(c)),
State::EmptyTagClosing => Ok(Some(self.empty_element_closing(c))),
State::CommentOrCDataOrDoctypeStarted => self.comment_or_cdata_or_doctype_started(c),
State::InsideCdata => Ok(self.inside_cdata(c)),
State::CDataStarted(s) => self.cdata_started(c, s),
State::InsideComment => Ok(self.inside_comment_state(c)),
State::CommentStarted => self.comment_started(c),
State::InsideProcessingInstruction => Ok(self.inside_processing_instruction(c)),
State::ProcessingInstructionClosing => Ok(self.processing_instruction_closing(c)),
State::ProcessingInstructionClosing => Ok(Some(self.processing_instruction_closing(c))),
State::CommentClosing(s) => self.comment_closing(c, s),
State::CDataClosing(s) => Ok(self.cdata_closing(c, s)),
State::InsideDoctype => Ok(self.inside_doctype(c)),
State::DoctypeStarted(s) => self.doctype_started(c, s),
State::InvalidCDataClosing(s) => Ok(self.invalid_cdata_closing(c, s)),
State::InsideMarkupDeclaration => self.markup_declaration(c),
State::InsideMarkupDeclarationQuotedString(q) => Ok(self.markup_declaration_string(c, q)),
State::InsideMarkupDeclarationQuotedString(q) => Ok(Some(self.markup_declaration_string(c, q))),
}
}

Expand All @@ -395,19 +399,19 @@ impl Lexer {
}

#[inline]
fn move_to_with(&mut self, st: State, token: Token) -> Option<Token> {
fn move_to_with(&mut self, st: State, token: Token) -> Token {
self.st = st;
Some(token)
token
}

#[inline]
fn move_to_and_reset_normal(&mut self, st: State, token: Token) -> Option<Token> {
fn move_to_and_reset_normal(&mut self, st: State, token: Token) -> Token {
self.normal_state = st;
self.st = st;
Some(token)
token
}

fn move_to_with_unread(&mut self, st: State, cs: &[char], token: Token) -> Option<Token> {
fn move_to_with_unread(&mut self, st: State, cs: &[char], token: Token) -> Token {
for c in cs.iter().rev().copied() {
self.char_queue.push_front(c);
}
Expand Down Expand Up @@ -442,7 +446,7 @@ impl Lexer {
let first = chars.next().unwrap_or('\0');
self.char_queue.extend(chars);
self.char_queue.push_back(c);
return Ok(self.move_to_with(State::Normal, Token::Character(first)));
return Ok(Some(self.move_to_with(State::Normal, Token::Character(first))));
}
Err(self.error(SyntaxError::UnexpectedTokenBefore(chunk, c)))
}
Expand Down Expand Up @@ -496,11 +500,11 @@ impl Lexer {
/// Encountered '<'
fn tag_opened(&mut self, c: char) -> Result {
match c {
'?' => Ok(self.move_to_with(State::InsideProcessingInstruction, Token::ProcessingInstructionStart)),
'/' => Ok(self.move_to_with(self.normal_state, Token::ClosingTagStart)),
'?' => Ok(Some(self.move_to_with(State::InsideProcessingInstruction, Token::ProcessingInstructionStart))),
'/' => Ok(Some(self.move_to_with(self.normal_state, Token::ClosingTagStart))),
'!' => Ok(self.move_to(State::CommentOrCDataOrDoctypeStarted)),
_ if is_whitespace_char(c) => Ok(self.move_to_with_unread(self.normal_state, &[c], Token::OpeningTagStart)),
_ if is_name_char(c) => Ok(self.move_to_with_unread(self.normal_state, &[c], Token::OpeningTagStart)),
_ if is_whitespace_char(c) => Ok(Some(self.move_to_with_unread(self.normal_state, &[c], Token::OpeningTagStart))),
_ if is_name_char(c) => Ok(Some(self.move_to_with_unread(self.normal_state, &[c], Token::OpeningTagStart))),
_ => self.handle_error("<", c)
}
}
Expand All @@ -512,7 +516,7 @@ impl Lexer {
'[' => Ok(self.move_to(State::CDataStarted(CDataStartedSubstate::E))),
'D' => Ok(self.move_to(State::DoctypeStarted(DoctypeStartedSubstate::D))),
'E' | 'A' | 'N' if matches!(self.normal_state, State::InsideDoctype) => {
Ok(self.move_to_with_unread(State::InsideMarkupDeclaration, &[c], Token::MarkupDeclarationStart))
Ok(Some(self.move_to_with_unread(State::InsideMarkupDeclaration, &[c], Token::MarkupDeclarationStart)))
},
_ => self.handle_error("<!", c),
}
Expand All @@ -521,7 +525,7 @@ impl Lexer {
/// Encountered '<!-'
fn comment_started(&mut self, c: char) -> Result {
match c {
'-' => Ok(self.move_to_with(State::InsideComment, Token::CommentStart)),
'-' => Ok(Some(self.move_to_with(State::InsideComment, Token::CommentStart))),
_ => self.handle_error("<!-", c),
}
}
Expand All @@ -535,28 +539,28 @@ impl Lexer {
CD ; 'A' ; CDA ; "<![CD",
CDA ; 'T' ; CDAT ; "<![CDA",
CDAT ; 'A' ; CDATA ; "<![CDAT";
CDATA ; '[' ; "<![CDATA" ; Ok(self.move_to_with(State::InsideCdata, Token::CDataStart))
CDATA ; '[' ; "<![CDATA" ; Ok(Some(self.move_to_with(State::InsideCdata, Token::CDataStart)))
)
}

/// Encountered '<!…' that isn't DOCTYPE or CDATA
fn markup_declaration(&mut self, c: char) -> Result {
match c {
'<' => self.handle_error("<!", c),
'>' => Ok(self.move_to_with(self.normal_state, Token::TagEnd)),
'>' => Ok(Some(self.move_to_with(self.normal_state, Token::TagEnd))),
'&' => Ok(Some(Token::ReferenceStart)),
';' => Ok(Some(Token::ReferenceEnd)),
'"' => Ok(self.move_to_with(State::InsideMarkupDeclarationQuotedString(QuoteStyle::Double), Token::DoubleQuote)),
'\'' => Ok(self.move_to_with(State::InsideMarkupDeclarationQuotedString(QuoteStyle::Single), Token::SingleQuote)),
'"' => Ok(Some(self.move_to_with(State::InsideMarkupDeclarationQuotedString(QuoteStyle::Double), Token::DoubleQuote))),
'\'' => Ok(Some(self.move_to_with(State::InsideMarkupDeclarationQuotedString(QuoteStyle::Single), Token::SingleQuote))),
_ => Ok(Some(Token::Character(c))),
}
}

fn markup_declaration_string(&mut self, c: char, q: QuoteStyle) -> Option<Token> {
fn markup_declaration_string(&mut self, c: char, q: QuoteStyle) -> Token {
match c {
'"' if q == QuoteStyle::Double => self.move_to_with(State::InsideMarkupDeclaration, Token::DoubleQuote),
'\'' if q == QuoteStyle::Single => self.move_to_with(State::InsideMarkupDeclaration, Token::SingleQuote),
_ => Some(Token::Character(c)),
_ => Token::Character(c),
}
}

Expand All @@ -569,14 +573,14 @@ impl Lexer {
DOC ; 'T' ; DOCT ; "<!DOC",
DOCT ; 'Y' ; DOCTY ; "<!DOCT",
DOCTY ; 'P' ; DOCTYP ; "<!DOCTY";
DOCTYP ; 'E' ; "<!DOCTYP" ; Ok(self.move_to_and_reset_normal(State::InsideDoctype, Token::DoctypeStart))
DOCTYP ; 'E' ; "<!DOCTYP" ; Ok(Some(self.move_to_and_reset_normal(State::InsideDoctype, Token::DoctypeStart)))
)
}

/// State used while awaiting the closing bracket for the <!DOCTYPE tag
fn inside_doctype(&mut self, c: char) -> Option<Token> {
match c {
'>' => self.move_to_and_reset_normal(State::Normal, Token::TagEnd),
'>' => Some(self.move_to_and_reset_normal(State::Normal, Token::TagEnd)),
'<' => self.move_to(State::TagStarted),
'&' => Some(Token::ReferenceStart),
';' => Some(Token::ReferenceEnd),
Expand All @@ -587,15 +591,15 @@ impl Lexer {
}

/// Encountered '?'
fn processing_instruction_closing(&mut self, c: char) -> Option<Token> {
fn processing_instruction_closing(&mut self, c: char) -> Token {
match c {
'>' => self.move_to_with(self.normal_state, Token::ProcessingInstructionEnd),
_ => self.move_to_with_unread(State::InsideProcessingInstruction, &[c], Token::Character('?')),
}
}

/// Encountered '/'
fn empty_element_closing(&mut self, c: char) -> Option<Token> {
fn empty_element_closing(&mut self, c: char) -> Token {
match c {
'>' => self.move_to_with(self.normal_state, Token::EmptyTagEnd),
_ => self.move_to_with_unread(self.normal_state, &[c], Token::Character('/')),
Expand All @@ -607,10 +611,10 @@ impl Lexer {
match s {
ClosingSubstate::First => match c {
'-' => Ok(self.move_to(State::CommentClosing(ClosingSubstate::Second))),
_ => Ok(self.move_to_with_unread(State::InsideComment, &[c], Token::Character('-'))),
_ => Ok(Some(self.move_to_with_unread(State::InsideComment, &[c], Token::Character('-')))),
},
ClosingSubstate::Second => match c {
'>' => Ok(self.move_to_with(self.normal_state, Token::CommentEnd)),
'>' => Ok(Some(self.move_to_with(self.normal_state, Token::CommentEnd))),
// double dash not followed by a greater-than is a hard error inside comment
_ => self.handle_error("--", c),
},
Expand All @@ -622,11 +626,11 @@ impl Lexer {
match s {
ClosingSubstate::First => match c {
']' => self.move_to(State::CDataClosing(ClosingSubstate::Second)),
_ => self.move_to_with_unread(State::InsideCdata, &[c], Token::Character(']')),
_ => Some(self.move_to_with_unread(State::InsideCdata, &[c], Token::Character(']'))),
},
ClosingSubstate::Second => match c {
'>' => self.move_to_with(State::Normal, Token::CDataEnd),
_ => self.move_to_with_unread(State::InsideCdata, &[']', c], Token::Character(']')),
'>' => Some(self.move_to_with(State::Normal, Token::CDataEnd)),
_ => Some(self.move_to_with_unread(State::InsideCdata, &[']', c], Token::Character(']'))),
},
}
}
Expand All @@ -636,11 +640,11 @@ impl Lexer {
match s {
ClosingSubstate::First => match c {
']' => self.move_to(State::InvalidCDataClosing(ClosingSubstate::Second)),
_ => self.move_to_with_unread(State::Normal, &[c], Token::Character(']')),
_ => Some(self.move_to_with_unread(State::Normal, &[c], Token::Character(']'))),
},
ClosingSubstate::Second => match c {
'>' => self.move_to_with(self.normal_state, Token::CDataEnd),
_ => self.move_to_with_unread(State::Normal, &[']', c], Token::Character(']')),
'>' => Some(self.move_to_with(self.normal_state, Token::CDataEnd)),
_ => Some(self.move_to_with_unread(State::Normal, &[']', c], Token::Character(']'))),
},
}
}
Expand Down
1 change: 1 addition & 0 deletions src/reader/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ macro_rules! gen_takes(
impl MarkupData {
#[inline]
#[allow(clippy::mem_replace_option_with_none)]
#[allow(clippy::mem_replace_with_default)]
fn $method(&mut self) -> $t {
std::mem::replace(&mut self.$field, $def)
}
Expand Down
2 changes: 1 addition & 1 deletion src/reader/parser/inside_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl PullParser {
let val = if let Some(hex) = num_str.strip_prefix('x') {
u32::from_str_radix(hex, 16).map_err(move |_| SyntaxError::InvalidNumericEntity(num_str.into()))?
} else {
u32::from_str_radix(num_str, 10).map_err(move |_| SyntaxError::InvalidNumericEntity(num_str.into()))?
num_str.parse::<u32>().map_err(move |_| SyntaxError::InvalidNumericEntity(num_str.into()))?
};
match char::from_u32(val) {
Some(c) if self.is_valid_xml_char(c) => Ok(c),
Expand Down
14 changes: 7 additions & 7 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ impl fmt::Display for Encoding {
#[cold]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Encoding::Utf8 => "UTF-8",
Encoding::Utf8 |
Encoding::Default => "UTF-8",
Encoding::Latin1 => "ISO-8859-1",
Encoding::Ascii => "US-ASCII",
Encoding::Utf16Be => "UTF-16",
Encoding::Utf16Le => "UTF-16",
Encoding::Utf16Be |
Encoding::Utf16Le |
Encoding::Utf16 => "UTF-16",
Encoding::Unknown => "(unknown)",
})
Expand Down Expand Up @@ -142,11 +142,11 @@ impl CharReader {
return Ok(Some(next.into()));
},
Encoding::Ascii => {
if next.is_ascii() {
return Ok(Some(next.into()));
return if next.is_ascii() {
Ok(Some(next.into()))
} else {
return Err(CharReadError::Io(io::Error::new(io::ErrorKind::InvalidData, "char is not ASCII")));
}
Err(CharReadError::Io(io::Error::new(io::ErrorKind::InvalidData, "char is not ASCII")))
};
},
Encoding::Unknown | Encoding::Utf16 => {
buf[pos] = next;
Expand Down

0 comments on commit a7b8acd

Please sign in to comment.