diff --git a/src/axis.rs b/src/axis.rs index f8ea7737..73939a49 100644 --- a/src/axis.rs +++ b/src/axis.rs @@ -41,6 +41,7 @@ pub enum Axis { FollowingSibling, Preceding, Following, + #[allow(clippy::enum_variant_names)] SelfAxis, } diff --git a/src/context.rs b/src/context.rs index aa952d5a..50739a8e 100644 --- a/src/context.rs +++ b/src/context.rs @@ -47,26 +47,24 @@ type Namespaces = HashMap; /// } /// } /// -/// fn main() { -/// let package = parser::parse("") -/// .expect("failed to parse XML"); -/// let document = package.as_document(); -/// let node = document.root().children()[0]; +/// let package = parser::parse("") +/// .expect("failed to parse XML"); +/// let document = package.as_document(); +/// let node = document.root().children()[0]; /// -/// let mut context = Context::new(); -/// context.set_function("sigmoid", Sigmoid); -/// context.set_variable("t", 2.0); -/// context.set_namespace("neural", "net:brain"); +/// let mut context = Context::new(); +/// context.set_function("sigmoid", Sigmoid); +/// context.set_variable("t", 2.0); +/// context.set_namespace("neural", "net:brain"); /// -/// let xpath = "sigmoid(@neural:bonus + $t)"; +/// let xpath = "sigmoid(@neural:bonus + $t)"; /// -/// let factory = Factory::new(); -/// let xpath = factory.build(xpath).expect("Could not compile XPath"); +/// let factory = Factory::new(); +/// let xpath = factory.build(xpath).expect("Could not compile XPath"); /// -/// let value = xpath.evaluate(&context, node).expect("XPath evaluation failed"); +/// let value = xpath.evaluate(&context, node).expect("XPath evaluation failed"); /// -/// assert_eq!(0.952, (value.number() * 1000.0).trunc() / 1000.0); -/// } +/// assert_eq!(0.952, (value.number() * 1000.0).trunc() / 1000.0); /// ``` /// /// Note that we are using a custom function (`sigmoid`), a variable diff --git a/src/expression.rs b/src/expression.rs index 1cb2f0d6..deb97592 100644 --- a/src/expression.rs +++ b/src/expression.rs @@ -64,8 +64,9 @@ pub type SubExpression = Box; macro_rules! binary_constructor( ($t:ident) => ( impl $t { + #[allow(clippy::new_ret_no_self)] pub fn new(left: SubExpression, right: SubExpression) -> SubExpression { - Box::new($t{left: left, right: right}) + Box::new($t{ left, right }) } } ); @@ -126,7 +127,7 @@ impl Equal { } let v = match (&left_val, &right_val) { - (&Value::Nodeset(ref left_nodes), &Value::Nodeset(ref right_nodes)) => { + (Value::Nodeset(left_nodes), Value::Nodeset(right_nodes)) => { let left_strings = str_vals(left_nodes); let right_strings = str_vals(right_nodes); !left_strings.is_disjoint(&right_strings) @@ -134,7 +135,7 @@ impl Equal { (&Value::Nodeset(ref nodes), &Number(val)) | (&Number(val), &Value::Nodeset(ref nodes)) => { let numbers = num_vals(nodes); - numbers.iter().any(|n| *n == val) + numbers.contains(&val) } (&Value::Nodeset(ref nodes), &Value::String(ref val)) | (&Value::String(ref val), &Value::Nodeset(ref nodes)) => { @@ -162,6 +163,7 @@ pub struct NotEqual { } impl NotEqual { + #[allow(clippy::new_ret_no_self)] pub fn new(left: SubExpression, right: SubExpression) -> SubExpression { Box::new(NotEqual { equal: Equal { left, right }, @@ -334,6 +336,7 @@ pub struct Path { } impl Path { + #[allow(clippy::new_ret_no_self)] pub fn new(start_point: SubExpression, steps: Vec) -> SubExpression { Box::new(Path { start_point, steps }) } @@ -359,6 +362,7 @@ pub struct Filter { } impl Filter { + #[allow(clippy::new_ret_no_self)] pub fn new(node_selector: SubExpression, predicate: SubExpression) -> SubExpression { let predicate = Predicate { expression: predicate, @@ -728,7 +732,7 @@ mod test { name: "left".into(), }); let right = Box::new(Literal { - value: Value::Number(6.28), + value: Value::Number(6.3), }); let expr = Equal { left, right }; diff --git a/src/function.rs b/src/function.rs index 0adbaefd..51dfe663 100644 --- a/src/function.rs +++ b/src/function.rs @@ -353,13 +353,13 @@ impl Function for TwoStringPredicate { fn starts_with() -> TwoStringPredicate { fn imp(a: &str, b: &str) -> bool { str::starts_with(a, b) - }; + } TwoStringPredicate(imp) } fn contains() -> TwoStringPredicate { fn imp(a: &str, b: &str) -> bool { str::contains(a, b) - }; + } TwoStringPredicate(imp) } @@ -501,7 +501,7 @@ impl Function for Translate { let s = s .chars() - .filter_map(|c| replacements.get(&c).cloned().unwrap_or_else(|| Some(c))) + .filter_map(|c| replacements.get(&c).cloned().unwrap_or(Some(c))) .collect(); Ok(Value::String(s)) @@ -1031,7 +1031,7 @@ mod test { fn assert_number(expected: f64, actual: Result, Error>) { match actual { Ok(Value::Number(n)) => assert_eq!(PedanticNumber(n), PedanticNumber(expected)), - _ => assert!(false, "{:?} did not evaluate correctly", actual), + _ => panic!("{:?} did not evaluate correctly", actual), } } diff --git a/src/lib.rs b/src/lib.rs index 2f62faa6..9c420837 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,14 +19,12 @@ //! use sxd_document::parser; //! use sxd_xpath::{evaluate_xpath, Value}; //! -//! fn main() { -//! let package = parser::parse("hello").expect("failed to parse XML"); -//! let document = package.as_document(); +//! let package = parser::parse("hello").expect("failed to parse XML"); +//! let document = package.as_document(); //! -//! let value = evaluate_xpath(&document, "/root").expect("XPath evaluation failed"); +//! let value = evaluate_xpath(&document, "/root").expect("XPath evaluation failed"); //! -//! assert_eq!("hello", value.string()); -//! } +//! assert_eq!("hello", value.string()); //! ``` //! //! Evaluating an XPath returns a [`Value`][], representing the @@ -45,21 +43,19 @@ //! use sxd_document::parser; //! use sxd_xpath::{Factory, Context, Value}; //! -//! fn main() { -//! let package = parser::parse("hello") -//! .expect("failed to parse XML"); -//! let document = package.as_document(); +//! let package = parser::parse("hello") +//! .expect("failed to parse XML"); +//! let document = package.as_document(); //! -//! let factory = Factory::new(); -//! let xpath = factory.build("/root").expect("Could not compile XPath"); +//! let factory = Factory::new(); +//! let xpath = factory.build("/root").expect("Could not compile XPath"); //! -//! let context = Context::new(); +//! let context = Context::new(); //! -//! let value = xpath.evaluate(&context, document.root()) -//! .expect("XPath evaluation failed"); +//! let value = xpath.evaluate(&context, document.root()) +//! .expect("XPath evaluation failed"); //! -//! assert_eq!("hello", value.string()); -//! } +//! assert_eq!("hello", value.string()); //! ``` //! //! See [`Context`][] for details on how to customize the @@ -83,9 +79,9 @@ //! defined prefixes, some XPath behavior may be confusing: //! //! 1. The `name` method will not include a prefix, even if the -//! element or attribute has a namespace. +//! element or attribute has a namespace. //! 2. The `namespace` axis will not include namespaces without -//! prefixes. +//! prefixes. //! //! #### Document order //! @@ -93,12 +89,19 @@ //! nodes to the document, some XPath behavior may be confusing: //! //! 1. These nodes have no [*document order*]. If you create a -//! variable containing these nodes and apply a predicate to them, -//! these nodes will appear after any nodes that are present in the -//! document, but the relative order of the nodes is undefined. +//! variable containing these nodes and apply a predicate to them, +//! these nodes will appear after any nodes that are present in the +//! document, but the relative order of the nodes is undefined. //! //! [*document order*]: https://www.w3.org/TR/xpath/#dt-document-order +// Ignoring these as our MSRV predates the suggestions +#![allow( + clippy::legacy_numeric_constants, + clippy::match_like_matches_macro, + clippy::option_as_ref_deref +)] + use snafu::{ResultExt, Snafu}; use std::borrow::ToOwned; use std::string; @@ -350,7 +353,7 @@ impl XPath { /// /// The most common case is to pass in a reference to a [`Context`][]: /// - /// ```rust,no-run + /// ```rust,no_run /// use sxd_document::dom::Document; /// use sxd_xpath::{XPath, Context}; /// @@ -442,12 +445,10 @@ pub enum Error { /// use sxd_document::parser; /// use sxd_xpath::{evaluate_xpath, Value}; /// -/// fn main() { -/// let package = parser::parse("12").expect("failed to parse the XML"); -/// let document = package.as_document(); +/// let package = parser::parse("12").expect("failed to parse the XML"); +/// let document = package.as_document(); /// -/// assert_eq!(Ok(Value::Number(3.0)), evaluate_xpath(&document, "/*/a + /*/b")); -/// } +/// assert_eq!(Ok(Value::Number(3.0)), evaluate_xpath(&document, "/*/a + /*/b")); /// ``` pub fn evaluate_xpath<'d>(document: &'d Document<'d>, xpath: &str) -> Result, Error> { let factory = Factory::new(); diff --git a/src/node_test.rs b/src/node_test.rs index 4571274d..c43e7466 100644 --- a/src/node_test.rs +++ b/src/node_test.rs @@ -181,7 +181,7 @@ mod test { } impl<'d> Setup<'d> { - fn new(package: &'d Package) -> Setup<'_> { + fn new(package: &'d Package) -> Setup<'d> { Setup { doc: package.as_document(), context: Context::without_core_functions(), diff --git a/src/nodeset.rs b/src/nodeset.rs index 4d0c4625..ee8e8370 100644 --- a/src/nodeset.rs +++ b/src/nodeset.rs @@ -110,7 +110,7 @@ impl<'d> Node<'d> { } else { name.local_part().to_owned() } - }; + } match *self { Root(_) => None, @@ -234,7 +234,7 @@ impl<'d> Node<'d> { _ => {} } } - }; + } fn text_descendants_string_value(node: Node<'_>) -> String { let mut result = String::new(); @@ -279,10 +279,10 @@ conversion_trait!(Node, { dom::ProcessingInstruction => Node::ProcessingInstruction }); -impl<'d> Into> for dom::ChildOfRoot<'d> { - fn into(self) -> Node<'d> { +impl<'d> From> for Node<'d> { + fn from(other: dom::ChildOfRoot<'d>) -> Node<'d> { use self::Node::*; - match self { + match other { dom::ChildOfRoot::Element(n) => Element(n), dom::ChildOfRoot::Comment(n) => Comment(n), dom::ChildOfRoot::ProcessingInstruction(n) => ProcessingInstruction(n), @@ -290,10 +290,10 @@ impl<'d> Into> for dom::ChildOfRoot<'d> { } } -impl<'d> Into> for dom::ChildOfElement<'d> { - fn into(self) -> Node<'d> { +impl<'d> From> for Node<'d> { + fn from(other: dom::ChildOfElement<'d>) -> Node<'d> { use self::Node::*; - match self { + match other { dom::ChildOfElement::Element(n) => Element(n), dom::ChildOfElement::Text(n) => Text(n), dom::ChildOfElement::Comment(n) => Comment(n), @@ -302,10 +302,10 @@ impl<'d> Into> for dom::ChildOfElement<'d> { } } -impl<'d> Into> for dom::ParentOfChild<'d> { - fn into(self) -> Node<'d> { +impl<'d> From> for Node<'d> { + fn from(other: dom::ParentOfChild<'d>) -> Self { use self::Node::*; - match self { + match other { dom::ParentOfChild::Root(n) => Root(n), dom::ParentOfChild::Element(n) => Element(n), } @@ -351,10 +351,7 @@ impl<'d> Nodeset<'d> { /// /// [document order]: https://www.w3.org/TR/xpath/#dt-document-order pub fn document_order_first(&self) -> Option> { - let node = match self.nodes.iter().next() { - Some(n) => n, - None => return None, - }; + let node = self.nodes.iter().next()?; if self.nodes.len() == 1 { return Some(*node); @@ -403,6 +400,7 @@ impl<'d> DocOrder<'d> { fn new(doc: dom::Document<'d>) -> Self { let mut idx = 0; let mut stack: Vec> = vec![doc.root().into()]; + #[allow(clippy::mutable_key_type)] let mut order = HashMap::new(); while let Some(n) = stack.pop() { diff --git a/src/parser.rs b/src/parser.rs index 85c0c584..5d90d62e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -71,7 +71,7 @@ where fn next_token_is(&mut self, token: &Token) -> bool { match self.peek() { - Some(&Ok(ref t)) => t == token, + Some(Ok(t)) => t == token, _ => false, } } @@ -702,12 +702,7 @@ mod test { use super::*; macro_rules! tokens( - ($($e:expr),*) => ({ - // leading _ to allow empty construction without a warning. - let mut _temp: Vec = ::std::vec::Vec::new(); - $(_temp.push(Ok($e));)* - _temp - }); + ($($e:expr),*) => (vec![$(Ok($e),)*]); ($($e:expr),+,) => (tokens!($($e),+)) ); @@ -731,7 +726,7 @@ mod test { impl<'d> ApproxEq for Value<'d> { fn is_approx_eq(&self, other: &Value<'d>) -> bool { match (self, other) { - (&Number(ref x), &Number(ref y)) => x.is_approx_eq(y), + (Number(x), Number(y)) => x.is_approx_eq(y), _ => panic!("It's nonsensical to compare these quantities"), } } @@ -749,12 +744,11 @@ mod test { impl<'d> TestDoc<'d> { fn root(&'d self) -> Root<'d> { - let &TestDoc(ref doc) = self; - doc.root() + self.0.root() } fn top_node(&'d self) -> Element<'d> { - let &TestDoc(ref doc) = self; + let doc = &self.0; let kids = doc.root().children(); match kids.len() { @@ -773,7 +767,7 @@ mod test { } fn add_child(&'d self, parent: Element<'d>, name: &str) -> Element<'d> { - let &TestDoc(ref doc) = self; + let doc = &self.0; let n = doc.create_element(name); parent.append_child(n); @@ -781,7 +775,7 @@ mod test { } fn add_text(&'d self, parent: Element<'d>, value: &str) -> Text<'d> { - let &TestDoc(ref doc) = self; + let doc = &self.0; let tn = doc.create_text(value); parent.append_child(tn); diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 35ec0567..40442929 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -87,7 +87,7 @@ trait XPathParseExt<'a> { impl<'a> XPathParseExt<'a> for StringPoint<'a> { fn consume_quoted_string(&self, quote: &str) -> XPathProgress<'a, &'a str, ()> { - let end_of_str = self.s.find(quote).or_else(|| Some(self.s.len())); + let end_of_str = self.s.find(quote).or(Some(self.s.len())); self.consume_to(end_of_str) } }