Skip to content

Commit 648919b

Browse files
committed
Address compiler / rustdoc / clippy warnings
1 parent 5677448 commit 648919b

File tree

9 files changed

+77
-81
lines changed

9 files changed

+77
-81
lines changed

src/axis.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub enum Axis {
4141
FollowingSibling,
4242
Preceding,
4343
Following,
44+
#[allow(clippy::enum_variant_names)]
4445
SelfAxis,
4546
}
4647

src/context.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,24 @@ type Namespaces = HashMap<String, String>;
4747
/// }
4848
/// }
4949
///
50-
/// fn main() {
51-
/// let package = parser::parse("<thing xmlns:ns0='net:brain' ns0:bonus='1' />")
52-
/// .expect("failed to parse XML");
53-
/// let document = package.as_document();
54-
/// let node = document.root().children()[0];
50+
/// let package = parser::parse("<thing xmlns:ns0='net:brain' ns0:bonus='1' />")
51+
/// .expect("failed to parse XML");
52+
/// let document = package.as_document();
53+
/// let node = document.root().children()[0];
5554
///
56-
/// let mut context = Context::new();
57-
/// context.set_function("sigmoid", Sigmoid);
58-
/// context.set_variable("t", 2.0);
59-
/// context.set_namespace("neural", "net:brain");
55+
/// let mut context = Context::new();
56+
/// context.set_function("sigmoid", Sigmoid);
57+
/// context.set_variable("t", 2.0);
58+
/// context.set_namespace("neural", "net:brain");
6059
///
61-
/// let xpath = "sigmoid(@neural:bonus + $t)";
60+
/// let xpath = "sigmoid(@neural:bonus + $t)";
6261
///
63-
/// let factory = Factory::new();
64-
/// let xpath = factory.build(xpath).expect("Could not compile XPath");
62+
/// let factory = Factory::new();
63+
/// let xpath = factory.build(xpath).expect("Could not compile XPath");
6564
///
66-
/// let value = xpath.evaluate(&context, node).expect("XPath evaluation failed");
65+
/// let value = xpath.evaluate(&context, node).expect("XPath evaluation failed");
6766
///
68-
/// assert_eq!(0.952, (value.number() * 1000.0).trunc() / 1000.0);
69-
/// }
67+
/// assert_eq!(0.952, (value.number() * 1000.0).trunc() / 1000.0);
7068
/// ```
7169
///
7270
/// Note that we are using a custom function (`sigmoid`), a variable

