Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Dec 12, 2023
1 parent e945d22 commit 4cd3b5a
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 62 deletions.
16 changes: 8 additions & 8 deletions examples/print_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,38 @@ fn main() {

match e {
XmlEvent::StartDocument { version, encoding, .. } => {
println!("StartDocument({version}, {encoding})")
println!("StartDocument({version}, {encoding})");
},
XmlEvent::EndDocument => {
println!("EndDocument");
break;
}
XmlEvent::ProcessingInstruction { name, data } => {
println!("ProcessingInstruction({name}={:?})", data.as_deref().unwrap_or_default())
println!("ProcessingInstruction({name}={:?})", data.as_deref().unwrap_or_default());
},
XmlEvent::StartElement { name, attributes, .. } => {
if attributes.is_empty() {
println!("StartElement({name})")
println!("StartElement({name})");
} else {
let attrs: Vec<_> = attributes
.iter()
.map(|a| format!("{}={:?}", &a.name, a.value))
.collect();
println!("StartElement({name} [{}])", attrs.join(", "))
println!("StartElement({name} [{}])", attrs.join(", "));
}
}
XmlEvent::EndElement { name } => {
println!("EndElement({name})")
println!("EndElement({name})");
},
XmlEvent::Comment(data) => {
println!(r#"Comment("{}")"#, data.escape_debug())
println!(r#"Comment("{}")"#, data.escape_debug());
}
XmlEvent::CData(data) => println!(r#"CData("{}")"#, data.escape_debug()),
XmlEvent::Characters(data) => {
println!(r#"Characters("{}")"#, data.escape_debug())
println!(r#"Characters("{}")"#, data.escape_debug());
}
XmlEvent::Whitespace(data) => {
println!(r#"Whitespace("{}")"#, data.escape_debug())
println!(r#"Whitespace("{}")"#, data.escape_debug());
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ macro_rules! gen_setter {
///
/// <small>See [`ParserConfig`][crate::ParserConfig] fields docs for details</small>
#[inline]
#[must_use]
pub fn $field<T: Into<$t>>(mut self, value: T) -> Self {
self.$field = value.into();
self
Expand All @@ -19,7 +20,8 @@ macro_rules! gen_setter {
///
/// <small>See [`ParserConfig`][crate::ParserConfig] fields docs for details</small>
#[inline]
#[must_use] pub fn $field(mut self, value: $t) -> Self {
#[must_use]
pub fn $field(mut self, value: $t) -> Self {
self.$field = value;
self
}
Expand All @@ -29,7 +31,8 @@ macro_rules! gen_setter {
///
/// <small>See [`ParserConfig`][crate::ParserConfig] fields docs for details</small>
#[inline]
#[must_use] pub fn $field(mut self, value: $t) -> Self {
#[must_use]
pub fn $field(mut self, value: $t) -> Self {
self.c.$field = value;
self
}
Expand Down
8 changes: 3 additions & 5 deletions src/reader/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl ParserConfig {
/// .add_entity("reg", "®")
/// .create_reader(&mut source);
/// ```
#[must_use]
pub fn add_entity<S: Into<String>, T: Into<String>>(mut self, entity: S, value: T) -> ParserConfig {
self.extra_entities.insert(entity.into(), value.into());
self
Expand Down Expand Up @@ -257,11 +258,8 @@ impl ParserConfig2 {
.and_then(|(_, args)| args.split_once('='));
if let Some((_, charset)) = charset {
let name = charset.trim().trim_matches('"');
match name.parse() {
Ok(enc) => {
self.override_encoding = Some(enc);
},
Err(_) => {},
if let Ok(enc) = name.parse() {
self.override_encoding = Some(enc);
}
}
self
Expand Down
2 changes: 1 addition & 1 deletion src/reader/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub(crate) enum SyntaxError {
}

impl fmt::Display for SyntaxError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.to_cow().fmt(f)
}
}
Expand Down
48 changes: 21 additions & 27 deletions src/reader/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ impl fmt::Display for Token {
}

impl Token {
pub fn as_static_str(&self) -> Option<&'static str> {
match *self {
pub fn as_static_str(self) -> Option<&'static str> {
match self {
Token::OpeningTagStart => Some("<"),
Token::ProcessingInstructionStart => Some("<?"),
Token::DoctypeStart => Some("<!DOCTYPE"),
Expand All @@ -110,11 +110,11 @@ impl Token {
}

// using String.push_str(token.to_string()) is simply way too slow
pub fn push_to_string(&self, target: &mut String) {
match *self {
pub fn push_to_string(self, target: &mut String) {
match self {
Token::Character(c) => {
debug_assert!(is_xml10_char(c) || is_xml11_char(c));
target.push(c)
target.push(c);
},
_ => if let Some(s) = self.as_static_str() {
target.push_str(s);
Expand Down Expand Up @@ -172,11 +172,13 @@ enum ClosingSubstate {
}

#[derive(Copy, Clone)]
#[allow(clippy::upper_case_acronyms)]
enum DoctypeStartedSubstate {
D, DO, DOC, DOCT, DOCTY, DOCTYP
}

#[derive(Copy, Clone)]
#[allow(clippy::upper_case_acronyms)]
enum CDataStartedSubstate {
E, C, CD, CDA, CDAT, CDATA
}
Expand Down Expand Up @@ -297,12 +299,9 @@ impl Lexer {

// Check if we have saved a char or two for ourselves
while let Some(c) = self.char_queue.pop_front() {
match self.dispatch_char(c)? {
Some(t) => {
self.inside_token = false;
return Ok(Some(t));
}
None => {} // continue
if let Some(t) = self.dispatch_char(c)? {
self.inside_token = false;
return Ok(Some(t));
}
}
// if char_queue is empty, all circular reparsing is done
Expand All @@ -319,14 +318,9 @@ impl Lexer {
self.head_pos.advance(1);
}

match self.dispatch_char(c)? {
Some(t) => {
self.inside_token = false;
return Ok(Some(t));
}
None => {
// continue
}
if let Some(t) = self.dispatch_char(c)? {
self.inside_token = false;
return Ok(Some(t));
}
}

Expand Down Expand Up @@ -689,7 +683,7 @@ mod tests {

#[test]
fn tricky_pi() {
let (mut lex, mut buf) = make_lex_and_buf(r#"<?x<!-- &??><x>"#);
let (mut lex, mut buf) = make_lex_and_buf(r"<?x<!-- &??><x>");

assert_oks!(for lex and buf ;
Token::ProcessingInstructionStart
Expand All @@ -711,7 +705,7 @@ mod tests {

#[test]
fn reparser() {
let (mut lex, mut buf) = make_lex_and_buf(r#"&a;"#);
let (mut lex, mut buf) = make_lex_and_buf(r"&a;");

assert_oks!(for lex and buf ;
Token::ReferenceStart
Expand Down Expand Up @@ -794,7 +788,7 @@ mod tests {
#[test]
fn special_chars_test() {
let (mut lex, mut buf) = make_lex_and_buf(
r#"?x!+ // -| ]z]]"#
r"?x!+ // -| ]z]]"
);

assert_oks!(for lex and buf ;
Expand All @@ -820,7 +814,7 @@ mod tests {
#[test]
fn cdata_test() {
let (mut lex, mut buf) = make_lex_and_buf(
r#"<a><![CDATA[x y ?]]> </a>"#
r"<a><![CDATA[x y ?]]> </a>"
);

assert_oks!(for lex and buf ;
Expand All @@ -845,7 +839,7 @@ mod tests {
#[test]
fn cdata_closers_test() {
let (mut lex, mut buf) = make_lex_and_buf(
r#"<![CDATA[] > ]> ]]><!---->]]<a>"#
r"<![CDATA[] > ]> ]]><!---->]]<a>"
);

assert_oks!(for lex and buf ;
Expand All @@ -872,7 +866,7 @@ mod tests {
#[test]
fn doctype_test() {
let (mut lex, mut buf) = make_lex_and_buf(
r#"<a><!DOCTYPE ab xx z> "#
r"<a><!DOCTYPE ab xx z> "
);
assert_oks!(for lex and buf ;
Token::OpeningTagStart
Expand All @@ -896,7 +890,7 @@ mod tests {
#[test]
fn tricky_comments() {
let (mut lex, mut buf) = make_lex_and_buf(
r#"<a><!-- C ->--></a>"#
r"<a><!-- C ->--></a>"
);
assert_oks!(for lex and buf ;
Token::OpeningTagStart
Expand Down Expand Up @@ -1146,7 +1140,7 @@ mod tests {
#[test]
fn issue_98_cdata_ending_with_right_bracket() {
let (mut lex, mut buf) = make_lex_and_buf(
r#"<![CDATA[Foo [Bar]]]>"#
r"<![CDATA[Foo [Bar]]]>"
);

assert_oks!(for lex and buf ;
Expand Down
22 changes: 11 additions & 11 deletions src/reader/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ enum QuoteToken {

impl QuoteToken {
#[inline]
fn from_token(t: &Token) -> Option<QuoteToken> {
match *t {
fn from_token(t: Token) -> Option<QuoteToken> {
match t {
Token::SingleQuote => Some(QuoteToken::SingleQuoteToken),
Token::DoubleQuote => Some(QuoteToken::DoubleQuoteToken),
_ => {
Expand Down Expand Up @@ -489,7 +489,7 @@ impl PullParser {
let name = this.take_buf();
match name.parse() {
Ok(name) => on_name(this, t, name),
Err(_) => Some(this.error(SyntaxError::InvalidQualifiedName(name.into()))),
Err(()) => Some(this.error(SyntaxError::InvalidQualifiedName(name.into()))),
}
};

Expand Down Expand Up @@ -535,7 +535,7 @@ impl PullParser {

Token::DoubleQuote | Token::SingleQuote => match self.data.quote {
None => { // Entered attribute value
self.data.quote = QuoteToken::from_token(&t);
self.data.quote = QuoteToken::from_token(t);
None
}
Some(q) if q.as_token() == t => {
Expand Down Expand Up @@ -716,9 +716,9 @@ mod tests {

#[test]
fn issue_140_entity_reference_inside_tag() {
let (mut r, mut p) = test_data!(r#"
let (mut r, mut p) = test_data!(r"
<bla>&#9835;</bla>
"#);
");

expect_event!(r, p, Ok(XmlEvent::StartDocument { .. }));
expect_event!(r, p, Ok(XmlEvent::StartElement { ref name, .. }) => *name == OwnedName::local("bla"));
Expand All @@ -729,18 +729,18 @@ mod tests {

#[test]
fn issue_220_comment() {
let (mut r, mut p) = test_data!(r#"<x><!-- <!--></x>"#);
let (mut r, mut p) = test_data!(r"<x><!-- <!--></x>");
expect_event!(r, p, Ok(XmlEvent::StartDocument { .. }));
expect_event!(r, p, Ok(XmlEvent::StartElement { .. }));
expect_event!(r, p, Ok(XmlEvent::EndElement { .. }));
expect_event!(r, p, Ok(XmlEvent::EndDocument));

let (mut r, mut p) = test_data!(r#"<x><!-- <!---></x>"#);
let (mut r, mut p) = test_data!(r"<x><!-- <!---></x>");
expect_event!(r, p, Ok(XmlEvent::StartDocument { .. }));
expect_event!(r, p, Ok(XmlEvent::StartElement { .. }));
expect_event!(r, p, Err(_)); // ---> is forbidden in comments

let (mut r, mut p) = test_data!(r#"<x><!--<text&x;> <!--></x>"#);
let (mut r, mut p) = test_data!(r"<x><!--<text&x;> <!--></x>");
p.config.c.ignore_comments = false;
expect_event!(r, p, Ok(XmlEvent::StartDocument { .. }));
expect_event!(r, p, Ok(XmlEvent::StartElement { .. }));
Expand Down Expand Up @@ -786,9 +786,9 @@ mod tests {

#[test]
fn reference_err() {
let (mut r, mut p) = test_data!(r#"
let (mut r, mut p) = test_data!(r"
<a>&&amp;</a>
"#);
");

expect_event!(r, p, Ok(XmlEvent::StartDocument { .. }));
expect_event!(r, p, Ok(XmlEvent::StartElement { .. }));
Expand Down
6 changes: 3 additions & 3 deletions src/reader/parser/inside_doctype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl PullParser {
},
Token::SingleQuote | Token::DoubleQuote => {
// just discard string literals
self.data.quote = super::QuoteToken::from_token(&t);
self.data.quote = super::QuoteToken::from_token(t);
self.into_state_continue(State::InsideDoctype(DoctypeSubstate::String))
},
Token::CDataEnd | Token::CDataStart => Some(self.error(SyntaxError::UnexpectedToken(t))),
Expand Down Expand Up @@ -98,12 +98,12 @@ impl PullParser {
// SYSTEM/PUBLIC not supported
Token::Character('S' | 'P') => {
let name = self.data.take_name();
self.entities.entry(name).or_insert_with(String::new); // Dummy value, but at least the name is recognized
self.entities.entry(name).or_default(); // Dummy value, but at least the name is recognized

self.into_state_continue(State::InsideDoctype(DoctypeSubstate::SkipDeclaration))
},
Token::SingleQuote | Token::DoubleQuote => {
self.data.quote = super::QuoteToken::from_token(&t);
self.data.quote = super::QuoteToken::from_token(t);
self.into_state_continue(State::InsideDoctype(DoctypeSubstate::EntityValue))
},
_ => Some(self.error(SyntaxError::UnexpectedTokenInEntity(t))),
Expand Down
6 changes: 3 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ mod tests {
assert!(matches!(CharReader::new().next_char_from(&mut bytes), Err(CharReadError::UnexpectedEof)));

let mut bytes: &[u8] = b"\xEF\xBB\x42"; // Nothing after BO
assert!(matches!(CharReader::new().next_char_from(&mut bytes), Err(_)));
assert!(CharReader::new().next_char_from(&mut bytes).is_err());

let mut bytes: &[u8] = b"\xFE\xFF\x00\x42"; // UTF-16
assert_eq!(CharReader::new().next_char_from(&mut bytes).unwrap(), Some('B'));
Expand All @@ -258,7 +258,7 @@ mod tests {
assert_eq!(CharReader { encoding: Encoding::Utf16Le }.next_char_from(&mut bytes).unwrap(), Some('뿐'));

let mut bytes: &[u8] = b"\xD8\xD8\x80";
assert!(matches!(CharReader { encoding: Encoding::Utf16 }.next_char_from(&mut bytes), Err(_)));
assert!(CharReader { encoding: Encoding::Utf16 }.next_char_from(&mut bytes).is_err());

let mut bytes: &[u8] = b"\x00\x42";
assert_eq!(CharReader { encoding: Encoding::Utf16 }.next_char_from(&mut bytes).unwrap(), Some('B'));
Expand All @@ -267,7 +267,7 @@ mod tests {
assert_eq!(CharReader { encoding: Encoding::Utf16 }.next_char_from(&mut bytes).unwrap(), Some('B'));

let mut bytes: &[u8] = b"\x00";
assert!(matches!(CharReader { encoding: Encoding::Utf16Be }.next_char_from(&mut bytes), Err(_)));
assert!(CharReader { encoding: Encoding::Utf16Be }.next_char_from(&mut bytes).is_err());

let mut bytes: &[u8] = "😊".as_bytes(); // correct non-BMP
assert_eq!(CharReader::new().next_char_from(&mut bytes).unwrap(), Some('😊'));
Expand Down
2 changes: 1 addition & 1 deletion src/writer/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl Emitter {

pub fn emit_attributes<W: Write>(&mut self, target: &mut W,
attributes: &[Attribute<'_>]) -> Result<()> {
for attr in attributes.iter() {
for attr in attributes {
write!(target, " {}=\"", attr.name.repr_display())?;
if self.config.perform_escaping {
write!(target, "{}", Escaped::<AttributeEscapes>::new(attr.value))?;
Expand Down
Loading

0 comments on commit 4cd3b5a

Please sign in to comment.