Skip to content

Commit 08590c8

Browse files
committed
Merge pull request #53 from dtolnay/clippy
Add clippy feature
2 parents 518c9c7 + 3c8f768 commit 08590c8

File tree

3 files changed

+44
-37
lines changed

3 files changed

+44
-37
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ matrix:
55
- rust: beta
66
env: FEATURES="serde_impl"
77
- rust: nightly
8-
env: FEATURES="serde_impl nightly"
8+
env: FEATURES="serde_impl nightly clippy"
99
script:
1010
- cargo build --features "$FEATURES"
1111
- cargo test --features "$FEATURES"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ nightly = []
2020
serde_impl = ["serde", "serde_json"]
2121

2222
[dependencies]
23+
clippy = { version = "0.*", optional = true }
2324
serde = { version = "^0.7", optional = true }
2425
serde_json = { version = "^0.7", optional = true }

src/lib.rs

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! A HashMap wrapper that holds key-value pairs in insertion order.
11+
//! A `HashMap` wrapper that holds key-value pairs in insertion order.
1212
//!
1313
//! # Examples
1414
//!
@@ -30,6 +30,10 @@
3030
#![forbid(missing_docs)]
3131
#![cfg_attr(all(feature = "nightly", test), feature(test))]
3232

33+
#![cfg_attr(feature = "clippy", feature(plugin))]
34+
#![cfg_attr(feature = "clippy", plugin(clippy))]
35+
#![cfg_attr(feature = "clippy", deny(clippy))]
36+
3337
// Optional Serde support
3438
#[cfg(feature = "serde_impl")]
3539
pub mod serde;
@@ -302,7 +306,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
302306
self.detach(node_ptr);
303307
self.attach(node_ptr);
304308
}
305-
return value;
309+
value
306310
}
307311