src/expression.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ pub type SubExpression = Box<dyn Expression + 'static>;
6464
macro_rules! binary_constructor(
6565
($t:ident) => (
6666
impl $t {
67+
#[allow(clippy::new_ret_no_self)]
6768
pub fn new(left: SubExpression, right: SubExpression) -> SubExpression {
68-
Box::new($t{left: left, right: right})
69+
Box::new($t{ left, right })
6970
}
7071
}
7172
);
@@ -126,15 +127,15 @@ impl Equal {
126127
}
127128

128129
let v = match (&left_val, &right_val) {
129-
(&Value::Nodeset(ref left_nodes), &Value::Nodeset(ref right_nodes)) => {
130+
(Value::Nodeset(left_nodes), Value::Nodeset(right_nodes)) => {
130131
let left_strings = str_vals(left_nodes);
131132
let right_strings = str_vals(right_nodes);
132133
!left_strings.is_disjoint(&right_strings)
133134
}
134135
(&Value::Nodeset(ref nodes), &Number(val))
135136
| (&Number(val), &Value::Nodeset(ref nodes)) => {
136137
let numbers = num_vals(nodes);
137-
numbers.iter().any(|n| *n == val)
138+
numbers.contains(&val)
138139
}
139140
(&Value::Nodeset(ref nodes), &Value::String(ref val))
140141
| (&Value::String(ref val), &Value::Nodeset(ref nodes)) => {
@@ -162,6 +163,7 @@ pub struct NotEqual {
162163
}
163164

164165
impl NotEqual {
166+
#[allow(clippy::new_ret_no_self)]
165167
pub fn new(left: SubExpression, right: SubExpression) -> SubExpression {
166168
Box::new(NotEqual {
167169
equal: Equal { left, right },
@@ -334,6 +336,7 @@ pub struct Path {
334336
}
335337

336338
impl Path {
339+
#[allow(clippy::new_ret_no_self)]
337340
pub fn new(start_point: SubExpression, steps: Vec<Step>) -> SubExpression {
338341
Box::new(Path { start_point, steps })
339342
}
@@ -359,6 +362,7 @@ pub struct Filter {
359362
}
360363

361364
impl Filter {
365+
#[allow(clippy::new_ret_no_self)]
362366
pub fn new(node_selector: SubExpression, predicate: SubExpression) -> SubExpression {
363367
let predicate = Predicate {
364368
expression: predicate,
@@ -728,7 +732,7 @@ mod test {
728732
name: "left".into(),
729733
});
730734
let right = Box::new(Literal {
731-
value: Value::Number(6.28),
735+
value: Value::Number(6.3),
732736
});
733737

734738
let expr = Equal { left, right };

src/function.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,13 @@ impl Function for TwoStringPredicate {
353353
fn starts_with() -> TwoStringPredicate {
354354
fn imp(a: &str, b: &str) -> bool {
355355
str::starts_with(a, b)
356-
};
356+
}
357357
TwoStringPredicate(imp)
358358
}
359359
fn contains() -> TwoStringPredicate {
360360
fn imp(a: &str, b: &str) -> bool {
361361
str::contains(a, b)
362-
};
362+
}
363363
TwoStringPredicate(imp)
364364
}
365365

@@ -501,7 +501,7 @@ impl Function for Translate {
501501

502502
let s = s
503503
.chars()
504-
.filter_map(|c| replacements.get(&c).cloned().unwrap_or_else(|| Some(c)))
504+
.filter_map(|c| replacements.get(&c).cloned().unwrap_or(Some(c)))
505505
.collect();
506506

507507
Ok(Value::String(s))
@@ -1031,7 +1031,7 @@ mod test {
10311031
fn assert_number(expected: f64, actual: Result<Value<'_>, Error>) {
10321032
match actual {
10331033
Ok(Value::Number(n)) => assert_eq!(PedanticNumber(n), PedanticNumber(expected)),
1034-
_ => assert!(false, "{:?} did not evaluate correctly", actual),
1034+
_ => panic!("{:?} did not evaluate correctly", actual),
10351035
}
10361036
}
10371037

src/lib.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919
//! use sxd_document::parser;
2020
//! use sxd_xpath::{evaluate_xpath, Value};
2121
//!
22-
//! fn main() {
23-
//! let package = parser::parse("<root>hello</root>").expect("failed to parse XML");
24-
//! let document = package.as_document();
22+
//! let package = parser::parse("<root>hello</root>").expect("failed to parse XML");
23+
//! let document = package.as_document();
2524
//!
26-
//! let value = evaluate_xpath(&document, "/root").expect("XPath evaluation failed");
25+
//! let value = evaluate_xpath(&document, "/root").expect("XPath evaluation failed");
2726
//!
28-
//! assert_eq!("hello", value.string());
29-
//! }
27+
//! assert_eq!("hello", value.string());
3028
//! ```
3129
//!
3230
//! Evaluating an XPath returns a [`Value`][], representing the
@@ -45,21 +43,19 @@
4543
//! use sxd_document::parser;
4644
//! use sxd_xpath::{Factory, Context, Value};
4745
//!
48-
//! fn main() {
49-
//! let package = parser::parse("<root>hello</root>")
50-
//! .expect("failed to parse XML");
51-
//! let document = package.as_document();
46+
//! let package = parser::parse("<root>hello</root>")
47+
//! .expect("failed to parse XML");
48+
//! let document = package.as_document();
5249
//!
53-
//! let factory = Factory::new();
54-
//! let xpath = factory.build("/root").expect("Could not compile XPath");
50+
//! let factory = Factory::new();
51+
//! let xpath = factory.build("/root").expect("Could not compile XPath");
5552
//!
56-
//! let context = Context::new();
53+
//! let context = Context::new();
5754
//!
58-
//! let value = xpath.evaluate(&context, document.root())
59-
//! .expect("XPath evaluation failed");
55+
//! let value = xpath.evaluate(&context, document.root())
56+
//! .expect("XPath evaluation failed");
6057
//!
61-
//! assert_eq!("hello", value.string());
62-
//! }
58+
//! assert_eq!("hello", value.string());
6359
//! ```
6460
//!
6561
//! See [`Context`][] for details on how to customize the
@@ -83,22 +79,29 @@
8379
//! defined prefixes, some XPath behavior may be confusing:
8480
//!
8581
//! 1. The `name` method will not include a prefix, even if the
86-
//! element or attribute has a namespace.
82+
//! element or attribute has a namespace.
8783
//! 2. The `namespace` axis will not include namespaces without
88-
//! prefixes.
84+
//! prefixes.
8985
//!
9086
//! #### Document order
9187
//!
9288
//! If you have programmatically created XML but not attached the
9389
//! nodes to the document, some XPath behavior may be confusing:
9490
//!
9591
//! 1. These nodes have no [*document order*]. If you create a
96-
//! variable containing these nodes and apply a predicate to them,
97-
//! these nodes will appear after any nodes that are present in the
98-
//! document, but the relative order of the nodes is undefined.
92+
//! variable containing these nodes and apply a predicate to them,
93+
//! these nodes will appear after any nodes that are present in the
94+
//! document, but the relative order of the nodes is undefined.
9995
//!
10096
//! [*document order*]: https://www.w3.org/TR/xpath/#dt-document-order
10197
98+
// Ignoring these as our MSRV predates the suggestions
99+
#![allow(
100+
clippy::legacy_numeric_constants,
101+
clippy::match_like_matches_macro,
102+
clippy::option_as_ref_deref
103+
)]
104+
102105
use snafu::{ResultExt, Snafu};
103106
use std::borrow::ToOwned;
104107
use std::string;
@@ -350,7 +353,7 @@ impl XPath {
350353
///
351354
/// The most common case is to pass in a reference to a [`Context`][]:
352355
///
353-
/// ```rust,no-run
356+
/// ```rust,no_run
354357
/// use sxd_document::dom::Document;
355358
/// use sxd_xpath::{XPath, Context};
356359
///
@@ -442,12 +445,10 @@ pub enum Error {
442445
/// use sxd_document::parser;
443446
/// use sxd_xpath::{evaluate_xpath, Value};
444447
///
445-
/// fn main() {
446-
/// let package = parser::parse("<root><a>1</a><b>2</b></root>").expect("failed to parse the XML");
447-
/// let document = package.as_document();
448+
/// let package = parser::parse("<root><a>1</a><b>2</b></root>").expect("failed to parse the XML");
449+
/// let document = package.as_document();
448450
///
449-
/// assert_eq!(Ok(Value::Number(3.0)), evaluate_xpath(&document, "/*/a + /*/b"));
450-
/// }
451+
/// assert_eq!(Ok(Value::Number(3.0)), evaluate_xpath(&document, "/*/a + /*/b"));
451452
/// ```
452453
pub fn evaluate_xpath<'d>(document: &'d Document<'d>, xpath: &str) -> Result<Value<'d>, Error> {
453454
let factory = Factory::new();

src/node_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ mod test {
181181
}
182182

183183
impl<'d> Setup<'d> {
184-
fn new(package: &'d Package) -> Setup<'_> {
184+
fn new(package: &'d Package) -> Setup<'d> {
185185
Setup {
186186
doc: package.as_document(),
187187
context: Context::without_core_functions(),

src/nodeset.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'d> Node<'d> {
110110
} else {
111111
name.local_part().to_owned()
112112
}
113-
};
113+
}
114114

115115
match *self {
116116
Root(_) => None,
@@ -234,7 +234,7 @@ impl<'d> Node<'d> {
234234
_ => {}
235235
}
236236
}
237-
};
237+
}
238238

239239
fn text_descendants_string_value(node: Node<'_>) -> String {
240240
let mut result = String::new();
@@ -279,21 +279,21 @@ conversion_trait!(Node, {
279279
dom::ProcessingInstruction => Node::ProcessingInstruction
280280
});
281281

282-
impl<'d> Into<Node<'d>> for dom::ChildOfRoot<'d> {
283-
fn into(self) -> Node<'d> {
282+
impl<'d> From<dom::ChildOfRoot<'d>> for Node<'d> {
283+
fn from(other: dom::ChildOfRoot<'d>) -> Node<'d> {
284284
use self::Node::*;
285-
match self {
285+
match other {
286286
dom::ChildOfRoot::Element(n) => Element(n),
287287
dom::ChildOfRoot::Comment(n) => Comment(n),
288288
dom::ChildOfRoot::ProcessingInstruction(n) => ProcessingInstruction(n),
289289
}
290290
}
291291
}
292292

293-
impl<'d> Into<Node<'d>> for dom::ChildOfElement<'d> {
294-
fn into(self) -> Node<'d> {
293+
impl<'d> From<dom::ChildOfElement<'d>> for Node<'d> {
294+
fn from(other: dom::ChildOfElement<'d>) -> Node<'d> {
295295
use self::Node::*;
296-
match self {
296+
match other {
297297
dom::ChildOfElement::Element(n) => Element(n),
298298
dom::ChildOfElement::Text(n) => Text(n),
299299
dom::ChildOfElement::Comment(n) => Comment(n),
@@ -302,10 +302,10 @@ impl<'d> Into<Node<'d>> for dom::ChildOfElement<'d> {
302302
}
303303
}
304304

305-
impl<'d> Into<Node<'d>> for dom::ParentOfChild<'d> {
306-
fn into(self) -> Node<'d> {
305+
impl<'d> From<dom::ParentOfChild<'d>> for Node<'d> {
306+
fn from(other: dom::ParentOfChild<'d>) -> Self {
307307
use self::Node::*;
308-
match self {
308+
match other {
309309
dom::ParentOfChild::Root(n) => Root(n),
310310
dom::ParentOfChild::Element(n) => Element(n),
311311
}
@@ -351,10 +351,7 @@ impl<'d> Nodeset<'d> {
351351
///
352352
/// [document order]: https://www.w3.org/TR/xpath/#dt-document-order
353353
pub fn document_order_first(&self) -> Option<Node<'d>> {
354-
let node = match self.nodes.iter().next() {
355-
Some(n) => n,
356-
None => return None,
357-
};
354+
let node = self.nodes.iter().next()?;
358355

359356
if self.nodes.len() == 1 {
360357
return Some(*node);
@@ -403,6 +400,7 @@ impl<'d> DocOrder<'d> {
403400
fn new(doc: dom::Document<'d>) -> Self {
404401
let mut idx = 0;
405402
let mut stack: Vec<Node<'_>> = vec![doc.root().into()];
403+
#[allow(clippy::mutable_key_type)]
406404
let mut order = HashMap::new();
407405

408406
while let Some(n) = stack.pop() {

0 commit comments

Comments
 (0)