308312
/// Removes and returns the value corresponding to the key from the map.
@@ -367,14 +371,14 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
367371
/// ```
368372
#[inline]
369373
pub fn pop_front(&mut self) -> Option<(K, V)> {
370-
if self.len() > 0 {
371-
let lru = unsafe { (*self.head).prev };
372-
self.detach(lru);
373-
return self.map
374-
.remove(&KeyRef{k: unsafe { &(*lru).key }})
375-
.map(|e| { let e = *e; (e.key, e.value) })
374+
if self.is_empty() {
375+
return None
376376
}
377-
None
377+
let lru = unsafe { (*self.head).prev };
378+
self.detach(lru);
379+
self.map
380+
.remove(&KeyRef{k: unsafe { &(*lru).key }})
381+
.map(|e| { let e = *e; (e.key, e.value) })
378382
}
379383

380384
/// Gets the first entry.
@@ -390,12 +394,12 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
390394
/// ```
391395
#[inline]
392396
pub fn front(&self) -> Option<(&K, &V)> {
393-
if self.len() > 0 {
394-
let lru = unsafe { (*self.head).prev };
395-
return self.map.get(&KeyRef{k: unsafe { &(*lru).key }})
396-
.map(|e| (&e.key, &e.value))
397+
if self.is_empty() {
398+
return None
397399
}
398-
None
400+
let lru = unsafe { (*self.head).prev };
401+
self.map.get(&KeyRef{k: unsafe { &(*lru).key }})
402+
.map(|e| (&e.key, &e.value))
399403
}
400404

401405
/// Removes the last entry.
@@ -413,14 +417,14 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
413417
/// ```
414418
#[inline]
415419
pub fn pop_back(&mut self) -> Option<(K, V)> {
416-
if self.len() > 0 {
417-
let mru = unsafe { (*self.head).next };
418-
self.detach(mru);
419-
return self.map
420-
.remove(&KeyRef{k: unsafe { &(*mru).key }})
421-
.map(|e| { let e = *e; (e.key, e.value) })
420+
if self.is_empty() {
421+
return None
422422
}
423-
None
423+
let mru = unsafe { (*self.head).next };
424+
self.detach(mru);
425+
self.map
426+
.remove(&KeyRef{k: unsafe { &(*mru).key }})
427+
.map(|e| { let e = *e; (e.key, e.value) })
424428
}
425429

426430
/// Gets the last entry.
@@ -436,12 +440,12 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
436440
/// ```
437441
#[inline]
438442
pub fn back(&mut self) -> Option<(&K, &V)> {
439-
if self.len() > 0 {
440-
let mru = unsafe { (*self.head).next };
441-
return self.map.get(&KeyRef{k: unsafe { &(*mru).key }})
442-
.map(|e| (&e.key, &e.value))
443+
if self.is_empty() {
444+
return None
443445
}
444-
None
446+
let mru = unsafe { (*self.head).next };
447+
self.map.get(&KeyRef{k: unsafe { &(*mru).key }})
448+
.map(|e| (&e.key, &e.value))
445449
}
446450

447451
/// Returns the number of key-value pairs in the map.
@@ -481,10 +485,10 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
481485
/// assert_eq!(None, iter.next());
482486
/// ```
483487
pub fn iter(&self) -> Iter<K, V> {
484-
let head = if ! self.head.is_null() {
485-
unsafe { (*self.head).prev }
486-
} else {
488+
let head = if self.head.is_null() {
487489
ptr::null_mut()
490+
} else {
491+
unsafe { (*self.head).prev }
488492
};
489493
Iter {
490494
head: head,
@@ -515,10 +519,10 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
515519
/// assert_eq!(&17, map.get(&"a").unwrap());
516520
/// ```
517521
pub fn iter_mut(&mut self) -> IterMut<K, V> {
518-
let head = if ! self.head.is_null() {
519-
unsafe { (*self.head).prev }
520-
} else {
522+
let head = if self.head.is_null() {
521523
ptr::null_mut()
524+
} else {
525+
unsafe { (*self.head).prev }
522526
};
523527
IterMut {
524528
head: head,
@@ -545,6 +549,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
545549
/// assert_eq!(&'b', keys.next().unwrap());
546550
/// assert_eq!(None, keys.next());
547551
/// ```
552+
#[cfg_attr(feature = "clippy", allow(needless_lifetimes))] // false positive
548553
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
549554
fn first<A, B>((a, _): (A, B)) -> A { a }
550555
let first: fn((&'a K, &'a V)) -> &'a K = first; // coerce to fn ptr
@@ -569,6 +574,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
569574
/// assert_eq!(&20, values.next().unwrap());
570575
/// assert_eq!(None, values.next());
571576
/// ```
577+
#[cfg_attr(feature = "clippy", allow(needless_lifetimes))] // false positive
572578
pub fn values<'a>(&'a self) -> Values<'a, K, V> {
573579
fn second<A, B>((_, b): (A, B)) -> B { b }
574580
let second: fn((&'a K, &'a V)) -> &'a V = second; // coerce to fn ptr
@@ -808,8 +814,7 @@ impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V> {
808814
self.remaining -= 1;
809815
unsafe {
810816
self.tail = (*self.tail).next;
811-
let r = Some((&(*self.tail).key, &(*self.tail).value));
812-
r
817+
Some((&(*self.tail).key, &(*self.tail).value))
813818
}
814819
}
815820
}
@@ -823,8 +828,7 @@ impl<'a, K, V> DoubleEndedIterator for IterMut<'a, K, V> {
823828
self.remaining -= 1;
824829
unsafe {
825830
self.tail = (*self.tail).next;
826-
let r = Some((&(*self.tail).key, &mut (*self.tail).value));
827-
r
831+
Some((&(*self.tail).key, &mut (*self.tail).value))
828832
}
829833
}
830834
}
@@ -840,6 +844,7 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
840844

841845
/// An insertion-order iterator over a `LinkedHashMap`'s keys.
842846
pub struct Keys<'a, K: 'a, V: 'a> {
847+
#[cfg_attr(feature = "clippy", allow(type_complexity))]
843848
inner: iter::Map<Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
844849
}
845850

@@ -864,6 +869,7 @@ impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
864869

865870
/// An insertion-order iterator over a `LinkedHashMap`'s values.
866871
pub struct Values<'a, K: 'a, V: 'a> {
872+
#[cfg_attr(feature = "clippy", allow(type_complexity))]
867873
inner: iter::Map<Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
868874
}
869875

0 commit comments

Comments
 (0)